Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

step3 (매장 식사 주문) #246

Open
wants to merge 7 commits into
base: yuhwanwoo
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,13 @@ public DeliveryOrderDto accept(final UUID orderId) {
DeliveryOrder deliveryOrder = deliveryOrderRepository.findById(orderId)
.orElseThrow(NoSuchElementException::new);
deliveryOrder.accept(kitchenridersClient);

return ConvertUtil.convert(deliveryOrder, DeliveryOrderDto.class);
}

@Transactional
public DeliveryOrderDto serve(final UUID orderId) {
DeliveryOrder deliveryOrder = deliveryOrderRepository.findById(orderId)
.orElseThrow(NoSuchElementException::new);

deliveryOrder.serve();
return ConvertUtil.convert(deliveryOrder, DeliveryOrderDto.class);
}
Expand All @@ -82,7 +80,6 @@ public DeliveryOrderDto serve(final UUID orderId) {
public DeliveryOrderDto startDelivery(final UUID orderId) {
DeliveryOrder deliveryOrder = deliveryOrderRepository.findById(orderId)
.orElseThrow(NoSuchElementException::new);

deliveryOrder.startDelivery();
return ConvertUtil.convert(deliveryOrder, DeliveryOrderDto.class);
}
Expand All @@ -91,7 +88,6 @@ public DeliveryOrderDto startDelivery(final UUID orderId) {
public DeliveryOrderDto completeDelivery(final UUID orderId) {
DeliveryOrder deliveryOrder = deliveryOrderRepository.findById(orderId)
.orElseThrow(NoSuchElementException::new);

deliveryOrder.completeDelivery();
return ConvertUtil.convert(deliveryOrder, DeliveryOrderDto.class);
}
Expand All @@ -100,7 +96,6 @@ public DeliveryOrderDto completeDelivery(final UUID orderId) {
public DeliveryOrderDto complete(final UUID orderId) {
DeliveryOrder deliveryOrder = deliveryOrderRepository.findById(orderId)
.orElseThrow(NoSuchElementException::new);

deliveryOrder.complete();
return ConvertUtil.convert(deliveryOrder, DeliveryOrderDto.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import javax.persistence.Embeddable;
import javax.persistence.ForeignKey;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import java.math.BigDecimal;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package kitchenpos.eatinorders.application;

import kitchenpos.eatinorders.tobe.domain.order.EatInMenuClient;
import kitchenpos.eatinorders.domain.OrderStatus;
import kitchenpos.eatinorders.shared.dto.EatInOrderDto;
import kitchenpos.eatinorders.tobe.domain.order.EatInOrderLineItems;
import kitchenpos.eatinorders.tobe.domain.order.EatInOrderRepository;
import kitchenpos.eatinorders.tobe.domain.ordertable.OrderTable;
import kitchenpos.eatinorders.tobe.domain.ordertable.OrderTableRepository;
import kitchenpos.eatinorders.shared.dto.EatInOrderLineItemDto;
import kitchenpos.eatinorders.shared.dto.request.EatInOrderCreateRequest;
import kitchenpos.eatinorders.tobe.domain.order.EatInOrder;
import kitchenpos.eatinorders.tobe.domain.order.EatInOrderLineItem;
import kitchenpos.eatinorders.tobe.domain.order.EatInOrderLineItemPrice;
import kitchenpos.eatinorders.tobe.domain.order.EatInOrderLineItemQuantity;
import kitchenpos.menus.tobe.domain.menu.MenuRepository;
import kitchenpos.shared.util.ConvertUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class EatInOrderService {
private final EatInOrderRepository eatInOrderRepository;
private final MenuRepository menuRepository;
private final OrderTableRepository orderTableRepository;
private final EatInMenuClient eatInMenuClient;

public EatInOrderService(EatInOrderRepository eatInOrderRepository, MenuRepository menuRepository, OrderTableRepository orderTableRepository, EatInMenuClient eatInMenuClient) {
this.eatInOrderRepository = eatInOrderRepository;
this.menuRepository = menuRepository;
this.orderTableRepository = orderTableRepository;
this.eatInMenuClient = eatInMenuClient;
}

@Transactional
public EatInOrderDto create(final EatInOrderCreateRequest request) {
List<EatInOrderLineItemDto> orderLineItemRequests = request.getOrderLineItems();
if (Objects.isNull(orderLineItemRequests) || orderLineItemRequests.isEmpty()) {
throw new IllegalArgumentException();
}

List<EatInOrderLineItem> orderLineItems = request.getOrderLineItems().stream()
.map(eatInOrderLineItemDto -> EatInOrderLineItem.of(
eatInOrderLineItemDto.getMenuId(),
new EatInOrderLineItemQuantity(eatInOrderLineItemDto.getQuantity()),
new EatInOrderLineItemPrice(eatInOrderLineItemDto.getPrice()),
eatInMenuClient
))
.collect(Collectors.toList());

OrderTable orderTable = orderTableRepository.findById(request.getOrderTableId())
.orElseThrow(NoSuchElementException::new);

EatInOrder eatInOrder = EatInOrder.of(
new EatInOrderLineItems(orderLineItems),
orderTable,
eatInMenuClient
);

return ConvertUtil.convert(eatInOrderRepository.save(eatInOrder), EatInOrderDto.class);
}

@Transactional
public EatInOrderDto accept(UUID orderId) {
EatInOrder eatInOrder = eatInOrderRepository.findById(orderId)
.orElseThrow(NoSuchElementException::new);
eatInOrder.accept();
return ConvertUtil.convert(eatInOrder, EatInOrderDto.class);
}

@Transactional
public EatInOrderDto serve(UUID orderId) {
EatInOrder eatInOrder = eatInOrderRepository.findById(orderId)
.orElseThrow(NoSuchElementException::new);
eatInOrder.serve();
return ConvertUtil.convert(eatInOrder, EatInOrderDto.class);
}

@Transactional
public EatInOrderDto complete(UUID orderId) {
EatInOrder eatInOrder = eatInOrderRepository.findById(orderId)
.orElseThrow(NoSuchElementException::new);
eatInOrder.complete();

OrderTable orderTable = orderTableRepository.findById(eatInOrder.getOrderTable().getId())
.orElseThrow(NoSuchElementException::new);
if (!eatInOrderRepository.existsByOrderTableAndStatusNot(orderTable, OrderStatus.COMPLETED)) {
orderTable.clear();
}
return ConvertUtil.convert(eatInOrder, EatInOrderDto.class);
}
Comment on lines +85 to +97
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

매장주문이 완료되었을 때 도메인 로직이 어떻게 수행되어야한다고 생각하실까요? 🤔

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

전 주문 상태 Served로 수정했습니다! 🙇

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

말씀드리고 싶었던 것은 현재 로직 중에 도메인로직으로 보이는 부분이 있어서 도메인 계층으로 위임하면 어떨까 의견을 드려본 것이였어요 😄
주문완료하는 로직에서 주문테이블과 매장주문 애그리거트간의 협력도 필요하다고 봐요.
그렇기 때문에 도메인 계층으로 위임할 때 여러 고민도 해보시면 좋을 것 같아서요

OrderTable의 추가 OrderOrderTable에 계속 쌓이며 모든 EatInOrder가 완료되면 EmptyTable이 된다.

Completed 가 되려면 전 주문은 Served 상태여야 한다.

모델링 해주신 것을 봤을 때는 지금처럼 구현한 것도 좋지만, 응용계층에서 도메인에 대한 제어가 더 많아질 것 같아요

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

말씀해주신 부분을 생각해보았는데요!
image

제 생각에는 사진 처럼
public void complete(EatInOrderRepository eatInOrderRepository)
메소드에 EatInOrderRepository를 파라미터로 받아야할 것 같은데 제가 이해한 게 맞을까요...?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

스크린샷에 올려주신 코드가 매장 주문 도메인인가요? 🤔
도메인에 레포지토리를 주입하거나 파라미터로 넘겨주는 행위는 저는 지양하고 있어요
루트 애그리거트의 핵심역할이 애그리거트의 일관성이 개지지 않다록 하는 것인데 레포지토리를 주입받거나 파라미터로 넘겨주게되면
타 애그리거트의 일관성이 깨질 우려가 있어요

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

네 매장주문 도메인인데요!
image

말씀해주신 방향으로 고민을 해봤는데, 도메인 로직으로 옮기려면
image
image
이런식으로 작성해야 하지 않나해서 repository에 대해 질문드렸었습니다!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그리고 제가 현재 고민하고 있던 부분도 이에 대한 것중 하나인데
구현하는 로직중 운영정책 등록 시 제목이 없다면 해당 정책의 Category명 + 숫자(숫자는 등록 시 마다 증가)
라는 요구사항이 존재하는데요. 해당 부분을 구현하기 위해서 고민을 해본 결과
image

Terms의 정적 메소드에 termsRepository 를 파라미터로 받도록 할 수 밖에 없을 것 같은데
말씀하신 방향대로라면 어떻게 진행하는 것이 맞는 방향인걸까요...?



@Transactional
public List<EatInOrderDto> findAll() {
return ConvertUtil.convertList(eatInOrderRepository.findAll(), EatInOrderDto.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import kitchenpos.deliveryorders.domain.KitchenridersClient;
import kitchenpos.eatinorders.domain.*;
import kitchenpos.eatinorders.tobe.domain.ordertable.OrderTable;
import kitchenpos.eatinorders.tobe.domain.ordertable.OrderTableRepository;
import kitchenpos.menus.tobe.domain.menu.Menu;
import kitchenpos.menus.tobe.domain.menu.MenuRepository;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -170,8 +172,7 @@ public Order complete(final UUID orderId) {
if (type == OrderType.EAT_IN) {
final OrderTable orderTable = order.getOrderTable();
if (!orderRepository.existsByOrderTableAndStatusNot(orderTable, OrderStatus.COMPLETED)) {
orderTable.setNumberOfGuests(0);
orderTable.setOccupied(false);
orderTable.clear();
}
}
return order;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

import kitchenpos.eatinorders.domain.OrderRepository;
import kitchenpos.eatinorders.domain.OrderStatus;
import kitchenpos.eatinorders.domain.OrderTable;
import kitchenpos.eatinorders.domain.OrderTableRepository;
import kitchenpos.eatinorders.shared.dto.request.EatInOrderTableChangeNumberOfGuestsRequest;
import kitchenpos.eatinorders.shared.dto.request.OrderTableCreateRequest;
import kitchenpos.eatinorders.tobe.domain.order.EatInOrderRepository;
import kitchenpos.eatinorders.tobe.domain.ordertable.NumberOfGuests;
import kitchenpos.eatinorders.tobe.domain.ordertable.OrderTable;
import kitchenpos.eatinorders.tobe.domain.ordertable.OrderTableName;
import kitchenpos.eatinorders.tobe.domain.ordertable.OrderTableRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -15,59 +20,47 @@
@Service
public class OrderTableService {
private final OrderTableRepository orderTableRepository;
private final OrderRepository orderRepository;
private final EatInOrderRepository eatInOrderRepository;

public OrderTableService(final OrderTableRepository orderTableRepository, final OrderRepository orderRepository) {
public OrderTableService(OrderTableRepository orderTableRepository, EatInOrderRepository eatInOrderRepository) {
this.orderTableRepository = orderTableRepository;
this.orderRepository = orderRepository;
this.eatInOrderRepository = eatInOrderRepository;
}

@Transactional
public OrderTable create(final OrderTable request) {
final String name = request.getName();
if (Objects.isNull(name) || name.isEmpty()) {
throw new IllegalArgumentException();
}
final OrderTable orderTable = new OrderTable();
orderTable.setId(UUID.randomUUID());
orderTable.setName(name);
orderTable.setNumberOfGuests(0);
orderTable.setOccupied(false);
public OrderTable create(final OrderTableCreateRequest request) {
final OrderTable orderTable = OrderTable.of(
new OrderTableName(request.getName()),
new NumberOfGuests(0),
false
);
return orderTableRepository.save(orderTable);
}

@Transactional
public OrderTable sit(final UUID orderTableId) {
final OrderTable orderTable = orderTableRepository.findById(orderTableId)
.orElseThrow(NoSuchElementException::new);
orderTable.setOccupied(true);
orderTable.sit();
return orderTable;
}

@Transactional
public OrderTable clear(final UUID orderTableId) {
final OrderTable orderTable = orderTableRepository.findById(orderTableId)
.orElseThrow(NoSuchElementException::new);
if (orderRepository.existsByOrderTableAndStatusNot(orderTable, OrderStatus.COMPLETED)) {
if (eatInOrderRepository.existsByOrderTableAndStatusNot(orderTable, OrderStatus.COMPLETED)) {
throw new IllegalStateException();
}
orderTable.setNumberOfGuests(0);
orderTable.setOccupied(false);
orderTable.clear();
return orderTable;
}

@Transactional
public OrderTable changeNumberOfGuests(final UUID orderTableId, final OrderTable request) {
final int numberOfGuests = request.getNumberOfGuests();
if (numberOfGuests < 0) {
throw new IllegalArgumentException();
}
public OrderTable changeNumberOfGuests(final UUID orderTableId, final EatInOrderTableChangeNumberOfGuestsRequest request) {
final OrderTable orderTable = orderTableRepository.findById(orderTableId)
.orElseThrow(NoSuchElementException::new);
if (!orderTable.isOccupied()) {
throw new IllegalStateException();
}
orderTable.setNumberOfGuests(numberOfGuests);
orderTable.changeNumberOfGuests(request.getNumberOfGuests());
return orderTable;
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/kitchenpos/eatinorders/domain/Order.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package kitchenpos.eatinorders.domain;

import kitchenpos.eatinorders.tobe.domain.ordertable.OrderTable;

import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package kitchenpos.eatinorders.domain;

import kitchenpos.eatinorders.tobe.domain.ordertable.OrderTable;

import java.util.List;
import java.util.Optional;
import java.util.UUID;
Expand Down
59 changes: 0 additions & 59 deletions src/main/java/kitchenpos/eatinorders/domain/OrderTable.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package kitchenpos.eatinorders.infra;

import kitchenpos.eatinorders.tobe.domain.order.EatInMenuClient;
import kitchenpos.menus.tobe.domain.menu.Menu;
import kitchenpos.menus.tobe.domain.menu.MenuRepository;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Service
public class EatInMenuClientImpl implements EatInMenuClient {

private final MenuRepository menuRepository;

public EatInMenuClientImpl(MenuRepository menuRepository) {
this.menuRepository = menuRepository;
}

@Override
public Optional<Menu> findById(UUID id) {
return menuRepository.findById(id);
}

@Override
public List<Menu> findAllByIdIn(List<UUID> ids) {
return menuRepository.findAllByIdIn(ids);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package kitchenpos.eatinorders.infra;

import kitchenpos.eatinorders.tobe.domain.order.EatInOrder;
import kitchenpos.eatinorders.tobe.domain.order.EatInOrderRepository;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.UUID;

public interface JpaEatInOrderRepositoryImpl extends EatInOrderRepository, JpaRepository<EatInOrder, UUID> {

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package kitchenpos.eatinorders.domain;
package kitchenpos.eatinorders.infra;

import kitchenpos.eatinorders.tobe.domain.ordertable.OrderTable;
import kitchenpos.eatinorders.tobe.domain.ordertable.OrderTableRepository;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.UUID;
Expand Down
Loading