postMoney.api.js 2.12 KB
Newer Older
Soo Hyun Kim's avatar
Soo Hyun Kim committed
1
import { DEBUG, enablePromise } from 'react-native-sqlite-storage';
Soo Hyun Kim's avatar
Soo Hyun Kim committed
2
import getDb from './moneyDB'
Soo Hyun Kim's avatar
Soo Hyun Kim committed
3
4
5
6
7
8
9
10
11
12
13
14
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

DEBUG(true);
enablePromise(true);

const insertMoney = async (moneyData) => {
    const db = await getDb();
    return new Promise((resolve, reject) => {
        db.transaction((tx) => {
            console.log("데이터 삽입하기");
            tx.executeSql('INSERT INTO Money (type, date, contents, price, asset_type, category, subcategory) VALUES (?,?,?,?,?,?,?);',
                moneyData,
                (error) => console.log(error))
            resolve('데이터 삽입 완료');
        })
    })
};

const selectCategories = async () => {
    const db = await getDb();
    return new Promise((resolve, reject) => {
        db.transaction(async (tx) => {
            console.log("카테고리 부르기");
            const [txn, results] = await tx.executeSql('SELECT * FROM categories');
            console.log('item length', results.rows.length);
            const temp = [];
            for (let i = 0; i < results.rows.length; i++) {
                const tempId = results.rows.item(i).category_id;
                const tempName = results.rows.item(i).category_name;
                temp.push({ id: tempId, value: tempName });
            }
            console.log(temp)
            resolve(temp);
        })
    })
}

const selectAssetsType = async () => {
    const db = await getDb();
    return new Promise((resolve, reject) => {
        db.transaction(async (tx) => {
            console.log("자산 유형 부르기");
            const [txn, results] = await tx.executeSql('SELECT * FROM assets_type');
            console.log('item length', results.rows.length);
            const temp = [];
            for (let i = 0; i < results.rows.length; i++) {
                const tempId = results.rows.item(i).assets_id;
                const tempName = results.rows.item(i).assets_name;
                temp.push({ id: tempId, value: tempName });
            }
            console.log(temp)
            resolve(temp);
        })
    })
}


const moneyApi = {
    insertMoney,
    selectCategories,
    selectAssetsType,
}

export default moneyApi;