Skip to content

Commit

Permalink
[레거시 코드 리팩토링 - 3단계] 허브(방대의) 미션 제출합니다. (#610)
Browse files Browse the repository at this point in the history
* 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
greeng00se authored Oct 26, 2023
1 parent c2d4308 commit 2ed6c34
Show file tree
Hide file tree
Showing 102 changed files with 1,468 additions and 794 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,12 @@
## 테이블

<img width="1011" alt="image" src="https://github.com/greeng00se/greeng00se.github.io/assets/58586537/1c2a352e-bed7-4c0f-89ed-8d6d31487b9c">

## 생각

ID를 받는 생성자를 제거하는 과정에서 생긴 고민

1. 모든 필드를 지정해줄 수 있는 주생성자를 만드는 건 구현 기술에 침투적인 생성자가 생기는 것인가?

id 생성자를 제거한다면, 결국 도메인을 식별하기 위한 값을 넣으려면 jpa와 같은 기술에 의존적인 생성을 해야하는데, 오히려 제거하는 것이 구현 기술에 침투적인 생성자를 만드는 것이 아닌가?
결국 해당 도메인을 식별하기 위한 값으로 본다면, jpa를 사용하지 않았을 때 값을 지정해줘야 할텐데, 해당 부분을 위해 남겨둬야하지 않을까?
4 changes: 4 additions & 0 deletions http/orders.http
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ Content-Type: application/json
{
"menuId": 1,
"quantity": 1
},
{
"menuId": 2,
"quantity": 2
}
]
}
Expand Down
52 changes: 0 additions & 52 deletions src/main/java/kitchenpos/application/MenuService.java

This file was deleted.

60 changes: 0 additions & 60 deletions src/main/java/kitchenpos/application/OrderService.java

This file was deleted.

51 changes: 0 additions & 51 deletions src/main/java/kitchenpos/application/TableGroupService.java

This file was deleted.

49 changes: 49 additions & 0 deletions src/main/java/kitchenpos/application/menu/MenuMapper.java
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 src/main/java/kitchenpos/application/menu/MenuService.java
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());
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package kitchenpos.application;
package kitchenpos.application.menugroup;

import static java.util.stream.Collectors.toList;

import java.util.List;
import kitchenpos.dao.MenuGroupRepository;
import kitchenpos.domain.MenuGroup;
import kitchenpos.dto.MenuGroupRequest;
import kitchenpos.dto.MenuGroupResponse;
import kitchenpos.domain.menugroup.MenuGroup;
import kitchenpos.domain.menugroup.MenuGroupRepository;
import kitchenpos.dto.menugroup.MenuGroupRequest;
import kitchenpos.dto.menugroup.MenuGroupResponse;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down
47 changes: 47 additions & 0 deletions src/main/java/kitchenpos/application/order/OrderMapper.java
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 src/main/java/kitchenpos/application/order/OrderService.java
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));
}
}
Loading

0 comments on commit 2ed6c34

Please sign in to comment.