From bf22bb7cee8c5b1376ad178b4a27277ce9d26bce Mon Sep 17 00:00:00 2001 From: marinesnow34 Date: Sun, 19 Nov 2023 16:44:05 +0900 Subject: [PATCH 1/2] =?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 --- .../readyverydemo/src/order/dto/CartDto.java | 16 ++++++ .../src/order/dto/CartGetRes.java | 15 +++++ .../src/order/dto/OptionDto.java | 12 ++++ .../src/order/dto/OrderMapper.java | 55 ++++++++++++++++++- 4 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/readyvery/readyverydemo/src/order/dto/CartDto.java create mode 100644 src/main/java/com/readyvery/readyverydemo/src/order/dto/CartGetRes.java create mode 100644 src/main/java/com/readyvery/readyverydemo/src/order/dto/OptionDto.java diff --git a/src/main/java/com/readyvery/readyverydemo/src/order/dto/CartDto.java b/src/main/java/com/readyvery/readyverydemo/src/order/dto/CartDto.java new file mode 100644 index 0000000..bbde913 --- /dev/null +++ b/src/main/java/com/readyvery/readyverydemo/src/order/dto/CartDto.java @@ -0,0 +1,16 @@ +package com.readyvery.readyverydemo.src.order.dto; + +import java.util.List; + +import lombok.Builder; +import lombok.Getter; + +@Getter +@Builder +public class CartDto { + private Long idx; + private String name; + private Long count; + private Long totalPrice; + private List options; +} diff --git a/src/main/java/com/readyvery/readyverydemo/src/order/dto/CartGetRes.java b/src/main/java/com/readyvery/readyverydemo/src/order/dto/CartGetRes.java new file mode 100644 index 0000000..32a81fd --- /dev/null +++ b/src/main/java/com/readyvery/readyverydemo/src/order/dto/CartGetRes.java @@ -0,0 +1,15 @@ +package com.readyvery.readyverydemo.src.order.dto; + +import java.util.List; + +import lombok.Builder; +import lombok.Getter; + +@Getter +@Builder +public class CartGetRes { + private String name; + private String imgUrl; + private Long totalPrice; + private List carts; +} diff --git a/src/main/java/com/readyvery/readyverydemo/src/order/dto/OptionDto.java b/src/main/java/com/readyvery/readyverydemo/src/order/dto/OptionDto.java new file mode 100644 index 0000000..37ca33c --- /dev/null +++ b/src/main/java/com/readyvery/readyverydemo/src/order/dto/OptionDto.java @@ -0,0 +1,12 @@ +package com.readyvery.readyverydemo.src.order.dto; + +import lombok.Builder; +import lombok.Getter; + +@Getter +@Builder +public class OptionDto { + private Long idx; + private String name; + private Long price; +} 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 a28ede5..0212ff2 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 @@ -8,6 +8,7 @@ import com.readyvery.readyverydemo.domain.Cart; import com.readyvery.readyverydemo.domain.CartItem; +import com.readyvery.readyverydemo.domain.CartOption; import com.readyvery.readyverydemo.domain.Foodie; import com.readyvery.readyverydemo.domain.FoodieOption; import com.readyvery.readyverydemo.domain.FoodieOptionCategory; @@ -35,8 +36,8 @@ private Long determinePrice(Foodie foodie, Long inout) { } else if (Objects.equals(inout, TAKE_OUT)) { return foodie.getTakeOut() != null ? foodie.getTakeOut().getPrice() : foodie.getPrice(); } - // inout이 EAT_IN도 TAKE_OUT도 아닌 경우, null을 반환 - return null; + // inout이 EAT_IN도 TAKE_OUT도 아닌 경우, 기본 값 반환 + return foodie.getPrice(); } private OptionCategoryDto foodieOptionCategoryToOptionCategoryDto(FoodieOptionCategory category) { @@ -83,4 +84,54 @@ public CartResetRes cartToCartResetRes(Cart cart) { .idx(cart.getId()) .build(); } + + public CartGetRes cartToCartGetRes(Cart cart, Long inout) { + return CartGetRes.builder() + .name(cart.getStore().getName()) + .imgUrl(cart.getStore().getImgs().get(0).getImgUrl()) + .carts( + cart.getCartItems() + .stream() + .map(cartItem -> cartItemToCartDto(cartItem, inout)) + .toList()) + .totalPrice( + cart.getCartItems() + .stream() + .mapToLong(cartItem -> cartItemTotalPrice(cartItem, inout)) + .sum()) + .build(); + } + + private CartDto cartItemToCartDto(CartItem cartItem, Long inout) { + return CartDto.builder() + .idx(cartItem.getId()) + .name(cartItem.getFoodie().getName()) + .count(cartItem.getCount()) + .totalPrice(cartItemTotalPrice(cartItem, inout)) + .options( + cartItem.getCartOptions() + .stream() + .map(this::cartOptionToOptionDto) + .toList()) + .build(); + } + + private OptionDto cartOptionToOptionDto(CartOption cartOption) { + return OptionDto.builder() + .idx(cartOption.getId()) + .name(cartOption.getFoodieOption().getName()) + .price(cartOption.getFoodieOption().getPrice()) + .build(); + } + + private Long cartItemTotalPrice(CartItem cartItem, Long inout) { + Long optionsPriceSum = cartItem.getCartOptions() + .stream() + .mapToLong(cartOption -> cartOption.getFoodieOption().getPrice()) + .sum(); + + Long totalPrice = optionsPriceSum + determinePrice(cartItem.getFoodie(), inout); + return totalPrice * cartItem.getCount(); + } + } From 995247085916972c6a8473d0815143607d9e54bc Mon Sep 17 00:00:00 2001 From: marinesnow34 Date: Sun, 19 Nov 2023 16:44:16 +0900 Subject: [PATCH 2/2] =?UTF-8?q?Feat:=20=EC=9E=A5=EB=B0=94=EA=B5=AC?= =?UTF-8?q?=EB=8B=88=20=EC=A1=B0=ED=9A=8C=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../readyverydemo/src/order/OrderController.java | 8 ++++++++ .../readyvery/readyverydemo/src/order/OrderService.java | 3 +++ .../readyverydemo/src/order/OrderServiceImpl.java | 9 +++++++++ 3 files changed, 20 insertions(+) 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 b992510..c544aa2 100644 --- a/src/main/java/com/readyvery/readyverydemo/src/order/OrderController.java +++ b/src/main/java/com/readyvery/readyverydemo/src/order/OrderController.java @@ -18,6 +18,7 @@ import com.readyvery.readyverydemo.src.order.dto.CartAddRes; import com.readyvery.readyverydemo.src.order.dto.CartEditReq; import com.readyvery.readyverydemo.src.order.dto.CartEidtRes; +import com.readyvery.readyverydemo.src.order.dto.CartGetRes; import com.readyvery.readyverydemo.src.order.dto.CartItemDeleteReq; import com.readyvery.readyverydemo.src.order.dto.CartItemDeleteRes; import com.readyvery.readyverydemo.src.order.dto.CartResetRes; @@ -40,6 +41,13 @@ public ResponseEntity getFoody( return new ResponseEntity<>(foodyDetailRes, HttpStatus.OK); } + @GetMapping("/cart") + public ResponseEntity getCart(@AuthenticationPrincipal CustomUserDetails userDetails, + @RequestParam("inout") Long inout) { + CartGetRes cartGetRes = orderService.getCart(userDetails, inout); + return new ResponseEntity<>(cartGetRes, HttpStatus.OK); + } + @PostMapping("/cart") public ResponseEntity addCart(@AuthenticationPrincipal CustomUserDetails userDetails, @RequestBody CartAddReq cartAddReq) { 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 9a970b9..cf2acd3 100644 --- a/src/main/java/com/readyvery/readyverydemo/src/order/OrderService.java +++ b/src/main/java/com/readyvery/readyverydemo/src/order/OrderService.java @@ -5,6 +5,7 @@ import com.readyvery.readyverydemo.src.order.dto.CartAddRes; import com.readyvery.readyverydemo.src.order.dto.CartEditReq; import com.readyvery.readyverydemo.src.order.dto.CartEidtRes; +import com.readyvery.readyverydemo.src.order.dto.CartGetRes; import com.readyvery.readyverydemo.src.order.dto.CartItemDeleteReq; import com.readyvery.readyverydemo.src.order.dto.CartItemDeleteRes; import com.readyvery.readyverydemo.src.order.dto.CartResetRes; @@ -20,4 +21,6 @@ public interface OrderService { CartItemDeleteRes deleteCart(CustomUserDetails userDetails, CartItemDeleteReq cartItemDeleteReq); CartResetRes resetCart(CustomUserDetails userDetails); + + CartGetRes getCart(CustomUserDetails userDetails, Long inout); } 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 e4e6e62..d8bc633 100644 --- a/src/main/java/com/readyvery/readyverydemo/src/order/OrderServiceImpl.java +++ b/src/main/java/com/readyvery/readyverydemo/src/order/OrderServiceImpl.java @@ -28,6 +28,7 @@ import com.readyvery.readyverydemo.src.order.dto.CartAddRes; import com.readyvery.readyverydemo.src.order.dto.CartEditReq; import com.readyvery.readyverydemo.src.order.dto.CartEidtRes; +import com.readyvery.readyverydemo.src.order.dto.CartGetRes; import com.readyvery.readyverydemo.src.order.dto.CartItemDeleteReq; import com.readyvery.readyverydemo.src.order.dto.CartItemDeleteRes; import com.readyvery.readyverydemo.src.order.dto.CartResetRes; @@ -118,6 +119,14 @@ public CartResetRes resetCart(CustomUserDetails userDetails) { return orderMapper.cartToCartResetRes(cart); } + @Override + public CartGetRes getCart(CustomUserDetails userDetails, Long inout) { + UserInfo user = getUserInfo(userDetails); + Cart cart = getCart(user); + + return orderMapper.cartToCartGetRes(cart, inout); + } + private void resetCartItem(Cart cart) { cart.setIsDeleted(true); }