From cc4feda063ff6f68667f18297b33d7262c248c59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=95=88=EC=A0=95=ED=9B=84?= Date: Sun, 4 Aug 2024 21:04:55 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=A3=BC=EB=AC=B8=20=EC=A0=84=EC=B2=B4?= =?UTF-8?q?=20=EC=A1=B0=ED=9A=8C=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cart/application/CartSaveUseCase.java | 2 +- .../application/CustomerFIndUseCase.java | 87 +++++++++++++++++++ .../application/CustomerSaveUseCase.java | 9 ++ .../apimodule/customer/dto/OrderMenu.java | 7 ++ .../apimodule/customer/dto/TotalOrder.java | 18 ++++ .../presentation/CustomerController.java | 57 ++++++++++++ .../service/CustomerQueryService.java | 9 ++ .../coremodule/order/domain/OrderItem.java | 3 +- 8 files changed, 189 insertions(+), 3 deletions(-) create mode 100644 api-module/src/main/java/com/likelion/apimodule/customer/application/CustomerFIndUseCase.java create mode 100644 api-module/src/main/java/com/likelion/apimodule/customer/application/CustomerSaveUseCase.java create mode 100644 api-module/src/main/java/com/likelion/apimodule/customer/dto/OrderMenu.java create mode 100644 api-module/src/main/java/com/likelion/apimodule/customer/dto/TotalOrder.java create mode 100644 api-module/src/main/java/com/likelion/apimodule/customer/presentation/CustomerController.java create mode 100644 core-module/src/main/java/com/likelion/coremodule/customer/service/CustomerQueryService.java diff --git a/api-module/src/main/java/com/likelion/apimodule/cart/application/CartSaveUseCase.java b/api-module/src/main/java/com/likelion/apimodule/cart/application/CartSaveUseCase.java index cbea8b2..70bb4ff 100644 --- a/api-module/src/main/java/com/likelion/apimodule/cart/application/CartSaveUseCase.java +++ b/api-module/src/main/java/com/likelion/apimodule/cart/application/CartSaveUseCase.java @@ -39,7 +39,7 @@ public String saveMyCart(String accessToken, CartSaveReq saveReq) { if (findMyCartCount(user.getUserId(), menu.getId()) >= 1) { Cart cart = cartQueryService.findCartByUserIdAndMenuId(user.getUserId(), menu.getId()); - cart.setCartQuantity(saveReq.quantity()); + cart.setCartQuantity(saveReq.quantity() + cart.getQuantity()); return "추가"; } else { diff --git a/api-module/src/main/java/com/likelion/apimodule/customer/application/CustomerFIndUseCase.java b/api-module/src/main/java/com/likelion/apimodule/customer/application/CustomerFIndUseCase.java new file mode 100644 index 0000000..fe0e8fc --- /dev/null +++ b/api-module/src/main/java/com/likelion/apimodule/customer/application/CustomerFIndUseCase.java @@ -0,0 +1,87 @@ +package com.likelion.apimodule.customer.application; + +import com.likelion.apimodule.customer.dto.TotalOrder; +import com.likelion.apimodule.order.dto.MenuOrder; +import com.likelion.coremodule.customer.service.CustomerQueryService; +import com.likelion.coremodule.menu.domain.Menu; +import com.likelion.coremodule.menu.service.MenuQueryService; +import com.likelion.coremodule.order.domain.Order; +import com.likelion.coremodule.order.domain.OrderItem; +import com.likelion.coremodule.order.service.OrderQueryService; +import com.likelion.coremodule.user.application.UserQueryService; +import com.likelion.coremodule.user.domain.User; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +@Service +@RequiredArgsConstructor +public class CustomerFIndUseCase { + + private final CustomerQueryService customerQueryService; + private final MenuQueryService menuQueryService; + private final OrderQueryService orderQueryService; + private final UserQueryService userQueryService; + + public List getTotalOrder(Long storeId) { + + List menus = menuQueryService.findMenusByStoreId(storeId); + List items = new ArrayList<>(); + for (Menu menu : menus) { + List item = orderQueryService.findOrderItemsByMenuId(menu.getId()); + items.addAll(item); + } + + Map> groupedItems = new HashMap<>(); + for (OrderItem item : items) { + groupedItems.computeIfAbsent(item.getOrder().getId(), k -> new ArrayList<>()).add(item); + } + + List totalOrders = new ArrayList<>(); + + for (Map.Entry> entry : groupedItems.entrySet()) { + + List orderItems = entry.getValue(); + Order order = orderQueryService.findOrderById(orderItems.get(0).getOrder().getId()); + User user = userQueryService.findById(order.getUser().getUserId()); + + List menuOrders = orderItems.stream() + .map(item -> { + + Menu menu = menuQueryService.findMenuById(item.getMenu().getId()); + + return new MenuOrder( + menu.getName(), + menu.getImageUrl(), + item.getQuantity(), + menu.getPrice() * item.getQuantity() + ); + }) + .collect(Collectors.toList()); + + Integer totalPrice = menuOrders.stream() + .mapToInt(MenuOrder::totalPrice) + .sum(); + + TotalOrder totalOrder = new TotalOrder( + order.getPickUpRoute(), + order.getVisitHour().toString(), + order.getVisitMin().toString(), + user.getName(), + order.getCreatedAt(), + order.getOrderNum(), + menuOrders, + totalPrice + ); + + totalOrders.add(totalOrder); + } + + return totalOrders; + } +} diff --git a/api-module/src/main/java/com/likelion/apimodule/customer/application/CustomerSaveUseCase.java b/api-module/src/main/java/com/likelion/apimodule/customer/application/CustomerSaveUseCase.java new file mode 100644 index 0000000..74ff2bf --- /dev/null +++ b/api-module/src/main/java/com/likelion/apimodule/customer/application/CustomerSaveUseCase.java @@ -0,0 +1,9 @@ +package com.likelion.apimodule.customer.application; + +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +public class CustomerSaveUseCase { +} diff --git a/api-module/src/main/java/com/likelion/apimodule/customer/dto/OrderMenu.java b/api-module/src/main/java/com/likelion/apimodule/customer/dto/OrderMenu.java new file mode 100644 index 0000000..86eb3e6 --- /dev/null +++ b/api-module/src/main/java/com/likelion/apimodule/customer/dto/OrderMenu.java @@ -0,0 +1,7 @@ +package com.likelion.apimodule.customer.dto; + +public record OrderMenu( + Long menuId, + String menuName +) { +} diff --git a/api-module/src/main/java/com/likelion/apimodule/customer/dto/TotalOrder.java b/api-module/src/main/java/com/likelion/apimodule/customer/dto/TotalOrder.java new file mode 100644 index 0000000..0e4eb72 --- /dev/null +++ b/api-module/src/main/java/com/likelion/apimodule/customer/dto/TotalOrder.java @@ -0,0 +1,18 @@ +package com.likelion.apimodule.customer.dto; + +import com.likelion.apimodule.order.dto.MenuOrder; + +import java.time.LocalDateTime; +import java.util.List; + +public record TotalOrder( + String pickUpRoute, + String visitHour, + String visitMin, + String userName, + LocalDateTime createdAt, + String orderNum, + List menu, + Integer totalPrice +) { +} diff --git a/api-module/src/main/java/com/likelion/apimodule/customer/presentation/CustomerController.java b/api-module/src/main/java/com/likelion/apimodule/customer/presentation/CustomerController.java new file mode 100644 index 0000000..cf4cf5c --- /dev/null +++ b/api-module/src/main/java/com/likelion/apimodule/customer/presentation/CustomerController.java @@ -0,0 +1,57 @@ +package com.likelion.apimodule.customer.presentation; + +import com.likelion.apimodule.customer.application.CustomerFIndUseCase; +import com.likelion.apimodule.customer.application.CustomerSaveUseCase; +import com.likelion.apimodule.customer.dto.TotalOrder; +import com.likelion.commonmodule.exception.common.ApplicationResponse; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.responses.ApiResponses; +import io.swagger.v3.oas.annotations.tags.Tag; +import lombok.RequiredArgsConstructor; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RequiredArgsConstructor +@RestController +@RequestMapping("/v1/customer") +@Validated +@Tag(name = "Customer", description = "Customer 관련 API") +public class CustomerController { + + private final CustomerFIndUseCase customerFIndUseCase; + private final CustomerSaveUseCase customerSaveUseCase; + + // 주문 전체 조회 (정후) + @GetMapping("/{storeId}") + @ApiResponses( + value = { + @ApiResponse( + responseCode = "200", + description = "주문 전체 조회", + useReturnTypeSchema = true + ) + } + ) + @Operation(summary = "주문 전체 조회 API", description = "주문 전체 조회 API 입니다.") + public ApplicationResponse> getTotalOrder( + @PathVariable Long storeId + ) { + + List orders = customerFIndUseCase.getTotalOrder(storeId); + return ApplicationResponse.ok(orders); + } + + // 접수하는 API (정후) + + // 준비 완료 시키는 API (정후) + + // 메뉴 조회 API (소연) + + // 메뉴 추가 API (소연) +} diff --git a/core-module/src/main/java/com/likelion/coremodule/customer/service/CustomerQueryService.java b/core-module/src/main/java/com/likelion/coremodule/customer/service/CustomerQueryService.java new file mode 100644 index 0000000..353ed39 --- /dev/null +++ b/core-module/src/main/java/com/likelion/coremodule/customer/service/CustomerQueryService.java @@ -0,0 +1,9 @@ +package com.likelion.coremodule.customer.service; + +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +public class CustomerQueryService { +} diff --git a/core-module/src/main/java/com/likelion/coremodule/order/domain/OrderItem.java b/core-module/src/main/java/com/likelion/coremodule/order/domain/OrderItem.java index 125a02d..08112b6 100644 --- a/core-module/src/main/java/com/likelion/coremodule/order/domain/OrderItem.java +++ b/core-module/src/main/java/com/likelion/coremodule/order/domain/OrderItem.java @@ -2,7 +2,6 @@ import com.likelion.commonmodule.exception.common.BaseEntity; import com.likelion.coremodule.menu.domain.Menu; -import com.likelion.coremodule.order.domain.Order; import jakarta.persistence.*; import lombok.*; @@ -22,7 +21,7 @@ public class OrderItem extends BaseEntity { @JoinColumn(name = "order_id") private Order order; - @ManyToOne(fetch = FetchType.LAZY) + @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "menu_id") private Menu menu;