diff --git a/src/main/java/com/jangburich/domain/order/application/OrderService.java b/src/main/java/com/jangburich/domain/order/application/OrderService.java index f547aeb..87c6a6f 100644 --- a/src/main/java/com/jangburich/domain/order/application/OrderService.java +++ b/src/main/java/com/jangburich/domain/order/application/OrderService.java @@ -4,13 +4,15 @@ import com.jangburich.domain.menu.domain.repository.MenuRepository; import com.jangburich.domain.order.domain.Cart; import com.jangburich.domain.order.domain.repository.CartRepository; -import com.jangburich.domain.order.domain.repository.OrdersRepository; import com.jangburich.domain.order.dto.request.AddCartRequest; +import com.jangburich.domain.order.dto.response.CartResponse; +import com.jangburich.domain.order.dto.response.CartResponse.GetCartItemsResponse; import com.jangburich.domain.store.domain.Store; import com.jangburich.domain.store.domain.repository.StoreRepository; import com.jangburich.domain.user.domain.User; import com.jangburich.domain.user.domain.repository.UserRepository; import com.jangburich.global.payload.Message; +import java.util.List; import java.util.Optional; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -65,4 +67,29 @@ public Message addCart(String userProviderId, AddCartRequest addCartRequest) { .message("장바구니에 상품을 추가했습니다.") .build(); } + + public CartResponse getCartItems(String userProviderId) { + User user = userRepository.findByProviderId(userProviderId) + .orElseThrow(() -> new NullPointerException()); + + List carts = cartRepository.findAllByUser(user); + + if (carts.isEmpty()) { + return CartResponse.of(List.of(), 0); + } + + List cartItems = carts.stream() + .map(cart -> GetCartItemsResponse.of( + cart.getMenu().getName(), + cart.getMenu().getDescription(), + cart.getQuantity(), + cart.getMenu().getPrice() + )) + .toList(); + + int discountAmount = 0; + CartResponse cartResponse = CartResponse.of(cartItems, discountAmount); + + return cartResponse; + } } diff --git a/src/main/java/com/jangburich/domain/order/domain/repository/CartRepository.java b/src/main/java/com/jangburich/domain/order/domain/repository/CartRepository.java index 51cd8a7..3e82e3b 100644 --- a/src/main/java/com/jangburich/domain/order/domain/repository/CartRepository.java +++ b/src/main/java/com/jangburich/domain/order/domain/repository/CartRepository.java @@ -1,6 +1,8 @@ package com.jangburich.domain.order.domain.repository; import com.jangburich.domain.order.domain.Cart; +import com.jangburich.domain.user.domain.User; +import java.util.List; import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; @@ -11,4 +13,6 @@ public interface CartRepository extends JpaRepository { @Query("SELECT c FROM Cart c WHERE c.user.userId = :userId AND c.menu.id = :menuId") Optional findByUserIdAndMenuId(@Param("userId") Long userId, @Param("menuId") Long menuId); + + List findAllByUser(User user); } diff --git a/src/main/java/com/jangburich/domain/order/dto/response/CartResponse.java b/src/main/java/com/jangburich/domain/order/dto/response/CartResponse.java new file mode 100644 index 0000000..2cb6307 --- /dev/null +++ b/src/main/java/com/jangburich/domain/order/dto/response/CartResponse.java @@ -0,0 +1,33 @@ +package com.jangburich.domain.order.dto.response; + +import java.util.List; + +public record CartResponse( + List cartItems, + Integer totalAmount, + Integer discountAmount, + Integer finalAmount +) { + public static CartResponse of(List cartItems, Integer discountAmount) { + int totalAmount = cartItems.stream() + .mapToInt(item -> item.menuPrice * item.quantity) + .sum(); + + discountAmount = 0; + + int finalAmount = totalAmount - discountAmount; + + return new CartResponse(cartItems, totalAmount, discountAmount, finalAmount); + } + + public record GetCartItemsResponse( + String menuName, + String menuDescription, + Integer quantity, + Integer menuPrice + ) { + public static GetCartItemsResponse of(String menuName, String menuDescription, Integer quantity, Integer menuPrice) { + return new GetCartItemsResponse(menuName, menuDescription, quantity, menuPrice); + } + } +} diff --git a/src/main/java/com/jangburich/domain/order/presentation/OrderController.java b/src/main/java/com/jangburich/domain/order/presentation/OrderController.java index e403e6a..914e958 100644 --- a/src/main/java/com/jangburich/domain/order/presentation/OrderController.java +++ b/src/main/java/com/jangburich/domain/order/presentation/OrderController.java @@ -2,6 +2,7 @@ import com.jangburich.domain.order.application.OrderService; import com.jangburich.domain.order.dto.request.AddCartRequest; +import com.jangburich.domain.order.dto.response.CartResponse; import com.jangburich.global.payload.Message; import com.jangburich.global.payload.ResponseCustom; import com.jangburich.utils.parser.AuthenticationParser; @@ -9,6 +10,7 @@ import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; import org.springframework.security.core.Authentication; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; @@ -23,11 +25,19 @@ public class OrderController { private final OrderService orderService; @Operation(summary = "장바구니 담기", description = "장바구니에 물건과 수량을 담습니다.") - @PostMapping("/cart") + @PostMapping("/carts") public ResponseCustom addCart( Authentication authentication, @RequestBody AddCartRequest addCartRequest ) { return ResponseCustom.OK(orderService.addCart(AuthenticationParser.parseUserId(authentication), addCartRequest)); } + + @Operation(summary = "장바구니 조회", description = "장바구니에 담은 상품을 조회합니다.") + @GetMapping("/carts") + public ResponseCustom getCartItems( + Authentication authentication + ) { + return ResponseCustom.OK(orderService.getCartItems(AuthenticationParser.parseUserId(authentication))); + } }