Created
July 8, 2017 11:18
-
-
Save khannedy/5c1e28a6e2029091877e77547e3d3762 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.idspring.commandpattern.service.impl; | |
import com.idspring.commandpattern.entity.Cart; | |
import com.idspring.commandpattern.entity.CartItem; | |
import com.idspring.commandpattern.entity.Product; | |
import com.idspring.commandpattern.model.service.AddProductToCartRequest; | |
import com.idspring.commandpattern.model.service.CreateNewCartRequest; | |
import com.idspring.commandpattern.model.service.RemoveProductFromCartRequest; | |
import com.idspring.commandpattern.model.service.UpdateProductInCartRequest; | |
import com.idspring.commandpattern.repository.CartRepository; | |
import com.idspring.commandpattern.repository.ProductRepository; | |
import com.idspring.commandpattern.service.CartService; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.stereotype.Service; | |
import reactor.core.publisher.Mono; | |
/** | |
* @author Eko Kurniawan Khannedy | |
* @since 08/07/17 | |
*/ | |
@Service | |
public class CartServiceImpl implements CartService { | |
@Autowired | |
private CartRepository cartRepository; | |
@Autowired | |
private ProductRepository productRepository; | |
@Override | |
public Mono<Cart> createNewCart(CreateNewCartRequest request) { | |
Cart cart = newCart(request.getCartId()); | |
return cartRepository.save(cart); | |
} | |
@Override | |
public Mono<Cart> removeProductFromCart(RemoveProductFromCartRequest request) { | |
return cartRepository.findById(request.getCartId()) | |
.map(cart -> findCartItemAndRemoveIt(cart, request.getProductId())) | |
.flatMap(cart -> cartRepository.save(cart)); | |
} | |
@Override | |
public Mono<Cart> addProductToCart(AddProductToCartRequest request) { | |
return Mono.zip( | |
objects -> addOrUpdateProductInCart((Cart) objects[0], (Product) objects[1], request.getQuantity()), | |
cartRepository.findById(request.getCartId()), | |
productRepository.findById(request.getProductId()) | |
).flatMap(cart -> cartRepository.save(cart)); | |
} | |
@Override | |
public Mono<Cart> updateProductInCart(UpdateProductInCartRequest request) { | |
return cartRepository.findById(request.getCartId()) | |
.map(cart -> updateCartItemQuantity(cart, request)); | |
} | |
private Cart newCart(String id) { | |
return Cart.builder() | |
.id(id) | |
.build(); | |
} | |
private Cart findCartItemAndRemoveIt(Cart cart, String productId) { | |
CartItem cartItem = findItemInCart(cart, productId); | |
return removeItemFromCart(cart, cartItem); | |
} | |
private CartItem findItemInCart(Cart cart, String productId) { | |
return cart.getItems().stream() | |
.filter(cartItem -> cartItem.getId().equals(productId)) | |
.findFirst().get(); | |
} | |
private Cart removeItemFromCart(Cart cart, CartItem cartItem) { | |
cart.getItems().remove(cartItem); | |
return cart; | |
} | |
private Cart updateCartItemQuantity(Cart cart, UpdateProductInCartRequest request) { | |
cart.getItems().stream() | |
.filter(cartItem -> cartItem.getId().equals(request.getProductId())) | |
.forEach(cartItem -> cartItem.setQuantity(cartItem.getQuantity() + request.getQuantity())); | |
return cart; | |
} | |
private Cart addOrUpdateProductInCart(Cart cart, Product product, Integer quantity) { | |
if (isCartContainProduct(cart, product)) { | |
incrementProductQuantity(cart, product, quantity); | |
} else { | |
addNewProductToCart(cart, product, quantity); | |
} | |
return cart; | |
} | |
private boolean isCartContainProduct(Cart cart, Product product) { | |
return cart.getItems().stream() | |
.anyMatch(cartItem -> cartItem.getId().equals(product.getId())); | |
} | |
private void incrementProductQuantity(Cart cart, Product product, Integer quantity) { | |
cart.getItems().stream() | |
.filter(cartItem -> cartItem.getId().equals(product.getId())) | |
.forEach(cartItem -> cartItem.setQuantity(cartItem.getQuantity() + quantity)); | |
} | |
private void addNewProductToCart(Cart cart, Product product, Integer quantity) { | |
CartItem item = CartItem.builder() | |
.id(product.getId()) | |
.name(product.getName()) | |
.price(product.getPrice()) | |
.quantity(quantity) | |
.build(); | |
cart.getItems().add(item); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment