Skip to content

Commit

Permalink
fix: 장바구니에 아무것도 없을 때 주문 시 active 되는 현상 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
LEEJaeHyeok97 committed Nov 20, 2024
1 parent 36965a7 commit 6bc0d02
Showing 1 changed file with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public class OrderService {
private final StoreRepository storeRepository;
private final OrdersRepository ordersRepository;
private final TeamRepository teamRepository;
private final StoreTeamRepository storeTeamRepository;

@Transactional
public Message addCart(String userProviderId, AddCartRequest addCartRequest) {
Expand Down Expand Up @@ -136,9 +135,13 @@ public Message order(String userProviderId, OrderRequest orderRequest) {
}

private List<Cart> mergeCarts(List<Cart> existingCarts, List<OrderRequest.OrderItemRequest> items, User user, Store store) {
List<Long> orderedMenuIds = items.stream()
.map(OrderRequest.OrderItemRequest::menuId)
.toList();
if (existingCarts.isEmpty()) {
for (OrderRequest.OrderItemRequest item : items) {
Cart newCartAfterOrder = createNewCartAfterOrder(item, user, store);
existingCarts.add(newCartAfterOrder);
}
return existingCarts;
}

for (OrderRequest.OrderItemRequest item : items) {
Optional<Cart> existingCart = findCartByMenuId(existingCarts, item.menuId());
Expand Down Expand Up @@ -171,6 +174,22 @@ private Cart createNewCart(OrderRequest.OrderItemRequest item, User user, Store
.build();
}

private Cart createNewCartAfterOrder(OrderRequest.OrderItemRequest item, User user, Store store) {
Menu menu = menuRepository.findById(item.menuId())
.orElseThrow(() -> new IllegalArgumentException("유효하지 않은 메뉴 ID입니다."));
Cart cart = Cart.builder()
.quantity(item.quantity())
.menu(menu)
.user(user)
.store(store)
.orders(null)
.build();

cart.updateStatus(Status.INACTIVE);

return cart;
}

private Orders saveOrder(User user, Store store, Team team, OrderRequest orderRequest) {
Orders orders = Orders.builder()
.store(store)
Expand Down

0 comments on commit 6bc0d02

Please sign in to comment.