-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[레거시 코드 리팩토링 - 3단계] 허브(방대의) 미션 제출합니다. (#610)
* docs: ID 생성자에 대한 생각 추가 * refactor: 테이블의 상태 변경시 테이블의 주문 상태를 확인하는 로직 이벤트로 분리 * refactor: 테이블의 그룹 해제시 테이블의 주문 상태를 확인하는 로직 이벤트로 분리 * refactor: 핸들러 테스트 통합 테스트로 변경 * refactor: 공통 패키지 분리 * refactor: product 패키지 분리 * refactor: table 패키지 분리 * refactor: tablegroup 패키지 분리 * refactor: menugroup 패키지 분리 * refactor: menu 패키지 분리 * refactor: order 패키지 분리 * refactor: OrderTable, TableGroup 양방향 관계 제거 * refactor: tableGroup 메서드명 변경 * refactor: 단체 지정 해제 기능 추가 * refactor: OrderTable 도메인 이벤트 사용하도록 수정 * refactor: default 메서드 사용하도록 수정 * refactor: OrderService default 메서드 사용하도록 수정 * refactor: 검증부 도메인 서비스로 분리 * refactor: 메뉴 생성 로직 개선 * refactor: 주문 메뉴 저장 로직 수정 * test: 정상적으로 작동하지 않는 주문 메뉴 테스트 수정 * refactor: style 수정
- Loading branch information
1 parent
c2d4308
commit 2ed6c34
Showing
102 changed files
with
1,468 additions
and
794 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,10 @@ Content-Type: application/json | |
{ | ||
"menuId": 1, | ||
"quantity": 1 | ||
}, | ||
{ | ||
"menuId": 2, | ||
"quantity": 2 | ||
} | ||
] | ||
} | ||
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
51 changes: 0 additions & 51 deletions
51
src/main/java/kitchenpos/application/TableGroupService.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package kitchenpos.application.menu; | ||
|
||
import static java.util.function.Function.identity; | ||
import static java.util.stream.Collectors.toMap; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
import kitchenpos.domain.menu.Menu; | ||
import kitchenpos.domain.menu.MenuProduct; | ||
import kitchenpos.domain.product.Product; | ||
import kitchenpos.domain.product.ProductRepository; | ||
import kitchenpos.dto.menu.MenuProductRequest; | ||
import kitchenpos.dto.menu.MenuRequest; | ||
import kitchenpos.support.money.Money; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class MenuMapper { | ||
private final ProductRepository productRepository; | ||
|
||
public MenuMapper(ProductRepository productRepository) { | ||
this.productRepository = productRepository; | ||
} | ||
|
||
public Menu toMenu(MenuRequest request) { | ||
Map<Long, Product> products = getProducts(request); | ||
List<MenuProduct> menuProducts = toMenuProducts(request, products); | ||
return new Menu(request.getName(), Money.valueOf(request.getPrice()), request.getMenuGroupId(), menuProducts); | ||
} | ||
|
||
private Map<Long, Product> getProducts(MenuRequest request) { | ||
return productRepository.findAllByIdIn(request.getMenuProductIds()).stream() | ||
.collect(toMap(Product::getId, identity())); | ||
} | ||
|
||
private List<MenuProduct> toMenuProducts(MenuRequest request, Map<Long, Product> products) { | ||
return request.getMenuProducts().stream() | ||
.map(menuProduct -> toMenuProduct(menuProduct, products.get(menuProduct.getProductId()))) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private MenuProduct toMenuProduct(MenuProductRequest menuProduct, Product product) { | ||
if (product == null) { | ||
throw new IllegalArgumentException("상품이 존재하지 않습니다."); | ||
} | ||
return new MenuProduct(product.getId(), product.getName(), product.getPrice(), menuProduct.getQuantity()); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/kitchenpos/application/menu/MenuService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package kitchenpos.application.menu; | ||
|
||
import static java.util.stream.Collectors.toList; | ||
|
||
import java.util.List; | ||
import kitchenpos.domain.menu.Menu; | ||
import kitchenpos.domain.menu.MenuRepository; | ||
import kitchenpos.domain.menu.MenuValidator; | ||
import kitchenpos.dto.menu.MenuRequest; | ||
import kitchenpos.dto.menu.MenuResponse; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Transactional | ||
@Service | ||
public class MenuService { | ||
private final MenuRepository menuRepository; | ||
private final MenuMapper menuMapper; | ||
private final MenuValidator menuValidator; | ||
|
||
public MenuService(MenuRepository menuRepository, MenuMapper menuMapper, MenuValidator menuValidator) { | ||
this.menuRepository = menuRepository; | ||
this.menuMapper = menuMapper; | ||
this.menuValidator = menuValidator; | ||
} | ||
|
||
public MenuResponse create(MenuRequest request) { | ||
Menu menu = menuMapper.toMenu(request); | ||
menu.register(menuValidator); | ||
return MenuResponse.from(menuRepository.save(menu)); | ||
} | ||
|
||
@Transactional(readOnly = true) | ||
public List<MenuResponse> list() { | ||
return menuRepository.findAll().stream() | ||
.map(MenuResponse::from) | ||
.collect(toList()); | ||
} | ||
} |
10 changes: 5 additions & 5 deletions
10
...chenpos/application/MenuGroupService.java → ...plication/menugroup/MenuGroupService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/java/kitchenpos/application/order/OrderMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package kitchenpos.application.order; | ||
|
||
import static java.util.stream.Collectors.toList; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import kitchenpos.domain.menu.Menu; | ||
import kitchenpos.domain.menu.MenuProducts; | ||
import kitchenpos.domain.menu.MenuRepository; | ||
import kitchenpos.domain.order.Order; | ||
import kitchenpos.domain.order.OrderLineItem; | ||
import kitchenpos.domain.order.OrderMenu; | ||
import kitchenpos.domain.order.OrderMenuProduct; | ||
import kitchenpos.dto.order.OrderCreateRequest; | ||
import kitchenpos.dto.order.OrderLineItemRequest; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class OrderMapper { | ||
private final MenuRepository menuRepository; | ||
|
||
public OrderMapper(MenuRepository menuRepository) { | ||
this.menuRepository = menuRepository; | ||
} | ||
|
||
public Order toOrder(OrderCreateRequest request) { | ||
Long orderTableId = request.getOrderTableId(); | ||
return request.getOrderLineItems().stream() | ||
.map(this::toOrderLineItem) | ||
.collect(Collectors.collectingAndThen(toList(), items -> new Order(orderTableId, items))); | ||
} | ||
|
||
private OrderLineItem toOrderLineItem(OrderLineItemRequest request) { | ||
return new OrderLineItem(null, request.getMenuId(), request.getQuantity(), toOrderMenu(request)); | ||
} | ||
|
||
private OrderMenu toOrderMenu(OrderLineItemRequest request) { | ||
Menu menu = menuRepository.getById(request.getMenuId()); | ||
return new OrderMenu(menu.getName(), menu.getPrice(), toOrderMenuProducts(menu.getMenuProducts())); | ||
} | ||
|
||
private List<OrderMenuProduct> toOrderMenuProducts(MenuProducts menuProducts) { | ||
return menuProducts.getItems().stream() | ||
.map(item -> new OrderMenuProduct(item.getName(), item.getPrice(), item.getQuantity())) | ||
.collect(toList()); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/kitchenpos/application/order/OrderService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package kitchenpos.application.order; | ||
|
||
import static java.util.stream.Collectors.toList; | ||
|
||
import java.util.List; | ||
import kitchenpos.domain.order.Order; | ||
import kitchenpos.domain.order.OrderRepository; | ||
import kitchenpos.domain.order.OrderStatus; | ||
import kitchenpos.domain.order.OrderValidator; | ||
import kitchenpos.dto.order.OrderCreateRequest; | ||
import kitchenpos.dto.order.OrderResponse; | ||
import kitchenpos.dto.order.OrderStatusUpdateRequest; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Transactional | ||
@Service | ||
public class OrderService { | ||
private final OrderMapper orderMapper; | ||
private final OrderRepository orderRepository; | ||
private final OrderValidator orderValidator; | ||
|
||
public OrderService(OrderMapper orderMapper, OrderRepository orderRepository, OrderValidator orderValidator) { | ||
this.orderMapper = orderMapper; | ||
this.orderRepository = orderRepository; | ||
this.orderValidator = orderValidator; | ||
} | ||
|
||
public OrderResponse create(OrderCreateRequest request) { | ||
Order order = orderMapper.toOrder(request); | ||
order.place(orderValidator); | ||
return OrderResponse.from(orderRepository.save(order)); | ||
} | ||
|
||
@Transactional(readOnly = true) | ||
public List<OrderResponse> list() { | ||
return orderRepository.findAll().stream() | ||
.map(OrderResponse::from) | ||
.collect(toList()); | ||
} | ||
|
||
public OrderResponse changeOrderStatus(Long orderId, OrderStatusUpdateRequest request) { | ||
Order order = orderRepository.getById(orderId); | ||
order.changeOrderStatus(OrderStatus.valueOf(request.getOrderStatus())); | ||
return OrderResponse.from(orderRepository.save(order)); | ||
} | ||
} |
Oops, something went wrong.