deptPage.api.js 2.49 KB
Newer Older
YoonDongMin's avatar
YDm    
YoonDongMin committed
1
2
3
4
5
6
7
8
9

import { DEBUG, enablePromise } from 'react-native-sqlite-storage';
import getDb from './moneyDB'

DEBUG(true);
enablePromise(true);

const insertDept = async (deptData) => {
    const db = await getDb();
Choi Ga Young's avatar
Choi Ga Young committed
10
    const { date, loan, message, money, remained_money } = deptData 
YoonDongMin's avatar
YDm    
YoonDongMin committed
11
12
13
    return new Promise((resolve, reject) => {
        db.transaction((tx) => {
            tx.executeSql('INSERT INTO dept (repayment_date, loan, loan_name, principal, repayment) VALUES (?,?,?,?,?);',
Choi Ga Young's avatar
Choi Ga Young committed
14
                [date, loan, message, money, remained_money], 
YoonDongMin's avatar
YDm    
YoonDongMin committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
                (error) => console.log(error))
            resolve('데이터 삽입 완료');
        })
    })
};





const selectLoan = async (title) => {
    const db = await getDb();
    return new Promise((resolve, reject) => {
        db.transaction(async (tx) => {
            const [txn, results] = await tx.executeSql(`SELECT * FROM dept WHERE loan ="${title}"`);
            const temp = [];
            for (let i = 0; i < results.rows.length; i++) {
                const tempId = results.rows.item(i).dept_id;
                const tempLoan = results.rows.item(i).loan;
                const tempPrincipal = results.rows.item(i).principal;
                const tempRepayment = results.rows.item(i).repayment;
                const tempRe_date = results.rows.item(i).repayment_date;
                const tempLoan_name = results.rows.item(i).loan_name;
                temp.push({ id: tempId, date: tempRe_date, loan: tempLoan, message: tempLoan_name, money: tempPrincipal, remained_money: tempRepayment });
            }
            resolve(temp);

        })
    })
}

const deleteDept = async (id) => {
    const db = await getDb();
    return new Promise((resolve, reject) => {
        db.transaction((tx) => {
            tx.executeSql(`DELETE FROM dept WHERE dept_id = ${id}`)
            resolve('삭제완료');
        })
    })
}


const updateDept = async (deptData, id) => {
    const db = await getDb();
    const { date, message, money, remained_money } = deptData
    console.log(date, message, id)
    return new Promise((resolve, reject) => {
        db.transaction((tx) => {
            tx.executeSql(`UPDATE dept set repayment_date =?, loan_name=?, principal=?, repayment=? where dept_id =${id};`,
                [date, message, money, remained_money],
                (error) => console.log(error))
            resolve('데이터 변경 완료');
        })
    })
};

const deptApi = {
    insertDept,
    selectLoan,
    deleteDept,
    updateDept,
}

export default deptApi;