-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
OrderService.java
148 lines (126 loc) · 5.05 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
package org.acme.order.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.acme.order.client.PastryAPIClient;
import org.acme.order.service.model.Order;
import org.acme.order.service.model.OrderEvent;
import org.acme.order.service.model.OrderInfo;
import org.acme.order.service.model.ProductQuantity;
import org.eclipse.microprofile.rest.client.inject.RestClient;
import io.quarkus.logging.Log;
/**
* OrderService is responsible for checking business rules/constraints on
* Orders.
*
* @author laurent
*/
@ApplicationScoped
public class OrderService {
private record PastryAvailability(String pastryName, boolean available) {
}
// This is a dumb implementation of an event sourcing repository. Don't use this
// in production!
private final Map<String, List<OrderEvent>> orderEventsRepository = new HashMap<>();
@Inject
@RestClient
PastryAPIClient pastryRepository;
@Inject
OrderEventPublisher eventPublisher;
/**
* This method will check that an Order can be actually placed and persisted. A
* full implementation
* will probably check stocks, customer loyalty, payment methods, shipping
* details, etc... For sake
* of simplicity, we'll just check that products (here pastries) are all
* available.
*
* @param info The order information.
* @return A created Order with incoming info, new unique identifier and created
* status.
* @throws UnavailablePastryException
*/
public Order placeOrder(OrderInfo info) throws UnavailablePastryException {
// For all products in order, check the availability calling the Pastry API.
var availabilityFutures = info.productQuantities().stream()
.map(ProductQuantity::productName)
.map(this::checkPastryAvailability)
.toList();
// Wait for all completable futures to finish.
CompletableFuture.allOf(availabilityFutures.toArray(new CompletableFuture[availabilityFutures.size()])).join();
var unavailablePastry = availabilityFutures.stream()
.map(this::getAvailability)
.filter(availability -> !availability.available())
.findAny();
if (unavailablePastry.isPresent()) {
var pastryName = unavailablePastry.get().pastryName();
throw new UnavailablePastryException(pastryName, "Pastry %s is not available".formatted(pastryName));
}
// Everything is available! Create (and probably persist ;-) a new order.
var result = new Order();
result.setCustomerId(info.customerId());
result.setProductQuantities(info.productQuantities());
// result.setProductQuantities(List.of(new ProductQuantity("Millefeuille", 1)));
result.setTotalPrice(info.totalPrice());
// Persist and publish creation event.
var orderCreated = new OrderEvent(System.currentTimeMillis(), result, "Creation");
persistOrderEvent(orderCreated);
eventPublisher.publishOrderCreated(orderCreated);
return result;
}
/**
*
* @param reviewedOrderEvent
*/
public void updateReviewedOrder(OrderEvent reviewedOrderEvent) {
persistOrderEvent(reviewedOrderEvent);
}
/**
*
* @param id
* @return
*/
public Order getOrder(String id) throws OrderNotFoundException {
var orderEvents = getOrderEvents(id);
return orderEvents.get(orderEvents.size() - 1).order();
}
/**
*
* @param id
* @return
* @throws OrderNotFoundException
*/
public List<OrderEvent> getOrderEvents(String id) throws OrderNotFoundException {
return Optional.ofNullable(orderEventsRepository.get(id))
.orElseThrow(() -> new OrderNotFoundException(id));
}
private CompletableFuture<PastryAvailability> checkPastryAvailability(String pastryName) {
return CompletableFuture.supplyAsync(() -> {
Log.infof("Checking pastry availability for pastry %s", pastryName);
return new PastryAvailability(pastryName, "available".equals(pastryRepository.getPastry(pastryName).status()));
})
.exceptionally(e -> {
Log.errorf(e, "Got exception from Pastry client: %s", e.getMessage());
return new PastryAvailability(pastryName, false);
});
}
private void persistOrderEvent(OrderEvent event) {
var orderEvents = Optional.ofNullable(orderEventsRepository.get(event.order().getId()))
.orElseGet(ArrayList::new);
orderEvents.add(event);
orderEventsRepository.put(event.order().getId(), orderEvents);
}
private PastryAvailability getAvailability(CompletableFuture<PastryAvailability> availabilityFuture) {
try {
return availabilityFuture.get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException("Unexpected exception: " + e.getMessage());
}
}
}