Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.1 kB
2
Indexable
Never
packagecom.infy.ekart.cart.service;
importjava.util.ArrayList;
importjava.util.HashSet;
importjava.util.List;
importjava.util.Optional;
importjava.util.Set;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Service;
importorg.springframework.transaction.annotation.Transactional;
importcom.infy.ekart.cart.dto.CartProductDTO;
importcom.infy.ekart.cart.dto.CustomerCartDTO;
importcom.infy.ekart.cart.dto.ProductDTO;
importcom.infy.ekart.cart.entity.CartProduct;
importcom.infy.ekart.cart.entity.CustomerCart;
importcom.infy.ekart.cart.exception.EKartCustomerCartException;
importcom.infy.ekart.cart.repository.CartProductRepository;
importcom.infy.ekart.cart.repository.CustomerCartRepository;
@Service(value="customerCartService")
@Transactional
publicclassCustomerCartServiceImplimplementsCustomerCartService{
@Autowired
privateCustomerCartRepositorycustomerCartRepository;
@Autowired
privateCartProductRepositorycartProductRepository;
//thismethodaddsnewproductsintothecustomercart
@Override
public Integer addProductToCart(CustomerCartDTO customerCartDTO) throws
EKartCustomerCartException{
Set<CartProduct>cartProducts=newHashSet<>();
IntegercartId=null;
for(CartProductDTOcartProductDTO:customerCartDTO.getCartProducts())
{
CartProductcartProduct=newCartProduct();
cartProduct.setProductId(cartProductDTO.getProduct().getProductId());
cartProduct.setQuantity(cartProductDTO.getQuantity());
cartProducts.add(cartProduct);
}
Optional<CustomerCart> cartOptional =
customerCartRepository.findByCustomerEmailId(customerCartDTO.getCustomerEmailId());
if(cartOptional.isEmpty()){
CustomerCartnewCart=new CustomerCart();
newCart.setCustomerEmailId(customerCartDTO.getCustomerEmailId());
newCart.setCartProducts(cartProducts);
customerCartRepository.save(newCart);
cartId=newCart.getCartId();
}
else{
CustomerCartcart=cartOptional.get();
for(CartProductcartProductToBeAdded:cartProducts)
{ BooleancheckProductAlreadyPresent=false;
for(CartProductcartProductFromCart:cart.getCartProducts())