calendarInfo.api.js 2.9 KB
Newer Older
Choi Ga Young's avatar
Choi Ga Young committed
1
2
3
4
5
6
import { DEBUG, enablePromise } from 'react-native-sqlite-storage';
import getDb from './moneyDB'

DEBUG(true);
enablePromise(true);

7
8
9
10
const getFullData = async ({ year, month }) => {
  const thisFirst = new Date(year, month, 1); //이번 달의 첫째 날
  const thisLast = new Date(year, month + 1, 0); //이번 달의 마지막 날

Choi Ga Young's avatar
Choi Ga Young committed
11
12
13
14
  const db = await getDb();
  return new Promise((res, rej) => {
    db.transaction(async (tx) => {
      console.log('월간 데이터');
15
      const [txn, results] = await tx.executeSql(`SELECT date, type_id, sum(price) as total from money where date BETWEEN "${String(thisFirst.toJSON()).split(/T/)[0]}" and "${String(thisLast.toJSON()).split(/T/)[0]}" group by date, type_id`);
Choi Ga Young's avatar
Choi Ga Young committed
16
      const temp = [];
17
18
19
20
21
22
23
24
      console.log('lelel', results.rows.length)
      if (results.rows.length != 0) {
        if (results.rows.item(0).type_id === 1) {
          temp.push({ date: results.rows.item(0).date, type: { 'input': results.rows.item(0).total } })
        }
        if (results.rows.item(0).type_id === 2) {
          temp.push({ date: results.rows.item(0).date, type: { 'output': results.rows.item(0).total } })
        }
Choi Ga Young's avatar
Choi Ga Young committed
25
      }
26

27
28
29
30
31
32
      for (let i = 1; i < results.rows.length; i++) {
        if (results.rows.item(i).date === results.rows.item(i - 1).date) {
          temp.pop()
          let { 1: input, 2: output } = {
            [results.rows.item(i).type_id]: results.rows.item(i).total,
            [results.rows.item(i - 1).type_id]: results.rows.item(i - 1).total,
Choi Ga Young's avatar
Choi Ga Young committed
33
          }
34
          temp.push({ date: results.rows.item(i).date, type: { 'input': input, 'output': output } })
Choi Ga Young's avatar
Choi Ga Young committed
35
        } else {
36
37
38
39
40
41
          if (results.rows.item(i).type_id === 1) {
            temp.push({ date: results.rows.item(i).date, type: { 'input': results.rows.item(i).total } })
          }
          if (results.rows.item(i).type_id === 2) {
            temp.push({ date: results.rows.item(i).date, type: { 'output': results.rows.item(i).total } })
          }
Choi Ga Young's avatar
Choi Ga Young committed
42
43
        }
      }
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
      console.log('결과 확인', temp)
      res(temp)
    })
  })
}

const detailData = async ({ findDate }) => {
  console.log('findData', findDate)
  const db = await getDb();
  return new Promise((res, rej) => {
    db.transaction(async (tx) => {
      console.log('월간 세부 데이터');
      const [txn, results] = await tx.executeSql(`SELECT money.type_id, category_name, contents, price FROM money inner JOIN categories on money.category_id = categories.category_id WHERE date="${findDate}"`);
      const temp = [];
      for (let i = 0; i < results.rows.length; i++) {
        console.log('item check', results.rows.item(i));
        temp.push({ id: i, category: results.rows.item(i).category_name, contents: results.rows.item(i).contents, price: results.rows.item(i).price, type: results.rows.item(i).type_id })
      }
      //console.log('temp', temp)
      res(temp);
Choi Ga Young's avatar
Choi Ga Young committed
64
65
66
67
68
    })
  })
}

const calApi = {
69
70
  getFullData,
  detailData
Choi Ga Young's avatar
Choi Ga Young committed
71
72
73
}

export default calApi;