-
Notifications
You must be signed in to change notification settings - Fork 199
/
Copy pathOrderTableService.java
78 lines (69 loc) · 2.78 KB
/
OrderTableService.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
package kitchenpos.application;
import kitchenpos.domain.order.OrderRepository;
import kitchenpos.domain.order.OrderStatus;
import kitchenpos.domain.order.eatin.OrderTable;
import kitchenpos.domain.order.eatin.OrderTableRepository;
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;
@Service
public class OrderTableService {
private final OrderTableRepository orderTableRepository;
private final OrderRepository orderRepository;
public OrderTableService(final OrderTableRepository orderTableRepository, final OrderRepository orderRepository) {
this.orderTableRepository = orderTableRepository;
this.orderRepository = orderRepository;
}
@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);
return orderTableRepository.save(orderTable);
}
@Transactional
public OrderTable sit(final UUID orderTableId) {
final OrderTable orderTable = orderTableRepository.findById(orderTableId)
.orElseThrow(NoSuchElementException::new);
orderTable.setOccupied(true);
return orderTable;
}
@Transactional
public OrderTable clear(final UUID orderTableId) {
final OrderTable orderTable = orderTableRepository.findById(orderTableId)
.orElseThrow(NoSuchElementException::new);
if (orderRepository.existsByOrderTableAndStatusNot(orderTable, OrderStatus.COMPLETED)) {
throw new IllegalStateException();
}
orderTable.setNumberOfGuests(0);
orderTable.setOccupied(false);
return orderTable;
}
@Transactional
public OrderTable changeNumberOfGuests(final UUID orderTableId, final OrderTable request) {
final int numberOfGuests = request.getNumberOfGuests();
if (numberOfGuests < 0) {
throw new IllegalArgumentException();
}
final OrderTable orderTable = orderTableRepository.findById(orderTableId)
.orElseThrow(NoSuchElementException::new);
if (!orderTable.isOccupied()) {
throw new IllegalStateException();
}
orderTable.setNumberOfGuests(numberOfGuests);
return orderTable;
}
@Transactional(readOnly = true)
public List<OrderTable> findAll() {
return orderTableRepository.findAll();
}
}