|
| 1 | +package com.baeldung.dddmodules.infrastructure.db; |
| 2 | + |
| 3 | +import com.baeldung.dddmodules.ordercontext.model.CustomerOrder; |
| 4 | +import com.baeldung.dddmodules.ordercontext.repository.CustomerOrderRepository; |
| 5 | +import com.baeldung.dddmodules.shippingcontext.model.PackageItem; |
| 6 | +import com.baeldung.dddmodules.shippingcontext.model.ShippableOrder; |
| 7 | +import com.baeldung.dddmodules.shippingcontext.repository.ShippingOrderRepository; |
| 8 | + |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Map; |
| 12 | +import java.util.Optional; |
| 13 | +import java.util.stream.Collectors; |
| 14 | + |
| 15 | +public class InMemoryOrderStore implements CustomerOrderRepository, ShippingOrderRepository { |
| 16 | + private Map<Integer, PersistenceOrder> ordersDb = new HashMap<>(); |
| 17 | + private volatile static InMemoryOrderStore instance = new InMemoryOrderStore(); |
| 18 | + |
| 19 | + @Override |
| 20 | + public void saveCustomerOrder(CustomerOrder order) { |
| 21 | + this.ordersDb.put(order.getOrderId(), new PersistenceOrder(order.getOrderId(), |
| 22 | + order.getPaymentMethod(), |
| 23 | + order.getAddress(), |
| 24 | + order |
| 25 | + .getOrderItems() |
| 26 | + .stream() |
| 27 | + .map(orderItem -> |
| 28 | + new PersistenceOrder.OrderItem(orderItem.getProductId(), |
| 29 | + orderItem.getQuantity(), |
| 30 | + orderItem.getUnitWeight(), |
| 31 | + orderItem.getUnitPrice())) |
| 32 | + .collect(Collectors.toList()) |
| 33 | + )); |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public Optional<ShippableOrder> findShippableOrder(int orderId) { |
| 38 | + if (!this.ordersDb.containsKey(orderId)) return Optional.empty(); |
| 39 | + PersistenceOrder orderRecord = this.ordersDb.get(orderId); |
| 40 | + return Optional.of( |
| 41 | + new ShippableOrder(orderRecord.orderId, orderRecord.orderItems |
| 42 | + .stream().map(orderItem -> new PackageItem(orderItem.productId, |
| 43 | + orderItem.itemWeight, |
| 44 | + orderItem.quantity * orderItem.unitPrice) |
| 45 | + ).collect(Collectors.toList()))); |
| 46 | + } |
| 47 | + |
| 48 | + public static InMemoryOrderStore provider() { |
| 49 | + return instance; |
| 50 | + } |
| 51 | + |
| 52 | + public static class PersistenceOrder { |
| 53 | + public int orderId; |
| 54 | + public String paymentMethod; |
| 55 | + public String address; |
| 56 | + public List<OrderItem> orderItems; |
| 57 | + |
| 58 | + public PersistenceOrder(int orderId, String paymentMethod, String address, List<OrderItem> orderItems) { |
| 59 | + this.orderId = orderId; |
| 60 | + this.paymentMethod = paymentMethod; |
| 61 | + this.address = address; |
| 62 | + this.orderItems = orderItems; |
| 63 | + } |
| 64 | + |
| 65 | + public static class OrderItem { |
| 66 | + public int productId; |
| 67 | + public float unitPrice; |
| 68 | + public float itemWeight; |
| 69 | + public int quantity; |
| 70 | + |
| 71 | + public OrderItem(int productId, int quantity, float unitWeight, float unitPrice) { |
| 72 | + this.itemWeight = unitWeight; |
| 73 | + this.quantity = quantity; |
| 74 | + this.unitPrice = unitPrice; |
| 75 | + this.productId = productId; |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments