-
Notifications
You must be signed in to change notification settings - Fork 199
/
Copy pathOrderService.java
186 lines (175 loc) · 7.09 KB
/
OrderService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package kitchenpos.order.application;
import kitchenpos.order.infra.KitchenridersClient;
import kitchenpos.menu.domain.Menu;
import kitchenpos.menu.domain.MenuRepository;
import kitchenpos.order.domain.*;
import kitchenpos.order.domain.OrderTable;
import kitchenpos.order.domain.OrderTableRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
@Service
public class OrderService {
private final OrderRepository orderRepository;
private final MenuRepository menuRepository;
private final OrderTableRepository orderTableRepository;
private final KitchenridersClient kitchenridersClient;
public OrderService(
final OrderRepository orderRepository,
final MenuRepository menuRepository,
final OrderTableRepository orderTableRepository,
final KitchenridersClient kitchenridersClient
) {
this.orderRepository = orderRepository;
this.menuRepository = menuRepository;
this.orderTableRepository = orderTableRepository;
this.kitchenridersClient = kitchenridersClient;
}
@Transactional
public Order create(final Order request) {
final OrderType type = request.getType();
if (Objects.isNull(type)) {
throw new IllegalArgumentException();
}
final List<OrderLineItem> orderLineItemRequests = request.getOrderLineItems();
if (Objects.isNull(orderLineItemRequests) || orderLineItemRequests.isEmpty()) {
throw new IllegalArgumentException();
}
final List<Menu> menus = menuRepository.findAllByIdIn(
orderLineItemRequests.stream()
.map(OrderLineItem::getMenuId)
.collect(Collectors.toList())
);
if (menus.size() != orderLineItemRequests.size()) {
throw new IllegalArgumentException();
}
final List<OrderLineItem> orderLineItems = new ArrayList<>();
for (final OrderLineItem orderLineItemRequest : orderLineItemRequests) {
final long quantity = orderLineItemRequest.getQuantity();
if (type != OrderType.EAT_IN) {
if (quantity < 0) {
throw new IllegalArgumentException();
}
}
final Menu menu = menuRepository.findById(orderLineItemRequest.getMenuId())
.orElseThrow(NoSuchElementException::new);
if (!menu.isDisplayed()) {
throw new IllegalStateException();
}
if (menu.getPrice().compareTo(orderLineItemRequest.getPrice()) != 0) {
throw new IllegalArgumentException();
}
final OrderLineItem orderLineItem = new OrderLineItem();
orderLineItem.setMenu(menu);
orderLineItem.setQuantity(quantity);
orderLineItems.add(orderLineItem);
}
Order order = new Order();
order.setId(UUID.randomUUID());
order.setType(type);
order.setStatus(OrderStatus.WAITING);
order.setOrderDateTime(LocalDateTime.now());
order.setOrderLineItems(orderLineItems);
if (type == OrderType.DELIVERY) {
final String deliveryAddress = request.getDeliveryAddress();
if (Objects.isNull(deliveryAddress) || deliveryAddress.isEmpty()) {
throw new IllegalArgumentException();
}
order.setDeliveryAddress(deliveryAddress);
}
if (type == OrderType.EAT_IN) {
final OrderTable orderTable = orderTableRepository.findById(request.getOrderTableId())
.orElseThrow(NoSuchElementException::new);
if (!orderTable.isOccupied()) {
throw new IllegalStateException();
}
order.setOrderTable(orderTable);
}
return orderRepository.save(order);
}
@Transactional
public Order accept(final UUID orderId) {
final Order order = orderRepository.findById(orderId)
.orElseThrow(NoSuchElementException::new);
if (order.getStatus() != OrderStatus.WAITING) {
throw new IllegalStateException();
}
if (order.getType() == OrderType.DELIVERY) {
BigDecimal sum = BigDecimal.ZERO;
for (final OrderLineItem orderLineItem : order.getOrderLineItems()) {
sum = orderLineItem.getMenu()
.getPrice()
.multiply(BigDecimal.valueOf(orderLineItem.getQuantity()));
}
kitchenridersClient.requestDelivery(orderId, sum, order.getDeliveryAddress());
}
order.setStatus(OrderStatus.ACCEPTED);
return order;
}
@Transactional
public Order serve(final UUID orderId) {
final Order order = orderRepository.findById(orderId)
.orElseThrow(NoSuchElementException::new);
if (order.getStatus() != OrderStatus.ACCEPTED) {
throw new IllegalStateException();
}
order.setStatus(OrderStatus.SERVED);
return order;
}
@Transactional
public Order startDelivery(final UUID orderId) {
final Order order = orderRepository.findById(orderId)
.orElseThrow(NoSuchElementException::new);
if (order.getType() != OrderType.DELIVERY) {
throw new IllegalStateException();
}
if (order.getStatus() != OrderStatus.SERVED) {
throw new IllegalStateException();
}
order.setStatus(OrderStatus.DELIVERING);
return order;
}
@Transactional
public Order completeDelivery(final UUID orderId) {
final Order order = orderRepository.findById(orderId)
.orElseThrow(NoSuchElementException::new);
if (order.getStatus() != OrderStatus.DELIVERING) {
throw new IllegalStateException();
}
order.setStatus(OrderStatus.DELIVERED);
return order;
}
@Transactional
public Order complete(final UUID orderId) {
final Order order = orderRepository.findById(orderId)
.orElseThrow(NoSuchElementException::new);
final OrderType type = order.getType();
final OrderStatus status = order.getStatus();
if (type == OrderType.DELIVERY) {
if (status != OrderStatus.DELIVERED) {
throw new IllegalStateException();
}
}
if (type == OrderType.TAKEOUT || type == OrderType.EAT_IN) {
if (status != OrderStatus.SERVED) {
throw new IllegalStateException();
}
}
order.setStatus(OrderStatus.COMPLETED);
if (type == OrderType.EAT_IN) {
final OrderTable orderTable = order.getOrderTable();
if (!orderRepository.existsByOrderTableAndStatusNot(orderTable, OrderStatus.COMPLETED)) {
orderTable.setNumberOfGuests(0);
orderTable.setOccupied(false);
}
}
return order;
}
@Transactional(readOnly = true)
public List<Order> findAll() {
return orderRepository.findAll();
}
}