Skip to content

Commit

Permalink
Add CreateOrderUsecase Class
Browse files Browse the repository at this point in the history
  • Loading branch information
Rebwon committed Dec 5, 2023
1 parent a0ea45c commit fa07806
Showing 5 changed files with 51 additions and 79 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package kr.flab.movieon.order.application;

import kr.flab.movieon.order.application.command.CreateOrderCommand;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

@Service
public final class CreateOrderUsecase {

private final OrderCommandExecutor executor;
private final TransactionTemplate transactionTemplate;
private final ApplicationEventPublisher publisher;

public CreateOrderUsecase(OrderCommandExecutor executor,
TransactionTemplate transactionTemplate,
ApplicationEventPublisher publisher) {
this.executor = executor;
this.transactionTemplate = transactionTemplate;
this.publisher = publisher;
}

public String create(String accountId, CreateOrderCommand command) {
var order = transactionTemplate.execute(status ->
executor.create(accountId, command)
);
order.pollAllEvents().forEach(publisher::publishEvent);
return order.getOrderKey();
}
}
Original file line number Diff line number Diff line change
@@ -10,49 +10,38 @@
import kr.flab.movieon.order.domain.OrderLineItem;
import kr.flab.movieon.order.domain.OrderRepository;
import kr.flab.movieon.order.domain.OrderValidator;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.stereotype.Component;

@Service
@Component
public final class OrderCommandExecutor {

private final OrderRepository orderRepository;
private final OrderValidator orderValidator;
private final TransactionTemplate transactionTemplate;
private final ApplicationEventPublisher publisher;

public OrderCommandExecutor(OrderRepository orderRepository,
OrderValidator orderValidator,
TransactionTemplate transactionTemplate,
ApplicationEventPublisher publisher) {
OrderValidator orderValidator) {
this.orderRepository = orderRepository;
this.orderValidator = orderValidator;
this.transactionTemplate = transactionTemplate;
this.publisher = publisher;
}

public String create(String accountId, CreateOrderCommand command) {
var order = transactionTemplate.execute(status -> {
var entity = Order.create(new Customer(accountId), command.payMethod(),
command.useOfPoint(), mapFrom(command.lineItems()));
orderValidator.validate(entity);
return orderRepository.save(entity);
});
order.pollAllEvents().forEach(publisher::publishEvent);
return order.getOrderKey();
public Order create(String accountId, CreateOrderCommand command) {
var order = Order.create(new Customer(accountId), command.payMethod(),
command.useOfPoint(), mapFrom(command.lineItems()));
orderValidator.validate(order);
orderRepository.save(order);
return order;
}

private List<OrderLineItem> mapFrom(List<CreateOrderLineItemCommand> items) {
return items.stream()
.map(p -> new OrderLineItem(p.itemId(), p.productName(), p.basePrice(),
mapFromOption(p.options())))
.toList();
.map(p -> new OrderLineItem(p.itemId(), p.productName(), p.basePrice(),
mapFromOption(p.options())))
.toList();
}

private List<OrderItemOption> mapFromOption(List<CreateOrderItemOptionCommand> options) {
return options.stream()
.map(o -> new OrderItemOption(o.optionName(), o.salesPrice()))
.toList();
.map(o -> new OrderItemOption(o.optionName(), o.salesPrice()))
.toList();
}
}
Original file line number Diff line number Diff line change
@@ -19,13 +19,13 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

final class CreateOrderCommandHandlerTest {
final class OrderCommandExecutorTest {

@Test
@DisplayName("주문 생성 명령을 처리하고 이벤트가 등록된다.")
void sut_create_order_command_handle() {
// Arrange
var sut = new CreateOrderCommandHandler(new DummyOrderRepository(),
var sut = new OrderCommandExecutor(new DummyOrderRepository(),
new OrderValidator(new ItemRepositoryStub(), new DummyPointManager()));

// Act
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@
import java.net.URI;
import kr.flab.movieon.common.AuthenticatedUser;
import kr.flab.movieon.common.result.ApiResponseEnvelop;
import kr.flab.movieon.order.application.OrderCommandExecutor;
import kr.flab.movieon.order.application.CreateOrderUsecase;
import kr.flab.movieon.order.presentation.request.CreateOrderRequest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
@@ -12,19 +12,19 @@
@RestController
public final class OrderApi implements OrderSpecification {

private final OrderCommandExecutor orderCommandExecutor;
private final CreateOrderUsecase createOrderUsecase;
private final String baseUrl;

public OrderApi(OrderCommandExecutor orderCommandExecutor,
public OrderApi(CreateOrderUsecase createOrderUsecase,
@Value("${app.url}") String baseUrl) {
this.orderCommandExecutor = orderCommandExecutor;
this.createOrderUsecase = createOrderUsecase;
this.baseUrl = baseUrl;
}

@Override
public ResponseEntity<ApiResponseEnvelop<URI>> create(CreateOrderRequest request,
AuthenticatedUser user) {
var info = orderCommandExecutor.create(user.id(), request.toCommand());
var info = createOrderUsecase.create(user.id(), request.toCommand());
URI uri = URI.create(baseUrl + "/api/v1/orders/" + info);
return ResponseEntity.ok(ApiResponseEnvelop.success(uri));
}

0 comments on commit fa07806

Please sign in to comment.