Reputation: 47
Html
<form th:action="@{/deleteCartItem(id=${produkt.product.id})}" th:object="${produkty}" method="post">
<div class="text-right">
<input type="submit" value="Delete" class="btn-sm btn-danger" />
</div>
</form>
Cart controller
@GetMapping("/kosik")
public String kosik(Principal principal,Model model){
User user = userServices.findByEmail(principal.getName());
Cart cart = cartServices.findCartByUser(user);
model.addAttribute("produkty",cartItemServices.findAllCartItems(cart));
model.addAttribute("cart",cartServices.findCartByUser(user));
model.addAttribute("user",user);
return "cart";
}
Delete method
@PostMapping("/deleteCartItem")
public String deleteCartItem(@ModelAttribute CartItem cartItem){
cartItemServices.deleteCartItem(cartItem.getCartItemId());
return "redirect:/kosik";
}
Service
public void deleteCartItem(Integer id){
cartItemRepository.deleteById(id);
}
CartItem Entity
@Entity
public class CartItem {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer cartItemId;
@NotNull
@Max(11)
private int quantity;
private double price;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "product_id")
private Product product;
@ManyToOne
@JoinColumn(name = "cart_id")
private Cart cart;
Getting cartitem id works, but i cant delete cartItem.. I also tryied delete it not by id, but delete cartItem instantly..
Upvotes: 0
Views: 77
Reputation: 5093
Since your entity don't have id field as primary key. It's not working.
Define the below method in CartItemRepository and use it to delete the Object.
Long deleteByCartItemId(Long id);
Or rename the cartItemId field as id.
Upvotes: 1