postMoney.api.js 2.91 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

DEBUG(true);
enablePromise(true);

const insertMoney = async (moneyData) => {
    const db = await getDb();
    return new Promise((resolve, reject) => {
        db.transaction((tx) => {
11
            tx.executeSql('INSERT INTO Money (type_id, date, contents, price, assets_id, category_id, subcategory_id) VALUES (?,?,?,?,?,?,?);',
Soo Hyun Kim's avatar
Soo Hyun Kim committed
12
13
14
15
16
17
18
                moneyData,
                (error) => console.log(error))
            resolve('데이터 삽입 완료');
        })
    })
};

19
const selectCategories = async (type_id) => {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
20
21
22
    const db = await getDb();
    return new Promise((resolve, reject) => {
        db.transaction(async (tx) => {
23
            const [txn, results] = await tx.executeSql(`SELECT * FROM categories WHERE type_id=${type_id}`);
Soo Hyun Kim's avatar
Soo Hyun Kim committed
24
            const temp = [];
25
26
            temp.push({id: 1, value:'기타'});
            for (let i = 0; i < 3*(Math.ceil((results.rows.length+1)/3))-1; i++) {
27
28
29
30
31
32
33
                if (i<results.rows.length){
                    const tempId = results.rows.item(i).category_id;
                    const tempName = results.rows.item(i).category_name;
                    temp.push({ id: tempId, value: tempName });
                } else {
                    temp.push({ id: 0, value: '' });
                }
Soo Hyun Kim's avatar
Soo Hyun Kim committed
34
35
36
37
38
39
            }
            resolve(temp);
        })
    })
}

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const selectSubCategories = async () => {
    const db = await getDb();
    return new Promise((resolve, reject) => {
        db.transaction(async (tx) => {
            const [txn, results] = await tx.executeSql('SELECT * FROM subcategories');
            const temp = [];
            for (let i = 0; i < results.rows.length; i++) {
                const tempId = results.rows.item(i).subcategory_id;
                const tempName = results.rows.item(i).subcategory_name;
                const tempCatId = results.rows.item(i).category_id;
                temp.push({ id: tempId, value: tempName, foreign_id: tempCatId });
            }
            resolve(temp);
        })
    })
}

Soo Hyun Kim's avatar
Soo Hyun Kim committed
57
58
59
60
61
62
const selectAssetsType = async () => {
    const db = await getDb();
    return new Promise((resolve, reject) => {
        db.transaction(async (tx) => {
            const [txn, results] = await tx.executeSql('SELECT * FROM assets_type');
            const temp = [];
63
            for (let i = 0; i < 3*(Math.ceil((results.rows.length)/3)); i++) {
64
65
66
67
68
69
70
                if (i<results.rows.length){
                    const tempId = results.rows.item(i).assets_id;
                    const tempName = results.rows.item(i).assets_name;
                    temp.push({ id: tempId, value: tempName });
                } else {
                    temp.push({ id: 0, value: '' });
                }
Soo Hyun Kim's avatar
Soo Hyun Kim committed
71
72
73
74
75
76
77
78
79
80
            }
            resolve(temp);
        })
    })
}


const moneyApi = {
    insertMoney,
    selectCategories,
81
    selectSubCategories,
Soo Hyun Kim's avatar
Soo Hyun Kim committed
82
83
84
85
    selectAssetsType,
}

export default moneyApi;