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
  • when "The method was not called" does the console log have "The fucking cart size:"? could you doubly check whether the request is sent (see browser network tab)? so we can be sure its backend issue not front end one. – Bagus Tesa Commented Nov 22, 2024 at 23:59
  • For the first 2 items in the cart, "The fucking cart size:" was shown in the console. I'm pretty sure that the front is fine. – Tuong Nguyen Commented Nov 23, 2024 at 0:04
  • the easiest suspect would be 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
Add a comment  | 

1 Answer 1

Reset to default 0

The 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