Skip to content

Commit

Permalink
fix: customer find usecase
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeongh00 committed Aug 6, 2024
1 parent 0627276 commit 55baef3
Showing 1 changed file with 29 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import com.likelion.apimodule.customer.dto.TotalOrder;
import com.likelion.apimodule.order.dto.MenuOrder;
import com.likelion.coremodule.VisitList.domain.VisitList;
import com.likelion.coremodule.VisitList.exception.VisitErrorCode;
import com.likelion.coremodule.VisitList.exception.VisitException;
import com.likelion.coremodule.VisitList.service.VisitListQueryService;
import com.likelion.coremodule.menu.domain.Menu;
import com.likelion.coremodule.menu.service.MenuQueryService;
Expand All @@ -16,8 +14,9 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
@RequiredArgsConstructor
Expand All @@ -30,13 +29,11 @@ public class CustomerFIndUseCase {

public List<TotalOrder> getTotalOrder(Long storeId) {

// Fetch menus by storeId
List<Menu> menus = menuQueryService.findMenusByStoreId(storeId);
if (menus == null || menus.isEmpty()) {
return Collections.emptyList(); // No menus found, return empty list
return Collections.emptyList();
}

// Fetch all order items for the fetched menus
List<OrderItem> items = new ArrayList<>();
for (Menu menu : menus) {
List<OrderItem> item = orderQueryService.findOrderItemsByMenuId(menu.getId());
Expand All @@ -45,49 +42,42 @@ public List<TotalOrder> getTotalOrder(Long storeId) {
}
}

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

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

for (Map.Entry<Long, List<OrderItem>> entry : groupedItems.entrySet()) {
List<OrderItem> orderItems = entry.getValue();
if (orderItems.isEmpty()) {
continue;
}
for (Order order : orders) {

Order order = orderQueryService.findOrderById(orderItems.get(0).getOrder().getId());
if (order == null) {
continue;
}
List<OrderItem> orderItems = orderQueryService.findOrderItemByOrderId(order.getId());

User user = userQueryService.findById(order.getUser().getUserId());
if (user == null) {
continue;
}

VisitList visitList = visitListQueryService.findVisitListByStoreIdAndUserId(storeId, user.getUserId());
if (visitList == null) {
throw new VisitException(VisitErrorCode.NO_VISIT_LIST_INFO);
continue;
}

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();
List<Menu> menuList = new ArrayList<>();
List<MenuOrder> menuOrders = new ArrayList<>();
int price = 0;

for (OrderItem item : orderItems) {
Menu menu = menuQueryService.findMenuById(item.getMenu().getId());
menuList.add(menu);

price += menu.getPrice() * item.getQuantity();

MenuOrder menuOrder = new MenuOrder(
menu.getName(),
menu.getImageUrl(),
item.getQuantity(),
menu.getPrice()
);
menuOrders.add(menuOrder);
}

TotalOrder totalOrder = new TotalOrder(
order.getPickUpRoute(),
Expand All @@ -99,7 +89,7 @@ public List<TotalOrder> getTotalOrder(Long storeId) {
order.getId(),
order.getOrderNum(),
menuOrders,
totalPrice
price
);

totalOrders.add(totalOrder);
Expand Down

0 comments on commit 55baef3

Please sign in to comment.