-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathOrderService.java
59 lines (50 loc) · 2.26 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
package com.mayank.fooddelivery.services;
import com.mayank.fooddelivery.commands.OrderCommandExecutor;
import com.mayank.fooddelivery.datastore.OrderData;
import com.mayank.fooddelivery.exceptions.ExceptionType;
import com.mayank.fooddelivery.exceptions.FoodDeliveryException;
import com.mayank.fooddelivery.model.Order;
import com.mayank.fooddelivery.model.OrderCommandType;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
@Service
public class OrderService {
private OrderData orderData;
private List<OrderCommandExecutor> orderCommandExecutorList;
@Autowired
public OrderService(OrderData orderData, List<OrderCommandExecutor> orderCommandExecutorList) {
this.orderData = orderData;
this.orderCommandExecutorList = orderCommandExecutorList;
}
public void updateOrder(@NonNull final Order order, @NonNull final OrderCommandType orderCommandType) {
for (OrderCommandExecutor orderCommandExecutor : orderCommandExecutorList) {
if (orderCommandExecutor.isApplicable(orderCommandType)) {
orderCommandExecutor.execute(order);
}
}
}
public Order getOrderById(@NonNull final String orderId) {
if (!orderData.getOrderById().containsKey(orderId)) {
throw new FoodDeliveryException(ExceptionType.ORDER_NOT_FOUND, "order not found");
}
return orderData.getOrderById().get(orderId);
}
public List<Order> getAllOrdersByRestaurantId(@NonNull final String userId, @NonNull final String restaurantId) {
List<Order> orderList = getAllOrders(userId);
return orderList.stream().filter(order -> order.getRestaurantId()
.equals(restaurantId)).collect(Collectors.toList());
}
public List<Order> getAllOrders(@NonNull final String userId) {
List<Order> orderList = new ArrayList<>();
List<String> orderIds = orderData.getOrderIdsByUserId().get(userId);
if (Optional.of(orderIds).isPresent()) {
orderIds.forEach(orderId -> orderList.add(getOrderById(orderId)));
}
return orderList;
}
}