본문 바로가기

내일 배움 캠프/kiosk

kiosk 3일차 7/27 상품 관리 (2)

728x90

상품 조회 API

1. routes

import { Router } from 'express';
import ItemsController from '../controllers/itmes.controller.js';

const router = Router();

const itemsController = new ItemsController();

router.post('/items', itemsController.makeItem);
router.get('/items', itemsController.getItemList);
router.delete('/items/:id', itemsController.removeItem);
router.delete('/response/items/:id', itemsController.answerRemoveItem);
router.patch('/items/:id', itemsController.editItem);

export default router;

2. controller

import ItemsService from '../services/items.service.js';

class ItemsController {
  itemsService = new ItemsService();

  makeItem = async (req, res) => {
    const { name, price, type } = req.body;

    const { status, message } = await this.itemsService.makeItem(
      name,
      price,
      type,
    );
    return res.status(status).json({ message });
  };
  getItemList = async (req, res) => {
    const { category } = req.query;
    const { status, message, list } = await this.itemsService.getItemList(
      category,
    );
    return res.status(status).json({ message, list });
  };
  removeItem = async (req, res) => {
    const { id } = req.params;
    const { status, message } = await this.itemsService.removeItem(id);
    return res.status(status).json({ message });
  };
  answerRemoveItem = async (req, res) => {
    const { id } = req.params;
    const { answer } = req.body;
    const { status, message } = await this.itemsService.answerRemoveItem(
      id,
      answer,
    );
    return res.status(status).json({ message });
  };
  editItem = async (req, res) => {
    const { id } = req.params;
    const { name, price } = req.body;
    const { status, message } = await this.itemsService.editItem(
      id,
      name,
      price,
    );
    return res.status(status).json({ message });
  };
}

export default ItemsController;

3. service

import Messages from './message.js';
import ItemsRepository from '../repositories/items.repository.js';
const noname = new Messages('이름');
const noprice = new Messages('가격');
class ItemsService {
  itemsRepository = new ItemsRepository();
  makeItem = async (name, price, type) => {
    const messages = new Messages('상품 추가');

    try {
      if (!name.length) {
        return noname.nosubject();
      } else if (!price) {
        return noprice.nosubject();
      } else if (!['COFFEE', 'JUICE', 'FOOD'].includes(type)) {
        return {
          status: 400,
          message: '알맞은 타입을 지정해 주세요.',
        };
      }

      const item = await this.itemsRepository.makeItem(name, price, type);
      if (item.name) {
        return messages.status200();
      } else {
        return messages.status400();
      }
    } catch (err) {
      return messages.status400();
    }
  };
  getItemList = async category => {
    const messages = new Messages('상품 조회');
    try {
      if (category == 'all') {
        const allItemList = await this.itemsRepository.getAllItemList();
        return {
          status: 200,
          message: '전체 상품이 조회되었습니다.',
          list: allItemList,
        };
      } else {
        const itemList = await this.itemsRepository.getItemList(category);
        return {
          status: 200,
          message: `${category} 타입의 상품이 조회되었습니다.`,
          list: itemList,
        };
      }
    } catch (err) {
      return messages.status400;
    }
  };
  removeItem = async id => {
    const messages = new Messages('상품 삭제');
    try {
      const checkItem = await this.itemsRepository.checkamount(id);
      if (checkItem.amount > 0) {
        return {
          status: 200,
          message: '현재 수량이 남아있습니다. 삭제하시겠습니까?',
        };
      } else if (checkItem.amount == 0) {
        const removeItem = await this.itemsRepository.removeItem(id);
        if (removeItem) {
          return messages.status200();
        }
      }
    } catch (err) {
      return messages.status400();
    }
  };
  answerRemoveItem = async (id, answer) => {
    const messages = new Messages('상품 삭제');
    try {
      if (answer == '예') {
        const removeItem = await this.itemsRepository.removeItem(id);
        if (removeItem) {
          return messages.status200();
        }
      } else {
        return messages.status400();
      }
    } catch (err) {
      return messages.status400();
    }
  };
  editItem = async (id, name, price) => {
    const messages = new Messages('상품 수정');

    try {
      if (!name.length) {
        return noname.nosubject();
      } else if (!price) {
        return noprice.nosubject();
      } else if (price < 0) {
        return {
          status: 400,
          message: '알맞은 가격을 입력해주세요.',
        };
      }

      const item = await this.itemsRepository.editItem(id, name, price);
      if (item) {
        return messages.status200();
      } else {
        return messages.status400();
      }
    } catch (err) {
      return messages.status400();
    }
  };
}
export default ItemsService;

4. repository

import Items from '../db/models/items.js';

class ItemsRepository {
  makeItem = async (name, price, type) => {
    const item = await Items.create({
      name,
      price,
      type,
    });

    return item;
  };

  getAllItemList = async () => {
    const allItemList = await Items.findAll();

    return allItemList;
  };

  getItemList = async category => {
    const itemList = await Items.findAll({
      where: { type: category },
    });

    return itemList;
  };

  checkamount = async id => {
    const checkItem = await Items.findByPk(id);
    return checkItem;
  };

  removeItem = async id => {
    const removeItem = await Items.destroy({ where: { id } });
    return removeItem;
  };

  editItem = async (id, name, price) => {
    const editItem = await Items.update({ name, price }, { where: { id } });
    return editItem;
  };
}

export default ItemsRepository;

'내일 배움 캠프 > kiosk' 카테고리의 다른 글

kiosk 6일차 8/1 주문 관리 (2)  (0) 2023.07.28
kiosk 5일차 7/31 주문 관리  (0) 2023.07.28
kiosk 4일차 7/28 상품 발주  (0) 2023.07.27
kiosk 2일차 7/26 상품관리  (0) 2023.07.26
kiosk 1일차 7/25 프로젝트 세팅  (0) 2023.07.25