joachim
joachim

Reputation: 47

Java hibernate is insterting and updating in same time

CartItemView

@PostMapping("/addToCart")
    public String addToCart(@ModelAttribute Product product,@Valid CartItem cartItem,@ModelAttribute User user){
        Cart cart = cartServices.findCartByUser(user);
        List<CartItem> cartItems = cartItemServices.findAllCartItems(cart);
        if (cartItems.size()==0) {
            cartItemServices.save(user, cartItem, product.getName());
        }
        for(int i=0;i<cartItems.size();i++) {
            CartItem cc = cartItems.get(i);
            if (product.getName().equals(cc.getProduct().getName())) {
                cartItemServices.update(user, cc, product.getName());
            }else {
                cartItemServices.save(user, cartItem, product.getName());
            }
        }

        return "redirect:/";
    }

CartItemService

public void save(User user,CartItem cartItem,String name){   //save new
    Cart cart = cartRepository.findCartByUser(user);
    Product product = productRepository.findByName(name);
    cartItem.setProduct(product);
    cartItem.setCart(cart);
    cartItem.setQuantity(1);
    cartItem.setPrice(product.getPrice());
    cartItemRepository.save(cartItem);
}
public CartItem update(User user,CartItem cartItem,String name){ //update
    Cart cart = cartRepository.findCartByUser(user);
    Product product = productRepository.findByName(name);
    cartItem.setProduct(product);
    cartItem.setCart(cart);
    cartItem.setQuantity(cartItem.getQuantity()+1);
    cartItem.setPrice(product.getPrice()*cartItem.getQuantity());
    return cartItemRepository.save(cartItem);
}

The problem is, if statement with else inside for loop.. Both of them always execute.. So if i add new item and another new item, then trying to add first item i added to cart, it create new item inside cart but also make the first item quantity +1

Upvotes: 1

Views: 69

Answers (2)

joachim
joachim

Reputation: 47

I have solved it this way

Boolean aa;
List<CartItem> cartItems = cartItemServices.findAllCartItems(cart);
for(CartItem cartItem1 : cartItems){
    if(cartItem1.getProduct().getName().equals(product.getName())){
        cartItemServices.update(user, cartItem1, product.getName());
        check=true;
        break;
    }
}
if(check==false){
    cartItemServices.save(user, cartItem, product.getName());
}

Upvotes: 1

misaki974
misaki974

Reputation: 1

I think the problem is that you're doing a loop to compare the names of your products.

If I have product A and product B in my cart, and I want to add product A again, once in the loop, I will compare product A with product A, it's the same, so it will add one Product A item. Then it will compare product B with product A (because your loop is not done), it's different, so it will add a new item Product A even if it has already been updated.

For each item with a different product name you will have a new item of your original product.

You can find a way to tell if the product is in your whole cart, instead of going through a loop.

cartItems.stream().anyMatch(item -> item.getProduct().getName().equals(product.getName()))

Upvotes: 0

Related Questions