์๊ธ๋นต์ ๋ค์ ์ฅ๋ฐ๊ตฌ๋์ ๋ด๊ณ ์ฃผ๋ฌธ ํ ์ค๋น๋ฅผ ํด๋ณด๊ฒ ๋ค.
- ์ฃผ๋ฌธ ํ๋ฉด์์ ์ฃผ๋ฌธ ์ ๋ณด๋ฅผ ์ฌ์ฉ์์๊ฒ ์ฌ์ ์ผ๋ก ๋ณด์ฌ์ฃผ๊ธฐ ์ํ ๋ก์ง์ ๊ตฌํํ์๋ค.
- ์ฃผ๋ฌธ ํ๋ฉด์ ๋ณด์ด๋ ์ ๋ณด๋ : ์ฃผ๋ฌธํ ๊ฐ๊ฒ, ๊ฐ๊ฒ์ ์ฃผ์, ๋ด ๋ฒํธ, ์ฃผ๋ฌธ ์ด ๊ธ์ก, + ๋ณธ์ธ์ ๊ฒฐ์ ์ ๋ณด ๋ฑ์ ๋์ ์ค ์์๋ค.
- ๋๋จธ์ง ํฝ์ ์์ ์๊ฐ, ์์ฒญ์ฌํญ, ๊ฒฐ์ ์ฌํญ ๋ฑ์ ๊ตฌ๋งค์๊ฐ ์ง์ ์ ํํ๋ ๊ธฐ์ค์ผ๋ก ์ ํ์๋ค.
- ์๋ต ๋ฐ๋
- ์ฃผ๋ฌธ์ ์ฃผ์ฒด๋ฅผ ๊ฐ๊ฒ์ ์ฐ๊ด์ํค๊ธฐ ์ํ์ฌ 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); }
- ์ฌ์ฉ์๊ฐ ์ฃผ๋ฌธ ์ ๋ณด๋ฅผ ๋ชจ๋ ์์ฑํ๊ณ ์ฃผ๋ฌธ์ ํ์ฌ ๊ฒฐ์ ์ ๋ณด๋ฅผ ๋ฐ์์ํค๊ธฐ ์ํ ๋จ๊ณ
- ํด๋น ์ฃผ๋ฌธ ์์ฒญ์ ๋ฐ์ ์ ๊ตฌ๋งค์์ ์ฃผ๋ฌธํํฉํ๋ฉด์ ์ฃผ๋ฌธ ์์ฒญ์ด ์ถ๊ฐ๊ฐ ๋๋ค.
- ํ๋งค์/๊ตฌ๋งค์ ๋ชจ๋ ์ฃผ๋ฌธ์ ๋ํด์ ๊ฐ์ ํ ์ด๋ธ์ ๋ฐ๋ผ๋ณด๋๋ก ์ค๊ณํ์๋ค.
- ์์ฒญ ๋ฐ๋
- ์ฅ๋ฐ๊ตฌ๋์ ๋ํ ์ ๋ณด๊ฐ ์๋๋ฐ, ์ด๋ ์ฅ๋ฐ๊ตฌ๋ ํ ์ด๋ธ์ ์ ์ ์ 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); } }
- ๊ตฌ๋งค์์ Idx๋ฅผ ์ด์ฉํ์ฌ ์ฅ๋ฐ๊ตฌ๋ ๋ชฉ๋ก์ ์กฐํํ๋ค.
- ํด๋น ์ฅ๋ฐ๊ตฌ๋ ๊ตฌ์ฑํ์ด ํ์ฌ ๊ฐ๊ฒ์ ๋จ์์๋ ๋ฑ๋ก ๋ฉ๋ด๋ฅผ ๋์์ผ๋ก ์ฃผ๋ฌธ์ด ๊ฐ๋ฅํ์ง ํ์ธ
- ์ฃผ๋ฌธ์ ๋ณด๋ฅผ ์ฃผ๋ฌธ ํ ์ด๋ธ์ ์ฝ์ ํ๋ค. (Order Table)
- ์ฃผ๋ฌธ์ ๊ด๋ จ๋ ๋ฉ๋ด๋ฅผ ์ฃผ๋ฌธ ๋ชฉ๋ก ํ ์ด๋ธ์ ์ฝ์ ํ๋ค. (OrderList Table)
- ์ค๋์ ๋ฉ๋ด ์ฃผ๋ฌธํ๋งํผ ๊ฐ ์์
- ์ฅ๋ฐ๊ตฌ๋ ์ด๊ธฐํ
์ฃผ๋ฌธ ๋๋ฉ์ธ ํฌ์คํ
- 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
ํ๋งค์ ์ฃผ๋ฌธ ํํฉํ์ธ