18. Node 가계부 만들기 [ 가계부 기능 Service 구현 ] - 5

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

만든 Repository를 이용하여 서비스 코드 제작 : https://crispypotato.tistory.com/226

JwtPassport 생성 : https://crispypotato.tistory.com/219

JwtPassport 적용 : https://crispypotato.tistory.com/225

 

require 목록

const httpError = require('http-errors')
const { Send } = require('../../lib/lib')
const {
  WriteAccount,
  SelectAccount,
  ChangeAccount,
  RemoveAccount,
  CashCost,
  ShinhanCost,
  SamsungCost,
  HyundaiCost,
  WooriCost,
  LotteCost,
  KbCost,
  RevenueCost,
  UseCardList
} = require('./accounts.repository')

 

1. 가계부 생성

 

설명

 

*유저 인덱스 값은 만들었던 JwtPassport에서 인덱스 값을 req.user에 넣어준다.

- req.user에 넣어준 인덱스를 사용한다.

- 날짜는 params로 받고 나머지 데이터는 body로 받음

- WriteAccount 레포지토리에 데이터를 넣어 생성

 

const CreateAccount = async (req, res, next) => {
  try {
    const userIndex = req.user
    const { date } = req.params
    const { bigCategory, smallCategory, card, cost } = req.body
    const result = await WriteAccount({
      userIndex,
      date,
      bigCategory,
      smallCategory,
      card,
      cost
    })
    if (result) {
      Send(res, '')
    } else {
      throw httpError(500)
    }
  } catch (E) {
    next(E)
  }
}

 

2. 가계부 수정

 

설명

 

*유저 인덱스 값은 만들었던 JwtPassport에서 인덱스 값을 req.user에 넣어준다.

- req.user에 넣어준 인덱스를 사용한다.

- ChangeAccount 레포지토리에 데이터를 넣어 수정

- SelectAccount 레포지토리에서 해당 가계부가 있는지 해당유저가 소유했는 유효성 검사를 진행하고 코드가 실행된다.

 

const UpdateAccount = async (req, res, next) => {
  try {
    const userIndex = req.user
    const { index } = req.params
    const { bigCategory, smallCategory, card, cost, date } = req.body
    const check = await SelectAccount({ userIndex, index })
    if (check) {
      const result = await ChangeAccount({
        index,
        userIndex,
        date,
        bigCategory,
        smallCategory,
        card,
        cost
      })
      if (result) {
        Send(res, '')
      } else {
        throw httpError(500)
      }
    } else {
      throw httpError(404)
    }
  } catch (E) {
    next(E)
  }
}

 

3. 가계부 삭제

 

설명

 

*유저 인덱스 값은 만들었던 JwtPassport에서 인덱스 값을 req.user에 넣어준다.

- req.user에 넣어준 인덱스를 사용한다.

- RemoveAccount 레포지토리에 데이터를 넣어 삭제

- SelectAccount 레포지토리에서 해당 가계부가 있는지 해당유저가 소유했는 유효성 검사를 진행하고 코드가 실행된다.

 

const DeleteAccount = async (req, res, next) => {
  try {
    const userIndex = req.user
    const { index } = req.params
    const check = await SelectAccount({ userIndex, index })
    if (check) {
      const result = await RemoveAccount({
        index
      })
      if (result) {
        Send(res, '')
      } else {
        throw httpError(500)
      }
    } else {
      throw httpError(404)
    }
  } catch (E) {
    next(E)
  }
}

 

4. 가계부 금액확인

 

설명

 

*유저 인덱스 값은 만들었던 JwtPassport에서 인덱스 값을 req.user에 넣어준다.

- req.user에 넣어준 인덱스를 사용한다.

- 날짜는 params로 받고 [ 일, 월, 년 ] 형식에 맞추면 검색이된다.

- 해당 카드 별 합산을 전체 더하고 수익금액을 뺀가격을 return 한다.

 

const DayAccountFind = async (req, res, next) => {
  try {
    const userIndex = req.user
    const { date } = req.params

    const cash = await CashCost({ userIndex, date })
    const shinhan = await ShinhanCost({ userIndex, date })
    const samsung = await SamsungCost({ userIndex, date })
    const hyundai = await HyundaiCost({ userIndex, date })
    const woori = await WooriCost({ userIndex, date })
    const lotte = await LotteCost({ userIndex, date })
    const kb = await KbCost({ userIndex, date })
    const revenue = await RevenueCost({ userIndex, date })
    const total =
      cash + shinhan + samsung + hyundai + woori + lotte + kb - revenue
    const data = {
      cash,
      shinhan,
      samsung,
      hyundai,
      woori,
      lotte,
      kb,
      revenue,
      total
    }

    Send(res, data)
  } catch (E) {
    next(E)
  }
}

 

5. 특정 날짜 특정 카드 사용 목록

 

설명

 

*유저 인덱스 값은 만들었던 JwtPassport에서 인덱스 값을 req.user에 넣어준다.

- req.user에 넣어준 인덱스를 사용한다.

- 특정 카드, 날짜를 params에서 받아 사용한다.

- 카드 사용 목록을 return 한다.

 

const CardAccountFind = async (req, res, next) => {
  const userIndex = req.user
  const { card, date } = req.params
  let total
  const useList = await UseCardList({ userIndex, card, date })
  if (card === 'cash') {
    total = await CashCost({ userIndex, date })
  } else if (card === 'shinhan') {
    total = await ShinhanCost({ userIndex, date })
  } else if (card === 'samsung') {
    total = await SamsungCost({ userIndex, date })
  } else if (card === 'hyundai') {
    total = await HyundaiCost({ userIndex, date })
  } else if (card === 'woori') {
    total = await WooriCost({ userIndex, date })
  } else if (card === 'lotte') {
    total = await LotteCost({ userIndex, date })
  } else if (card === 'kb') {
    total = await KbCost({ userIndex, date })
  }
  Send(res, { useList, total })
}

 

6. accounts.service.js 최종 코드

const httpError = require('http-errors')
const { Send } = require('../../lib/lib')
const {
  WriteAccount,
  SelectAccount,
  ChangeAccount,
  RemoveAccount,
  CashCost,
  ShinhanCost,
  SamsungCost,
  HyundaiCost,
  WooriCost,
  LotteCost,
  KbCost,
  RevenueCost,
  UseCardList
} = require('./accounts.repository')

// 가계부 생성
const CreateAccount = async (req, res, next) => {
  try {
    const userIndex = req.user
    const { date } = req.params
    const { bigCategory, smallCategory, card, cost } = req.body
    const result = await WriteAccount({
      userIndex,
      date,
      bigCategory,
      smallCategory,
      card,
      cost
    })
    if (result) {
      Send(res, '')
    } else {
      throw httpError(500)
    }
  } catch (E) {
    next(E)
  }
}

// 가계부 수정
const UpdateAccount = async (req, res, next) => {
  try {
    const userIndex = req.user
    const { index } = req.params
    const { bigCategory, smallCategory, card, cost, date } = req.body
    const check = await SelectAccount({ userIndex, index })
    if (check) {
      const result = await ChangeAccount({
        index,
        userIndex,
        date,
        bigCategory,
        smallCategory,
        card,
        cost
      })
      if (result) {
        Send(res, '')
      } else {
        throw httpError(500)
      }
    } else {
      throw httpError(404)
    }
  } catch (E) {
    next(E)
  }
}

// 가계부 삭제
const DeleteAccount = async (req, res, next) => {
  try {
    const userIndex = req.user
    const { index } = req.params
    const check = await SelectAccount({ userIndex, index })
    if (check) {
      const result = await RemoveAccount({
        index
      })
      if (result) {
        Send(res, '')
      } else {
        throw httpError(500)
      }
    } else {
      throw httpError(404)
    }
  } catch (E) {
    next(E)
  }
}

// 가계부 금액확인
const DayAccountFind = async (req, res, next) => {
  try {
    const userIndex = req.user
    const { date } = req.params

    const cash = await CashCost({ userIndex, date })
    const shinhan = await ShinhanCost({ userIndex, date })
    const samsung = await SamsungCost({ userIndex, date })
    const hyundai = await HyundaiCost({ userIndex, date })
    const woori = await WooriCost({ userIndex, date })
    const lotte = await LotteCost({ userIndex, date })
    const kb = await KbCost({ userIndex, date })
    const revenue = await RevenueCost({ userIndex, date })
    const total =
      cash + shinhan + samsung + hyundai + woori + lotte + kb - revenue
    const data = {
      cash,
      shinhan,
      samsung,
      hyundai,
      woori,
      lotte,
      kb,
      revenue,
      total
    }

    Send(res, data)
  } catch (E) {
    next(E)
  }
}

const CardAccountFind = async (req, res, next) => {
  const userIndex = req.user
  const { card, date } = req.params
  let total
  const useList = await UseCardList({ userIndex, card, date })
  if (card === 'cash') {
    total = await CashCost({ userIndex, date })
  } else if (card === 'shinhan') {
    total = await ShinhanCost({ userIndex, date })
  } else if (card === 'samsung') {
    total = await SamsungCost({ userIndex, date })
  } else if (card === 'hyundai') {
    total = await HyundaiCost({ userIndex, date })
  } else if (card === 'woori') {
    total = await WooriCost({ userIndex, date })
  } else if (card === 'lotte') {
    total = await LotteCost({ userIndex, date })
  } else if (card === 'kb') {
    total = await KbCost({ userIndex, date })
  }
  Send(res, { useList, total })
}

module.exports = {
  CreateAccount,
  UpdateAccount,
  DeleteAccount,
  DayAccountFind,
  CardAccountFind
}
728x90
반응형