Skip to content

Commit

Permalink
Merge pull request #106 from marinesnow34/name
Browse files Browse the repository at this point in the history
Feat: sold out, 영업 종료 여부 추가
  • Loading branch information
marinesnow34 authored Nov 29, 2023
2 parents f583fab + 2074557 commit 768a90b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ public enum ExceptionCode {
INVALID_INOUT(400, "Invalid inout."),
CART_NOT_EDITABLE(400, "Cart is not editable."),
COUPON_NOT_VALID(400, "Coupon is not valid."),
NOT_MY_CART(400, "Not my cart.");
NOT_MY_CART(400, "Not my cart."),
STORE_NOT_OPEN(400, "Store is not open."),
CART_SOLD_OUT(400, "Cart is sold out."),
CART_INOUT_NOT_MATCH(400, "Cart inout is not match.");

private int status;
private String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public CartAddRes addCart(CustomUserDetails userDetails, CartAddReq cartAddReq)
Cart cart = cartRepository.findByUserInfoAndIsDeletedFalseAndIsOrderedFalse(user)
.orElseGet(() -> makeCart(user, store, cartAddReq.getInout()));

verifyCart(cart);
verifyCart(cart, cartAddReq.getInout());
verifyItemsInCart(cart, store, cartAddReq.getInout());
CartItem cartItem = makeCartItem(cart, foodie, cartAddReq.getCount());
List<CartOption> cartOptions = cartAddReq.getOptions().stream()
Expand Down Expand Up @@ -133,21 +133,27 @@ private void changeCartStore(Cart cart, Store store, Long inout) {
@Override
public CartEidtRes editCart(CustomUserDetails userDetails, Long idx, Long count) {
CartItem cartItem = getCartItem(idx);
verifyCart(cartItem.getCart());
verifyCart(cartItem.getCart(), null);
verifyCartItem(cartItem, userDetails);

editCartItem(cartItem, count);
cartItemRepository.save(cartItem);
return orderMapper.cartToCartEditRes(cartItem);
}

private void verifyCart(Cart cart) {
private void verifyCart(Cart cart, Long inout) {
if (cart.getIsOrdered()) {
throw new BusinessLogicException(ExceptionCode.CART_NOT_EDITABLE);
}
if (cart.getIsDeleted()) {
throw new BusinessLogicException(ExceptionCode.CART_NOT_EDITABLE);
}
if (inout == null) {
return;
}
if (!cart.getInOut().equals(inout)) {
throw new BusinessLogicException(ExceptionCode.CART_INOUT_NOT_MATCH);
}
}

@Override
Expand Down Expand Up @@ -202,6 +208,7 @@ public TosspaymentMakeRes requestTossPayment(CustomUserDetails userDetails, Paym
Coupon coupon = getCoupon(paymentReq.getCouponId());

verifyCoupon(user, coupon);
verifyCartSoldOut(cart);
// Long amount = calculateAmount(store, paymentReq.getCarts(), paymentReq.getInout());
Long amount = calculateAmount2(cart);
Order order = makeOrder(user, store, amount, cart, coupon);
Expand All @@ -211,6 +218,12 @@ public TosspaymentMakeRes requestTossPayment(CustomUserDetails userDetails, Paym
return orderMapper.orderToTosspaymentMakeRes(order);
}

private void verifyCartSoldOut(Cart cart) {
if (cart.getCartItems().stream().anyMatch(cartItem -> cartItem.getFoodie().isSoldOut())) {
throw new BusinessLogicException(ExceptionCode.CART_SOLD_OUT);
}
}

private void verifyCoupon(UserInfo user, Coupon coupon) {
isCoupon(coupon);
isUserCoupon(user, coupon);
Expand Down Expand Up @@ -590,11 +603,25 @@ private Store getStore(Long storeId) {
}

private void verifyCartAddReq(Foodie foodie, CartAddReq cartAddReq) {
verifyStoreOpen(foodie.getFoodieCategory().getStore());
verifyFoodyNotSoldOut(foodie);
verifyOption(foodie, cartAddReq.getOptions());
verifyEssentialOption(foodie, cartAddReq.getOptions());
verifyInout(cartAddReq.getInout());
}

private void verifyFoodyNotSoldOut(Foodie foodie) {
if (foodie.isSoldOut()) {
throw new BusinessLogicException(ExceptionCode.FOODY_NOT_FOUND);
}
}

private void verifyStoreOpen(Store store) {
if (!store.isStatus()) {
throw new BusinessLogicException(ExceptionCode.STORE_NOT_OPEN);
}
}

private void verifyInout(Long inout) {
if (!inout.equals(EAT_IN) && !inout.equals(TAKE_OUT)) {
throw new BusinessLogicException(ExceptionCode.INVALID_INOUT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ private MenuDto categoryToMenuDto(FoodieCategory category) {
.category(category.getName())
.menuItems(category.getFoodies()
.stream()
.filter(foodie -> !foodie.isSoldOut())
.map(this::foodieToMenuItems)
.toList())
.build();
Expand Down

0 comments on commit 768a90b

Please sign in to comment.