๊ฐœ๋ฐœ์ผ์ง€-34 [๐Ÿ›’์žฅ๋ฐ”๊ตฌ๋‹ˆ ๋„๋ฉ”์ธ: ์žฅ๋ฐ”๊ตฌ๋‹ˆ ์ฃผ๋ฌธ]

๊น€์„ฑ์ธยท2023๋…„ 9์›” 25์ผ
0

์žฅ๋ฐ”๊ตฌ๋‹ˆ ๊ธฐ๋Šฅ ๊ด€๋ จ ํฌ์ŠคํŒ…


์†Œ๊ธˆ๋นต์„ ๋‹ค์‹œ ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ๋‹ด๊ณ  ์ฃผ๋ฌธ ํ•  ์ค€๋น„๋ฅผ ํ•ด๋ณด๊ฒ ๋‹ค.

6.[GET] /jat/app/basket/order

์ฃผ๋ฌธ ์ค€๋น„ ํ™”๋ฉด์—์„œ ๋ณผ ์ •๋ณด๋“ค

  • ์ฃผ๋ฌธ ํ™”๋ฉด์—์„œ ์ฃผ๋ฌธ ์ •๋ณด๋ฅผ ์‚ฌ์šฉ์ž์—๊ฒŒ ์‚ฌ์ „์œผ๋กœ ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•œ ๋กœ์ง์„ ๊ตฌํ˜„ํ•˜์˜€๋‹ค.
  • ์ฃผ๋ฌธ ํ™”๋ฉด์— ๋ณด์ด๋Š” ์ •๋ณด๋Š” : ์ฃผ๋ฌธํ•  ๊ฐ€๊ฒŒ, ๊ฐ€๊ฒŒ์˜ ์ฃผ์†Œ, ๋‚ด ๋ฒˆํ˜ธ, ์ฃผ๋ฌธ ์ด ๊ธˆ์•ก, + ๋ณธ์ธ์˜ ๊ฒฐ์ œ์ •๋ณด ๋“ฑ์„ ๋„์›Œ ์ค„ ์ˆ˜์žˆ๋‹ค.
  • ๋‚˜๋จธ์ง€ ํ”ฝ์—…์˜ˆ์ •์‹œ๊ฐ„, ์š”์ฒญ์‚ฌํ•ญ, ๊ฒฐ์ œ์‚ฌํ•ญ ๋“ฑ์€ ๊ตฌ๋งค์ž๊ฐ€ ์ง์ ‘ ์„ ํƒํ•˜๋Š” ๊ธฐ์ค€์œผ๋กœ ์ •ํ•˜์˜€๋‹ค.

  • ์‘๋‹ต ๋ฐ”๋””
    • ์ฃผ๋ฌธ์˜ ์ฃผ์ฒด๋ฅผ ๊ฐ€๊ฒŒ์— ์—ฐ๊ด€์‹œํ‚ค๊ธฐ ์œ„ํ•˜์—ฌ storeIdx๋„ ๊ฐ™์ด ๋ฐ˜ํ™˜ํ•ด์ฃผ์—ˆ๋‹ค.
  • ์ปจํŠธ๋กค๋Ÿฌ
  • ์„œ๋น„์Šค
    • ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ๋‹ด๊ธด ๋ฉ”๋‰ด๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ฃผ๋ฌธ ์ •๋ณด๋ฅผ ์กฐํšŒ
    public BasketOrderRes getBasketOrder(int userIdx) throws BaseException{
        try{
            BasketOrderRes orderRes = basketDao.getBasketOrder(userIdx);
            return orderRes;
        }catch (IncorrectResultSizeDataAccessException error) { // ์ฟผ๋ฆฌ๋ฌธ์— ํ•ด๋‹นํ•˜๋Š” ๊ฒฐ๊ณผ๊ฐ€ ์—†๊ฑฐ๋‚˜ 2๊ฐœ ์ด์ƒ์ผ ๋•Œ
            throw new BaseException(NO_BASKET_ITEMS);// ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ๋‹ด๊ธด ํ•ญ๋ชฉ์—†์ด ์ฃผ๋ฌธ ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
        }catch(Exception e){
            throw new BaseException(DATABASE_ERROR);
        }
    }
  • Dao
// ๊ฐ€๊ฒŒ Idx, ๊ฐ€๊ฒŒ ์ด๋ฆ„, ๊ฐ€๊ฒŒ ์ฃผ์†Œ, ํฐ ๋ฒˆํ˜ธ, ์ฃผ๋ฌธ ๊ฐ€๊ฒฉ ์กฐํšŒ
    public BasketOrderRes getBasketOrder(int userIdx) {
        String query = "SELECT\n" +
                "    S.storeIdx,\n" +
                "    S.store_name,\n" +
                "    S.store_address,\n" +
                "    C.phone,\n" +
                "    SUM(B.cnt * TM.price) as orderPrice\n" +
                "FROM Basket B\n" +
                "LEFT JOIN TodayMenu TM on B.todaymenuIdx = TM.todaymenuIdx\n" +
                "LEFT JOIN Stores S on B.storeIdx = S.storeIdx\n" +
                "LEFT JOIN Customers C on B.customerIdx = C.customerIdx\n" +
                "WHERE B.customerIdx = ?\n" +
                "AND B.status != 'D'";

        return this.jdbcTemplate.queryForObject(query,
                (rs, rowNum) -> new BasketOrderRes(
                        rs.getInt("storeIdx"),
                        rs.getString("store_name"),
                        rs.getString("store_address"),
                        rs.getString("phone"),
                        rs.getInt("orderPrice")
                ), userIdx);
    }

ํ…Œ์ŠคํŠธ


7. [POST] /jat/app/basket/order

๊ตฌ๋งค์ž์˜ ์ฃผ๋ฌธํ•˜๊ธฐ

  • ์‚ฌ์šฉ์ž๊ฐ€ ์ฃผ๋ฌธ ์ •๋ณด๋ฅผ ๋ชจ๋‘ ์ž‘์„ฑํ•˜๊ณ  ์ฃผ๋ฌธ์„ ํ•˜์—ฌ ๊ฒฐ์ œ ์ •๋ณด๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๊ธฐ ์œ„ํ•œ ๋‹จ๊ณ„
  • ํ•ด๋‹น ์ฃผ๋ฌธ ์š”์ฒญ์„ ๋ฐœ์ƒ ์‹œ ๊ตฌ๋งค์ž์˜ ์ฃผ๋ฌธํ˜„ํ™ฉํ™”๋ฉด์— ์ฃผ๋ฌธ ์š”์ฒญ์ด ์ถ”๊ฐ€๊ฐ€ ๋œ๋‹ค.
  • ํŒ๋งค์ž/๊ตฌ๋งค์ž ๋ชจ๋‘ ์ฃผ๋ฌธ์— ๋Œ€ํ•ด์„œ ๊ฐ™์€ ํ…Œ์ด๋ธ”์„ ๋ฐ”๋ผ๋ณด๋„๋ก ์„ค๊ณ„ํ•˜์˜€๋‹ค.
  • ์š”์ฒญ ๋ฐ”๋””
    • ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ์—†๋Š”๋ฐ, ์ด๋Š” ์žฅ๋ฐ”๊ตฌ๋‹ˆ ํ…Œ์ด๋ธ”์— ์œ ์ €์˜ Idx๋กœ ์ •๋ณด๊ฐ€ ์œ ์ง€๋˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ
  • ์ปจํŠธ๋กค๋Ÿฌ
  • ์„œ๋น„์Šค
    @Transactional(rollbackFor = BaseException.class)
    public OrderDoneRes postBasketOrder(int userIdx, OrderDoneReq orderReq) throws BaseException {
        // 1) ์ฃผ๋ฌธํ•˜๊ธฐ ์ „ ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ๋‹ด๊ธด ํ’ˆ๋ชฉ ๋ฆฌ์ŠคํŠธ ๊ฐ€์ ธ์˜ค๊ธฐ
        int storeIdx = orderReq.getStoreIdx();
        List<BasketOrderItem> basketItems;
        try {
            basketItems = basketDao.getBasketOrderItems(userIdx);
        } catch(Exception e){
            throw new BaseException(DATABASE_ERROR);
        }

        // 2) ํ•ด๋‹น ์žฅ๋ฐ”๊ตฌ๋‹ˆ ๋ชฉ๋ก๋“ค์ด ๊ฐ€๊ฒŒ์— ์ถฉ๋ถ„ํžˆ ๋‚จ์•„์žˆ๋Š”์ง€ ํ™•์ธ์ธ
        try{
            for (BasketOrderItem bItem : basketItems){
                BasketTodayMenu bTodayMenu = basketDao.checkItemRemain(storeIdx, bItem.getTodayMenuIdx(), bItem.getCnt());
                // ์กฐํšŒํ•œ ๊ฐœ์ˆ˜๊ฐ€ ๊ฐ€๊ฒŒ์— ๋‚จ์•„์žˆ๋Š” ๊ฐœ์ˆ˜๋ณด๋‹ค ํฌ๋‹ค๋ฉด..?
                if(bTodayMenu.getRemain() < bItem.getCnt() || bTodayMenu.getTodayMenuIdx() != bItem.getTodayMenuIdx()){
                    throw new IncorrectResultSizeDataAccessException(0);
                }
            }
        }catch (IncorrectResultSizeDataAccessException error) { // ์ฟผ๋ฆฌ๋ฌธ์— ํ•ด๋‹นํ•˜๋Š” ๊ฒฐ๊ณผ๊ฐ€ ์—†๊ฑฐ๋‚˜ 2๊ฐœ ์ด์ƒ์ผ ๋•Œ
            throw new BaseException(STORE_TODAY_MENU_LACK); // ๊ฐ€๊ฒŒ์˜ ๋–จ์ด๋ฉ”๋‰ด ๊ฐœ์ˆ˜๊ฐ€ ๋ถ€์กฑํ•˜์—ฌ ์ฃผ๋ฌธ์ด ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
        }catch(Exception e){
            throw new BaseException(DATABASE_ERROR);
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd ");
        String pickupDate = sdf.format(new Date());
        orderReq.setPickupTime(pickupDate + orderReq.getPickupTime());

        try{
            // ์ฃผ๋ฌธ ์ •๋ณด
            int orderIdx = basketDao.postBasketOrder(userIdx, orderReq);

            // ์ฃผ๋ฌธ์— ๋“ค์–ด๊ฐ€๋Š” ๋ฉ”๋‰ด ์ •๋ณด
            int orderMenuCnt = basketDao.postBasketOrderItems(orderIdx, basketItems);
            if (orderMenuCnt != basketItems.size())
                throw new Exception();

            // ์ฃผ๋ฌธํ•œ๋งŒํผ ์˜ค๋Š˜์˜ ๋ฉ”๋‰ด ๊ฐœ์ˆ˜ ๊ฐ์†Œ
            int tmDescCnt = basketDao.todayMenuDecrease(basketItems);
            if (tmDescCnt != basketItems.size())
                throw new Exception();

            // ์ฃผ๋ฌธํ–‡์œผ๋ฉด ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ์žˆ๋Š” ์•„์ดํ…œ ์ „๋ถ€ ๋น„ํ™œ์„ฑํ™”
            int basketCount = basketDao.basketItemDone(basketItems);

            return new OrderDoneRes(orderIdx, basketItems.size());
        }catch(Exception e){
            throw new BaseException(ORDER_FAILED);
        }
    }
  1. ๊ตฌ๋งค์ž์˜ Idx๋ฅผ ์ด์šฉํ•˜์—ฌ ์žฅ๋ฐ”๊ตฌ๋‹ˆ ๋ชฉ๋ก์„ ์กฐํšŒํ•œ๋‹ค.
  2. ํ•ด๋‹น ์žฅ๋ฐ”๊ตฌ๋‹ˆ ๊ตฌ์„ฑํ’ˆ์ด ํ˜„์žฌ ๊ฐ€๊ฒŒ์— ๋‚จ์•„์žˆ๋Š” ๋“ฑ๋ก ๋ฉ”๋‰ด๋ฅผ ๋Œ€์ƒ์œผ๋กœ ์ฃผ๋ฌธ์ด ๊ฐ€๋Šฅํ•œ์ง€ ํ™•์ธ
  3. ์ฃผ๋ฌธ์ •๋ณด๋ฅผ ์ฃผ๋ฌธ ํ…Œ์ด๋ธ”์— ์‚ฝ์ž…ํ•œ๋‹ค. (Order Table)
  4. ์ฃผ๋ฌธ์— ๊ด€๋ จ๋œ ๋ฉ”๋‰ด๋ฅผ ์ฃผ๋ฌธ ๋ชฉ๋ก ํ…Œ์ด๋ธ”์— ์‚ฝ์ž…ํ•œ๋‹ค. (OrderList Table)
  5. ์˜ค๋Š˜์˜ ๋ฉ”๋‰ด ์ฃผ๋ฌธํ•œ๋งŒํผ ๊ฐ’ ์ˆ˜์ •
  6. ์žฅ๋ฐ”๊ตฌ๋‹ˆ ์ดˆ๊ธฐํ™”
    ์ฃผ๋ฌธ ๋„๋ฉ”์ธ ํฌ์ŠคํŒ…
  • Dao
// ๊ตฌ๋งค์ž์˜ ์žฅ๋ฐ”๊ตฌ๋‹ˆ ์กฐํšŒ
    public List<BasketOrderItem> getBasketOrderItems(int userIdx) {
        String query = "SELECT * FROM Basket WHERE customerIdx = ? AND status = 'A'";

        return this.jdbcTemplate.query(query,
                (rs, rowNum) -> new BasketOrderItem(
                        rs.getInt(1),
                        rs.getInt(2),
                        rs.getInt(3),
                        rs.getInt(4),
                        rs.getInt(5)
                ), userIdx);
    }
// ๊ตฌ๋งค์ž๊ฐ€ ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ๋‹ด์€ ๊ฐœ์ˆ˜๊ฐ€ ์ฃผ๋ฌธ ๊ฐ€๋Šฅํ•œ ๊ฐœ์ˆ˜๋ฅผ ๋„˜๋Š”์ง€ ํ™•์ธ
    public BasketTodayMenu checkItemRemain(int storeIdx, int todayMenuIdx, int cnt) {
        String query = "SELECT\n" +
                "    *\n" +
                "FROM TodayMenu\n" +
                "WHERE storeIdx = ?\n" +
                "AND todaymenuIdx = ?\n" +
                "AND remain >= ?";

        Object[] params = new Object[]{
                storeIdx,
                todayMenuIdx,
                cnt
        };

        return this.jdbcTemplate.queryForObject(query,
                (rs, rowNum) -> new BasketTodayMenu(
                        rs.getInt("todaymenuIdx"),
                        rs.getInt("remain")
                ),params);
    }
// ์ฃผ๋ฌธ ์ •๋ณด ๋ฐœ์ƒ
    public int postBasketOrder(int userIdx, OrderDoneReq orderReq) {
        String query = "INSERT INTO Orders(storeIdx, customerIdx, request, order_time, pickup_time, payment_status)\n" +
                "VALUES(?, ?, ?, NOW(), ?, ?)";

        Object[] params = new Object[]{
                orderReq.getStoreIdx(),
                userIdx,
                orderReq.getRequest(),
                orderReq.getPickupTime(),
                orderReq.getPaymentStatus()
        };

        this.jdbcTemplate.update(query, params);

        String lastInsertIdQuery = "select last_insert_id()";
        return this.jdbcTemplate.queryForObject(lastInsertIdQuery,int.class);
    }
// ์ฃผ๋ฌธ ํ’ˆ๋ชฉ ์‚ฝ์ž…
    public List<BasketOrderItem> getBasketOrderItems(int userIdx) {
        String query = "SELECT * FROM Basket WHERE customerIdx = ? AND status = 'A'";

        return this.jdbcTemplate.query(query,
                (rs, rowNum) -> new BasketOrderItem(
                        rs.getInt(1),
                        rs.getInt(2),
                        rs.getInt(3),
                        rs.getInt(4),
                        rs.getInt(5)
                ), userIdx)
    }
// ๋–จ์ด ๋ฉ”๋‰ด ๋“ฑ๋ก ํ˜„ํ™ฉ ์ˆ˜์ •(๊ฐœ์ˆ˜ ๊ฐ์†Œ)
    public int todayMenuDecrease(List<BasketOrderItem> basketItems) {
        String query = "UPDATE TodayMenu\n" +
                "    SET remain = remain - ?\n" +
                "WHERE todaymenuIdx = ?\n" +
                "AND remain >= ?";

        return this.jdbcTemplate.batchUpdate(query,
                basketItems,
                basketItems.size(),
                (PreparedStatement ps, BasketOrderItem bItem) ->{
                    ps.setInt(1, bItem.getCnt());
                    ps.setInt(2, bItem.getTodayMenuIdx());
                    ps.setInt(3, bItem.getCnt());
                }).length;
    }
// ์žฅ๋ฐ”๊ตฌ๋‹ˆ ์ดˆ๊ธฐํ™”
    public int basketItemDone(List<BasketOrderItem> basketItems) {
        String query = "UPDATE Basket\n" +
                "    SET status = 'D'\n" +
                "WHERE basketIdx = ?";

        return this.jdbcTemplate.batchUpdate(query,
                basketItems,
                basketItems.size(),
                (PreparedStatement ps, BasketOrderItem bItem) ->{
                    ps.setInt(1, bItem.getBasketIdx());
                }).length;
    }

ํ…Œ์ŠคํŠธ

  • ์ฃผ๋ฌธํ•˜๊ธฐ ๊ตฌ๋งค์ž ํ™”๋ฉด์—์„œ ์ง„ํ–‰

Order Table

์ฃผ๋ฌธ ์ •๋ณด ์˜ฌ๋ฐ”๋ฅด๊ฒŒ DB์— ๋ฐ˜์˜

OrderLists Table

ํŒ๋งค์ž ์ฃผ๋ฌธ ํ˜„ํ™ฉํ™•์ธ

0๊ฐœ์˜ ๋Œ“๊ธ€