deptPage.api.js 2.46 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, repayment } = 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, repayment], 
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
                (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;
Choi Ga Young's avatar
Choi Ga Young committed
34
                temp.push({ id: tempId, date: tempRe_date, loan: tempLoan, message: tempLoan_name, money: tempPrincipal, repayment: tempRepayment });
YoonDongMin's avatar
YDm    
YoonDongMin committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
            }
            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();
Choi Ga Young's avatar
Choi Ga Young committed
55
    const { date, message, money, repayment } = deptData
YoonDongMin's avatar
YDm    
YoonDongMin committed
56
57
58
59
    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};`,
Choi Ga Young's avatar
Choi Ga Young committed
60
                [date, message, money, repayment],
YoonDongMin's avatar
YDm    
YoonDongMin committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
                (error) => console.log(error))
            resolve('데이터 변경 완료');
        })
    })
};

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

export default deptApi;