editOption.api.js 5.52 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import { DEBUG, enablePromise } from 'react-native-sqlite-storage';
import getDb from './moneyDB'

DEBUG(true);
enablePromise(true);

const CAT_LIMIT_ID = 14
const ASSET_LIMIT_ID = 10

const selectCategories = async (type_id) => {
    const db = await getDb();
    return new Promise((resolve, reject) => {
        db.transaction(async (tx) => {
            console.log("카테고리 부르기");
15
16
17
18
19
20
21
22
23
24
25
            const [txn, results] = await tx.executeSql(
                `select cat.category_id, category_name, subcat_id, subcat_name from categories as cat 
                left join ( 
                    SELECT category_id, 
                        group_concat(subcategory_id, '|') as subcat_id, 
                        group_concat(subcategory_name, '|') as subcat_name
                    from subcategories group by category_id
                ) as subcat
                on cat.category_id=subcat.category_id 
                where cat.type_id='${type_id}'`
            );
26
27
28
29
30
            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;
31
32
33
34
35
36
37
38
                let deletable = (tempId < CAT_LIMIT_ID) ? false : true;
                const tempSubOptions = [];
                if (results.rows.item(i).subcat_id) {
                    const tempSubId = results.rows.item(i).subcat_id.split('|');
                    const tempSubValue = results.rows.item(i).subcat_name.split('|');
                    for (let i = 0; i < tempSubId.length; i++) {
                        tempSubOptions.push({ id: tempSubId[i], value: tempSubValue[i], foreign_id: tempId });
                    }
39
                }
40
                temp.push({ id: tempId, value: tempName, deletable: deletable, subOptions: tempSubOptions });
41
42
43
            }
            console.log(temp)
            resolve(temp);
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
            // console.log("카테고리 부르기");
            // const [txn, results] = await tx.executeSql(`SELECT * FROM categories WHERE type_id=${type_id}`);
            // 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;
            //     if (tempId < CAT_LIMIT_ID) {
            //         temp.push({ id: tempId, value: tempName, deletable: false });
            //     } else {
            //         temp.push({ id: tempId, value: tempName, deletable: true });
            //     }
            // }
            // console.log(temp)
            // resolve(temp);
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
        })
    })
}

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;
                if (tempId < ASSET_LIMIT_ID) {
                    temp.push({ id: tempId, value: tempName, deletable: false });
                } else {
                    temp.push({ id: tempId, value: tempName, deletable: true });
                }
            }
            console.log(temp)
            resolve(temp);
        })
    })
}

const deleteOption = async (table, data) => {
    const db = await getDb();
    const { id, name } = data
    return new Promise((resolve, reject) => {
        db.transaction((tx) => {
            console.log("데이터 삭제하기");
            tx.executeSql(`DELETE FROM ${table} WHERE ${name}_id = ${id};`)
            resolve(`${name} 데이터 삭제 완료`);
        })
    })
}

const updateOption = async (table, data) => {
    const db = await getDb();
    const { id, name, value } = data
    console.log(table, id, value)
    return new Promise((resolve, reject) => {
        db.transaction((tx) => {
            console.log("데이터 수정하기");
            tx.executeSql(`UPDATE ${table} set ${name}_name =? where ${name}_id =${id};`,
                [value],
                (error) => console.log(error))
            resolve(`${name} 데이터 변경 완료`);
        })
    })
};

const addOption = async (table, data) => {
    const db = await getDb();
    const { name, value, foreign_name = null, foreign_id } = data;
    let insertQeury = '';
    let queryData = [];
    if (foreign_name === null) {
        insertQeury = `INSERT INTO ${table} (${name}_name) VALUES (?);`
        queryData = [value]
    } else {
        insertQeury = `INSERT INTO ${table} (${name}_name, ${foreign_name}_id) VALUES (?,?);`
        queryData = [value, foreign_id]
    }
    // console.log(insertQuery, queryData)
    return new Promise((resolve, reject) => {
        db.transaction((tx) => {
            console.log("데이터 삽입하기");
            tx.executeSql(insertQeury,
                queryData,
                (error) => console.log(error))
            resolve('데이터 삽입 카테고리 카테고리 완료');
        })
    })
};

const editApi = {
    selectCategories,
    selectAssetsType,
    deleteOption,
    updateOption,
    addOption,
}

export default editApi;