17. Node 가계부 만들기 [ 가계부 기능 Repository 구현 ] - 4

2022. 5. 29. 15:03프로젝트/가계부 - 제작 JavaScript

카드별 금액 확인 기능에는 일, 월, 년 별로 확인하는 기능이 같은 코드로도 가능해서 Sequlize Op 연산자를 이용해서 날짜가 들어가면 합산 금액이 나오게 사용하도록 만들었다.

 

날짜 데이터 형식은 YYYY-MM-DD 형식이다

 

require 목록

- 모델

- sequlize

- Op

const Accounts = require('../../models/accountlist')
const sequelize = require('sequelize')
const Op = sequelize.Op

 

1. 가계부 생성

 

설명

 

- 유저 인덱스, 사용 날짜, 큰 카테고리, 작은 카테고리, 사용 카드 종류, 사용 금액을 받아 생성

const WriteAccount = async (data) => {
  try {
    const { userIndex, date, bigCategory, smallCategory, card, cost } = data
    await Accounts.create({
      userIndex,
      date,
      bigCategory,
      smallCategory,
      card,
      cost
    })
    return true
  } catch (E) {
    return false
  }
}

 

2. 가계부 수정

 

설명

 

- 받은 가계부의 인덱스와 유저 인덱스를 기준으로 가계부 수정

const ChangeAccount = async (data) => {
  try {
    const { index, userIndex, date, bigCategory, smallCategory, card, cost } =
      data
    await Accounts.update(
      {
        bigCategory,
        smallCategory,
        card,
        cost,
        date
      },
      {
        where: { userIndex, index }
      }
    )
    return true
  } catch (E) {
    return false
  }
}

 

3. 가계부 삭제

 

설명

 

- 받은 가계부의 인덱스와 유저 인덱스를 기준으로 삭제

const RemoveAccount = async (data) => {
  try {
    const { index } = data
    await Accounts.destroy({
      where: { index }
    })
    return true
  } catch (E) {
    return false
  }
}

 

4. 인덱스 기준 가계부 검색

 

설명

 

- 받은 가계부의 인덱스와 유저 인덱스를 기준으로 검색

- 데이터 수정/삭제시 유효성 검사를 위해 요청 함

const SelectAccount = async (data) => {
  const { index, userIndex } = data
  const account = await Accounts.findOne({
    where: {
      index,
      userIndex
    }
  })
  if (account) {
    return true
  } else {
    return false
  }
}

 

5. 카드 별 금액 확인

 

설명

 

- Op 연산자를 이용해 일, 월, 년 기준으로 검색하여 합산 금액를 낸다. 

* 데이터가 없을 경우 NaN이 발생하여 if 문으로 0으로 만들어주는 코드를 추가함

 

- 일 검색  " YYYY-MM-DD "

- 월 검색 " YYYY-MM "

- 년 검색 " YYYY "

 

- 카드 종류 설명

[ 지출 ]

- Cash : 현금 

- Shinhan : 신한 

- Samsung : 삼성

- Hyundai : 현대

- Woori : 우리

- Lotte : 롯데

- KB : KB

 

[ 수익 ]

- Revenue : 수익금액

 

const CashCost = async (data) => {
  const { userIndex, date } = data
  const cash = await Accounts.sum('cost', {
    where: {
      userIndex,
      card: 'cash',
      [Op.or]: [
        {
          date: {
            [Op.like]: date + '%'
          }
        }
      ]
    }
  })
  let result = 0
  if (isNaN(cash) === false) {
    result = cash
  }
  return result
}

 

6. 특정 카드의 특정 날짜에 사용한 금액 확인

 

설명

 

- 특정 날짜의 특정 카드를 어떻게 썼는지 확인

- 합산을 내지않고 목록 출력

const UseCardList = async (data) => {
  const { userIndex, card, date } = data
  const total = await Accounts.findAll({
    where: {
      userIndex,
      card,
      [Op.or]: [
        {
          date: {
            [Op.like]: date + '%'
          }
        }
      ]
    }
  })
  return total
}

 

7. accounts.repository.js 전체 코드

const Accounts = require('../../models/accountlist')
const sequelize = require('sequelize')
const Op = sequelize.Op

// date 형식은 YYYY-MM-DD
// 가계부 작성
const WriteAccount = async (data) => {
  try {
    const { userIndex, date, bigCategory, smallCategory, card, cost } = data
    await Accounts.create({
      userIndex,
      date,
      bigCategory,
      smallCategory,
      card,
      cost
    })
    return true
  } catch (E) {
    return false
  }
}

// 특정 가계부 확인
const SelectAccount = async (data) => {
  const { index, userIndex } = data
  const account = await Accounts.findOne({
    where: {
      index,
      userIndex
    }
  })
  if (account) {
    return true
  } else {
    return false
  }
}

// 가계부 수정
const ChangeAccount = async (data) => {
  try {
    const { index, userIndex, date, bigCategory, smallCategory, card, cost } =
      data
    await Accounts.update(
      {
        bigCategory,
        smallCategory,
        card,
        cost,
        date
      },
      {
        where: { userIndex, index }
      }
    )
    return true
  } catch (E) {
    return false
  }
}

// 가계부 삭제
const RemoveAccount = async (data) => {
  try {
    const { index } = data
    await Accounts.destroy({
      where: { index }
    })
    return true
  } catch (E) {
    return false
  }
}

// 현금 사용 금액
const CashCost = async (data) => {
  const { userIndex, date } = data
  const cash = await Accounts.sum('cost', {
    where: {
      userIndex,
      card: 'cash',
      [Op.or]: [
        {
          date: {
            [Op.like]: date + '%'
          }
        }
      ]
    }
  })
  let result = 0
  if (isNaN(cash) === false) {
    result = cash
  }
  return result
}

// 신한 카드 사용 금액
const ShinhanCost = async (data) => {
  const { userIndex, date } = data
  const cash = await Accounts.sum('cost', {
    where: {
      userIndex,
      card: 'shinhan',
      [Op.or]: [
        {
          date: {
            [Op.like]: date + '%'
          }
        }
      ]
    }
  })
  let result = 0
  if (isNaN(cash) === false) {
    result = cash
  }
  return result
}

// 삼성 카드 사용금액
const SamsungCost = async (data) => {
  const { userIndex, date } = data
  const cash = await Accounts.sum('cost', {
    where: {
      userIndex,
      card: 'samsung',
      [Op.or]: [
        {
          date: {
            [Op.like]: date + '%'
          }
        }
      ]
    }
  })
  let result = 0
  if (isNaN(cash) === false) {
    result = cash
  }
  return result
}

// 현대 카드 사용금액
const HyundaiCost = async (data) => {
  const { userIndex, date } = data
  const cash = await Accounts.sum('cost', {
    where: {
      userIndex,
      card: 'hyundai',
      [Op.or]: [
        {
          date: {
            [Op.like]: date + '%'
          }
        }
      ]
    }
  })
  let result = 0
  if (isNaN(cash) === false) {
    result = cash
  }
  return result
}

// 우리 카드 사용금액
const WooriCost = async (data) => {
  const { userIndex, date } = data
  const cash = await Accounts.sum('cost', {
    where: {
      userIndex,
      card: 'woori',
      [Op.or]: [
        {
          date: {
            [Op.like]: date + '%'
          }
        }
      ]
    }
  })
  let result = 0
  if (isNaN(cash) === false) {
    result = cash
  }
  return result
}

// 롯데 카드 사용금액
const LotteCost = async (data) => {
  const { userIndex, date } = data
  const cash = await Accounts.sum('cost', {
    where: {
      userIndex,
      card: 'lotte',
      [Op.or]: [
        {
          date: {
            [Op.like]: date + '%'
          }
        }
      ]
    }
  })
  let result = 0
  if (isNaN(cash) === false) {
    result = cash
  }
  return result
}

// kb 카드 사용금액
const KbCost = async (data) => {
  const { userIndex, date } = data
  const cash = await Accounts.sum('cost', {
    where: {
      userIndex,
      card: 'kb',
      [Op.or]: [
        {
          date: {
            [Op.like]: date + '%'
          }
        }
      ]
    }
  })
  let result = 0
  if (isNaN(cash) === false) {
    result = cash
  }
  return result
}

// 수익 금액
const RevenueCost = async (data) => {
  const { userIndex, date } = data
  const cash = await Accounts.sum('cost', {
    where: {
      userIndex,
      card: 'revenue',
      [Op.or]: [
        {
          date: {
            [Op.like]: date + '%'
          }
        }
      ]
    }
  })
  let result = 0
  if (isNaN(cash) === false) {
    result = cash
  }
  return result
}

// 날짜 별 사용 카드별 상세 목록 확인
const UseCardList = async (data) => {
  const { userIndex, card, date } = data
  const total = await Accounts.findAll({
    where: {
      userIndex,
      card,
      [Op.or]: [
        {
          date: {
            [Op.like]: date + '%'
          }
        }
      ]
    }
  })
  return total
}

module.exports = {
  WriteAccount,
  SelectAccount,
  ChangeAccount,
  RemoveAccount,
  CashCost,
  ShinhanCost,
  SamsungCost,
  HyundaiCost,
  WooriCost,
  LotteCost,
  KbCost,
  RevenueCost,
  UseCardList
}

 

728x90
반응형