private CartProduct getCartProduct( ProductAddedToCustomerCart productAddedToCustomerCart, CustomerCart customerCart) { UUID productId = productAddedToCustomerCart.getProductId(); Optional<CartProduct> cartProduct = customerCart .getProducts() .stream() .filter(p -> p.getProductId().equals(productId)) .findFirst(); CartProduct productToAdd; if (cartProduct.isPresent()) { productToAdd = cartProduct.get(); int newQuantity = productToAdd.getQuantity() + 1; productToAdd.setQuantity(newQuantity); } else { productToAdd = new CartProduct(); productToAdd.setProductId(productId); productToAdd.setQuantity(1); } Product productDeatils = this.productRepository.getProduct(productId); if (productDeatils != null) { productToAdd.setDescription(productDeatils.getDescription()); productToAdd.setCost(productDeatils.getCost()); productToAdd.setName(productDeatils.getName()); } return productToAdd; }
public void handle(ProductAddedToCustomerCart productAddedToCustomerCart) { CustomerCart customerCart = customerCartRepository.getCustomerCart(productAddedToCustomerCart.getCartId()); if (customerCart == null) { customerCart = new CustomerCart(); customerCart.setCustomerCartId(productAddedToCustomerCart.getCartId()); } // Deal with Product Addition CartProduct productToAdd = getCartProduct(productAddedToCustomerCart, customerCart); // Remove existing version (if it exists) and add new version customerCart .getProducts() .removeIf(p -> p.getProductId().equals(productAddedToCustomerCart.getProductId())); customerCart.getProducts().add(productToAdd); customerCart.setCorrelationId(productAddedToCustomerCart.getCorrelationId()); customerCart.setUpdatedAt(productAddedToCustomerCart.getUpdateDateTime()); // Store customerCartRepository.storeCustomerCart(customerCart); }