From eeeb3e83d00301cb3325c08e43aa41bac0a8ff7d Mon Sep 17 00:00:00 2001 From: marinesnow34 Date: Sun, 19 Nov 2023 14:58:51 +0900 Subject: [PATCH 1/3] =?UTF-8?q?Config:=20dto=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/readyvery/readyverydemo/domain/Cart.java | 2 ++ .../readyverydemo/src/order/dto/CartResetRes.java | 10 ++++++++++ .../readyverydemo/src/order/dto/OrderMapper.java | 7 +++++++ 3 files changed, 19 insertions(+) create mode 100644 src/main/java/com/readyvery/readyverydemo/src/order/dto/CartResetRes.java diff --git a/src/main/java/com/readyvery/readyverydemo/domain/Cart.java b/src/main/java/com/readyvery/readyverydemo/domain/Cart.java index c082a49..a464419 100644 --- a/src/main/java/com/readyvery/readyverydemo/domain/Cart.java +++ b/src/main/java/com/readyvery/readyverydemo/domain/Cart.java @@ -19,6 +19,7 @@ import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; +import lombok.Setter; import lombok.extern.slf4j.Slf4j; @Getter @@ -28,6 +29,7 @@ @AllArgsConstructor @Slf4j @Builder +@Setter public class Cart extends BaseTimeEntity { @Id diff --git a/src/main/java/com/readyvery/readyverydemo/src/order/dto/CartResetRes.java b/src/main/java/com/readyvery/readyverydemo/src/order/dto/CartResetRes.java new file mode 100644 index 0000000..b6541d8 --- /dev/null +++ b/src/main/java/com/readyvery/readyverydemo/src/order/dto/CartResetRes.java @@ -0,0 +1,10 @@ +package com.readyvery.readyverydemo.src.order.dto; + +import lombok.Builder; +import lombok.Getter; + +@Getter +@Builder +public class CartResetRes { + private Long idx; +} diff --git a/src/main/java/com/readyvery/readyverydemo/src/order/dto/OrderMapper.java b/src/main/java/com/readyvery/readyverydemo/src/order/dto/OrderMapper.java index 5ffc796..a28ede5 100644 --- a/src/main/java/com/readyvery/readyverydemo/src/order/dto/OrderMapper.java +++ b/src/main/java/com/readyvery/readyverydemo/src/order/dto/OrderMapper.java @@ -6,6 +6,7 @@ import org.springframework.stereotype.Component; +import com.readyvery.readyverydemo.domain.Cart; import com.readyvery.readyverydemo.domain.CartItem; import com.readyvery.readyverydemo.domain.Foodie; import com.readyvery.readyverydemo.domain.FoodieOption; @@ -76,4 +77,10 @@ public CartItemDeleteRes cartToCartItemDeleteRes(CartItem cartItem) { .idx(cartItem.getId()) .build(); } + + public CartResetRes cartToCartResetRes(Cart cart) { + return CartResetRes.builder() + .idx(cart.getId()) + .build(); + } } From 9aff89728f0e68899a90a6ebe7977bc615683e45 Mon Sep 17 00:00:00 2001 From: marinesnow34 Date: Sun, 19 Nov 2023 14:59:02 +0900 Subject: [PATCH 2/3] =?UTF-8?q?Feat:=20=EC=9E=A5=EB=B0=94=EA=B5=AC?= =?UTF-8?q?=EB=8B=88=20=EC=82=AD=EC=A0=9C=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../global/exception/ExceptionCode.java | 3 ++- .../src/order/OrderController.java | 7 ++++++ .../readyverydemo/src/order/OrderService.java | 3 +++ .../src/order/OrderServiceImpl.java | 22 +++++++++++++++++++ 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/readyvery/readyverydemo/global/exception/ExceptionCode.java b/src/main/java/com/readyvery/readyverydemo/global/exception/ExceptionCode.java index 86c5db5..bd6e46e 100644 --- a/src/main/java/com/readyvery/readyverydemo/global/exception/ExceptionCode.java +++ b/src/main/java/com/readyvery/readyverydemo/global/exception/ExceptionCode.java @@ -11,7 +11,8 @@ public enum ExceptionCode { INVALID_OPTION_COUNT(400, "Invalid option count."), INVALID_OPTION(400, "Invalid option."), OPTION_NOT_FOUND(404, "Option does not exists."), - CART_ITEM_NOT_FOUND(404, "Cart item does not exists."); + CART_ITEM_NOT_FOUND(404, "Cart item does not exists."), + CART_NOT_FOUND(404, "Cart does not exists."); private int status; private String message; diff --git a/src/main/java/com/readyvery/readyverydemo/src/order/OrderController.java b/src/main/java/com/readyvery/readyverydemo/src/order/OrderController.java index 19bbdc3..b992510 100644 --- a/src/main/java/com/readyvery/readyverydemo/src/order/OrderController.java +++ b/src/main/java/com/readyvery/readyverydemo/src/order/OrderController.java @@ -20,6 +20,7 @@ import com.readyvery.readyverydemo.src.order.dto.CartEidtRes; import com.readyvery.readyverydemo.src.order.dto.CartItemDeleteReq; import com.readyvery.readyverydemo.src.order.dto.CartItemDeleteRes; +import com.readyvery.readyverydemo.src.order.dto.CartResetRes; import com.readyvery.readyverydemo.src.order.dto.FoodyDetailRes; import lombok.RequiredArgsConstructor; @@ -59,4 +60,10 @@ public ResponseEntity deleteCartItem(@AuthenticationPrincipal CartItemDeleteRes cartItemDeleteRes = orderService.deleteCart(userDetails, cartItemDeleteReq); return new ResponseEntity<>(cartItemDeleteRes, HttpStatus.OK); } + + @DeleteMapping("/cart/reset") + public ResponseEntity resetCart(@AuthenticationPrincipal CustomUserDetails userDetails) { + CartResetRes cartResetRes = orderService.resetCart(userDetails); + return new ResponseEntity<>(cartResetRes, HttpStatus.OK); + } } diff --git a/src/main/java/com/readyvery/readyverydemo/src/order/OrderService.java b/src/main/java/com/readyvery/readyverydemo/src/order/OrderService.java index b974c5d..9a970b9 100644 --- a/src/main/java/com/readyvery/readyverydemo/src/order/OrderService.java +++ b/src/main/java/com/readyvery/readyverydemo/src/order/OrderService.java @@ -7,6 +7,7 @@ import com.readyvery.readyverydemo.src.order.dto.CartEidtRes; import com.readyvery.readyverydemo.src.order.dto.CartItemDeleteReq; import com.readyvery.readyverydemo.src.order.dto.CartItemDeleteRes; +import com.readyvery.readyverydemo.src.order.dto.CartResetRes; import com.readyvery.readyverydemo.src.order.dto.FoodyDetailRes; public interface OrderService { @@ -17,4 +18,6 @@ public interface OrderService { CartEidtRes editCart(CustomUserDetails userDetails, CartEditReq cartEditReq); CartItemDeleteRes deleteCart(CustomUserDetails userDetails, CartItemDeleteReq cartItemDeleteReq); + + CartResetRes resetCart(CustomUserDetails userDetails); } diff --git a/src/main/java/com/readyvery/readyverydemo/src/order/OrderServiceImpl.java b/src/main/java/com/readyvery/readyverydemo/src/order/OrderServiceImpl.java index 832c7cb..73dfb1f 100644 --- a/src/main/java/com/readyvery/readyverydemo/src/order/OrderServiceImpl.java +++ b/src/main/java/com/readyvery/readyverydemo/src/order/OrderServiceImpl.java @@ -30,6 +30,7 @@ import com.readyvery.readyverydemo.src.order.dto.CartEidtRes; import com.readyvery.readyverydemo.src.order.dto.CartItemDeleteReq; import com.readyvery.readyverydemo.src.order.dto.CartItemDeleteRes; +import com.readyvery.readyverydemo.src.order.dto.CartResetRes; import com.readyvery.readyverydemo.src.order.dto.FoodyDetailRes; import com.readyvery.readyverydemo.src.order.dto.OrderMapper; @@ -99,6 +100,27 @@ public CartItemDeleteRes deleteCart(CustomUserDetails userDetails, CartItemDelet return orderMapper.cartToCartItemDeleteRes(cartItem); } + @Override + public CartResetRes resetCart(CustomUserDetails userDetails) { + UserInfo user = getUserInfo(userDetails); + Cart cart = getCart(user); + + resetCartItem(cart); + + cartRepository.save(cart); + return orderMapper.cartToCartResetRes(cart); + } + + private void resetCartItem(Cart cart) { + cart.setIsDeleted(true); + } + + private Cart getCart(UserInfo user) { + return cartRepository.findByUserInfoAndIsDeletedFalse(user).orElseThrow( + () -> new BusinessLogicException(ExceptionCode.CART_NOT_FOUND) + ); + } + private void deleteCartItem(CartItem cartItem) { cartItem.setIsDeleted(true); } From 26693d81feba6aab2effee056956743db5340ae1 Mon Sep 17 00:00:00 2001 From: marinesnow34 Date: Sun, 19 Nov 2023 15:10:00 +0900 Subject: [PATCH 3/3] =?UTF-8?q?Fix:=20=EC=9E=A5=EB=B0=94=EA=B5=AC=EB=8B=88?= =?UTF-8?q?=20=EB=8B=A4=EB=A5=B8=20=EA=B0=80=EA=B2=8C=20=EC=95=84=EC=9D=B4?= =?UTF-8?q?=ED=85=9C=20=EC=B6=94=EA=B0=80=EC=8B=9C=20=EC=98=A4=EB=A5=98=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../readyverydemo/global/exception/ExceptionCode.java | 3 ++- .../readyverydemo/src/order/OrderServiceImpl.java | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/readyvery/readyverydemo/global/exception/ExceptionCode.java b/src/main/java/com/readyvery/readyverydemo/global/exception/ExceptionCode.java index bd6e46e..68926f1 100644 --- a/src/main/java/com/readyvery/readyverydemo/global/exception/ExceptionCode.java +++ b/src/main/java/com/readyvery/readyverydemo/global/exception/ExceptionCode.java @@ -12,7 +12,8 @@ public enum ExceptionCode { INVALID_OPTION(400, "Invalid option."), OPTION_NOT_FOUND(404, "Option does not exists."), CART_ITEM_NOT_FOUND(404, "Cart item does not exists."), - CART_NOT_FOUND(404, "Cart does not exists."); + CART_NOT_FOUND(404, "Cart does not exists."), + ITEM_NOT_SAME_STORE(400, "Item is not same store."); private int status; private String message; diff --git a/src/main/java/com/readyvery/readyverydemo/src/order/OrderServiceImpl.java b/src/main/java/com/readyvery/readyverydemo/src/order/OrderServiceImpl.java index 73dfb1f..e4e6e62 100644 --- a/src/main/java/com/readyvery/readyverydemo/src/order/OrderServiceImpl.java +++ b/src/main/java/com/readyvery/readyverydemo/src/order/OrderServiceImpl.java @@ -66,6 +66,7 @@ public CartAddRes addCart(CustomUserDetails userDetails, CartAddReq cartAddReq) verifyCartAddReq(foodie, cartAddReq); Cart cart = cartRepository.findByUserInfoAndIsDeletedFalse(user).orElseGet(() -> makeCart(user, store)); + verifyItemsInCart(cart, store); CartItem cartItem = makeCartItem(cart, foodie, cartAddReq.getCount()); List cartOptions = cartAddReq.getOptions().stream() .map(option -> makeCartOption(cartItem, option)) @@ -78,6 +79,12 @@ public CartAddRes addCart(CustomUserDetails userDetails, CartAddReq cartAddReq) return orderMapper.cartToCartAddRes(cartItem); } + private void verifyItemsInCart(Cart cart, Store store) { + if (!cart.getStore().equals(store)) { + throw new BusinessLogicException(ExceptionCode.ITEM_NOT_SAME_STORE); + } + } + @Override public CartEidtRes editCart(CustomUserDetails userDetails, CartEditReq cartEditReq) { CartItem cartItem = getCartItem(cartEditReq.getIdx());