Skip to content

Commit

Permalink
feat: 주문 전체 조회 API
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeongh00 committed Aug 4, 2024
1 parent 5729508 commit cc4feda
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<TotalOrder> getTotalOrder(Long storeId) {

List<Menu> menus = menuQueryService.findMenusByStoreId(storeId);
List<OrderItem> items = new ArrayList<>();
for (Menu menu : menus) {
List<OrderItem> item = orderQueryService.findOrderItemsByMenuId(menu.getId());
items.addAll(item);
}

Map<Long, List<OrderItem>> groupedItems = new HashMap<>();
for (OrderItem item : items) {
groupedItems.computeIfAbsent(item.getOrder().getId(), k -> new ArrayList<>()).add(item);
}

List<TotalOrder> totalOrders = new ArrayList<>();

for (Map.Entry<Long, List<OrderItem>> entry : groupedItems.entrySet()) {

List<OrderItem> orderItems = entry.getValue();
Order order = orderQueryService.findOrderById(orderItems.get(0).getOrder().getId());
User user = userQueryService.findById(order.getUser().getUserId());

List<MenuOrder> 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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.likelion.apimodule.customer.application;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class CustomerSaveUseCase {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.likelion.apimodule.customer.dto;

public record OrderMenu(
Long menuId,
String menuName
) {
}
Original file line number Diff line number Diff line change
@@ -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<MenuOrder> menu,
Integer totalPrice
) {
}
Original file line number Diff line number Diff line change
@@ -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<List<TotalOrder>> getTotalOrder(
@PathVariable Long storeId
) {

List<TotalOrder> orders = customerFIndUseCase.getTotalOrder(storeId);
return ApplicationResponse.ok(orders);
}

// 접수하는 API (정후)

// 준비 완료 시키는 API (정후)

// 메뉴 조회 API (소연)

// 메뉴 추가 API (소연)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.likelion.coremodule.customer.service;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class CustomerQueryService {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand All @@ -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;

Expand Down

0 comments on commit cc4feda

Please sign in to comment.