admin管理员组文章数量:1122832
i tried to delete the last item from the cart. For all the previous item, the @GetMapping("remove-song-from-cart") work and return me to the cart page again but when hit the delete button of the last item. The method was not called. I think the problem is from the the get method remove-song-from-cart
package musicstore.musicselling.Entity;
import java.util.HashSet;
import java.util.Set;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
@Entity
public class Cart {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long cartId;
@OneToMany(mappedBy = "cart", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
private Set<CartItem> cartItems = new HashSet<>();
public Long getCartId() {
return cartId;
}
public Set<CartItem> getCartItems() {
return cartItems;
}
public void setCartItems(Set<CartItem> cartItems) {
this.cartItems = cartItems;
}
public void addCartItems(CartItem item) {
cartItems.add(item);
item.setCart(this);
}
public void removeCartItems(CartItem item) {
cartItems.remove(item);
item.setCart(null);
}
}
package musicstore.musicselling.Entity;
import jakarta.annotation.Generated;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
@Entity
public class CartItem {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long itemId;
@ManyToOne
@JoinColumn(name = "cartId")
private Cart cart;
@ManyToOne
@JoinColumn(name = "songId")
private Song song;
@ManyToOne
@JoinColumn(name = "albumId")
private Album album;
public CartItem() {
}
public Long getItemId() {
return itemId;
}
public Song getSong() {
return song;
}
public void setSong(Song song) {
this.song = song;
}
public Album getAlbum() {
return album;
}
public void setAlbum(Album album) {
this.album = album;
}
public Cart getCart() {
return cart;
}
public void setCart(Cart cart) {
this.cart = cart;
}
}
package musicstore.musicselling.Controller;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.ui.Model;
import musicstore.musicselling.Entity.*;
import musicstore.musicselling.Repository.AlbumRepository;
import musicstore.musicselling.Repository.CartItemRepository;
import musicstore.musicselling.Repository.CartRepository;
import musicstore.musicselling.Repository.SongRepository;
@Controller
public class CartController {
@Autowired
SongRepository songRepository;
@Autowired
AlbumRepository albumRepository;
@Autowired
CartRepository cartRepository;
@Autowired
private CartItemRepository cartItemRepository;
private Cart getOrCreateCart() {
if (cartRepository.findByCartId(1L) == null) {
Cart cart = new Cart();
return cartRepository.save(cart);
}
return cartRepository.findByCartId(1L);
}
// add the song to the cart
@GetMapping("/add-song-to-cart")
public String addSongToCart(@RequestParam Long songId) {
Cart cart = getOrCreateCart();
Song song = songRepository.findBySongId(songId);
CartItem item = new CartItem();
item.setSong(song);
cart.addCartItems(item);
cartRepository.save(cart);
System.out.println("Add success");
return "redirect:/song-list";
}
// push the data of cart list to the cart page
@GetMapping("/cart")
public String songCart(Model model) {
Cart cart = getOrCreateCart();
double price = 0;
List<Song> songItems = new ArrayList<>();
for (CartItem cartItem : cart.getCartItems()) {
if (cartItem.getSong() != null) {
songItems.add(cartItem.getSong());
price = price + cartItem.getSong().getSongPrice();
}
}
String totalPrice = String.format("%.2f", price);
model.addAttribute("totalPrice", totalPrice);
model.addAttribute("songItems", songItems);
System.out.println("cart item size: " + cart.getCartItems().size());
for (CartItem cartItem : cart.getCartItems()) {
System.out.println("Song id: " + cartItem.getSong().getSongId());
}
return "cart";
}
// remove the song from cart
@GetMapping("remove-song-from-cart")
public String removeSongFromCart(@RequestParam Long songId) {
Cart cart = getOrCreateCart();
Set<CartItem> cartList = cart.getCartItems();
System.out.println("The fucking cart size: " + cartList.size());
CartItem itemToRemove = null;
for (CartItem item : cartList) {
if (item.getSong().getSongId().equals(songId)) {
itemToRemove = item;
}
}
if (itemToRemove != null) {
cart.removeCartItems(itemToRemove);
cartRepository.save(cart);
}
System.out.println("item to remove: " +
itemToRemove.getSong().getSongName());
return "redirect:/cart";
}
}
i tried to delete the last item from the cart. For all the previous item, the @GetMapping("remove-song-from-cart") work and return me to the cart page again but when hit the delete button of the last item. The method was not called. I think the problem is from the the get method remove-song-from-cart
package musicstore.musicselling.Entity;
import java.util.HashSet;
import java.util.Set;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
@Entity
public class Cart {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long cartId;
@OneToMany(mappedBy = "cart", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
private Set<CartItem> cartItems = new HashSet<>();
public Long getCartId() {
return cartId;
}
public Set<CartItem> getCartItems() {
return cartItems;
}
public void setCartItems(Set<CartItem> cartItems) {
this.cartItems = cartItems;
}
public void addCartItems(CartItem item) {
cartItems.add(item);
item.setCart(this);
}
public void removeCartItems(CartItem item) {
cartItems.remove(item);
item.setCart(null);
}
}
package musicstore.musicselling.Entity;
import jakarta.annotation.Generated;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
@Entity
public class CartItem {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long itemId;
@ManyToOne
@JoinColumn(name = "cartId")
private Cart cart;
@ManyToOne
@JoinColumn(name = "songId")
private Song song;
@ManyToOne
@JoinColumn(name = "albumId")
private Album album;
public CartItem() {
}
public Long getItemId() {
return itemId;
}
public Song getSong() {
return song;
}
public void setSong(Song song) {
this.song = song;
}
public Album getAlbum() {
return album;
}
public void setAlbum(Album album) {
this.album = album;
}
public Cart getCart() {
return cart;
}
public void setCart(Cart cart) {
this.cart = cart;
}
}
package musicstore.musicselling.Controller;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.ui.Model;
import musicstore.musicselling.Entity.*;
import musicstore.musicselling.Repository.AlbumRepository;
import musicstore.musicselling.Repository.CartItemRepository;
import musicstore.musicselling.Repository.CartRepository;
import musicstore.musicselling.Repository.SongRepository;
@Controller
public class CartController {
@Autowired
SongRepository songRepository;
@Autowired
AlbumRepository albumRepository;
@Autowired
CartRepository cartRepository;
@Autowired
private CartItemRepository cartItemRepository;
private Cart getOrCreateCart() {
if (cartRepository.findByCartId(1L) == null) {
Cart cart = new Cart();
return cartRepository.save(cart);
}
return cartRepository.findByCartId(1L);
}
// add the song to the cart
@GetMapping("/add-song-to-cart")
public String addSongToCart(@RequestParam Long songId) {
Cart cart = getOrCreateCart();
Song song = songRepository.findBySongId(songId);
CartItem item = new CartItem();
item.setSong(song);
cart.addCartItems(item);
cartRepository.save(cart);
System.out.println("Add success");
return "redirect:/song-list";
}
// push the data of cart list to the cart page
@GetMapping("/cart")
public String songCart(Model model) {
Cart cart = getOrCreateCart();
double price = 0;
List<Song> songItems = new ArrayList<>();
for (CartItem cartItem : cart.getCartItems()) {
if (cartItem.getSong() != null) {
songItems.add(cartItem.getSong());
price = price + cartItem.getSong().getSongPrice();
}
}
String totalPrice = String.format("%.2f", price);
model.addAttribute("totalPrice", totalPrice);
model.addAttribute("songItems", songItems);
System.out.println("cart item size: " + cart.getCartItems().size());
for (CartItem cartItem : cart.getCartItems()) {
System.out.println("Song id: " + cartItem.getSong().getSongId());
}
return "cart";
}
// remove the song from cart
@GetMapping("remove-song-from-cart")
public String removeSongFromCart(@RequestParam Long songId) {
Cart cart = getOrCreateCart();
Set<CartItem> cartList = cart.getCartItems();
System.out.println("The fucking cart size: " + cartList.size());
CartItem itemToRemove = null;
for (CartItem item : cartList) {
if (item.getSong().getSongId().equals(songId)) {
itemToRemove = item;
}
}
if (itemToRemove != null) {
cart.removeCartItems(itemToRemove);
cartRepository.save(cart);
}
System.out.println("item to remove: " +
itemToRemove.getSong().getSongName());
return "redirect:/cart";
}
}
Share
Improve this question
edited Nov 23, 2024 at 0:03
Tuong Nguyen
asked Nov 22, 2024 at 23:45
Tuong NguyenTuong Nguyen
212 bronze badges
3
|
1 Answer
Reset to default 0The possible solution could be
if (itemToRemove != null) {
cart.getCartItems().remove(itemToRemove);
cartRepository.save(cart);
}
because may be your database may not be synchronised,So directly fetching from database then deleting may work.
本文标签: javaShopping cart deletetion bugStack Overflow
版权声明:本文标题:java - Shopping cart deletetion bug - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736300292a1930762.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
item.getSong().getSongId().equals(songId)
but, we can't be sure until we check the parameter sent by frontend and the stuffs in the database. – Bagus Tesa Commented Nov 23, 2024 at 3:46