我已經(jīng)實(shí)現(xiàn)了一個(gè)功能,當(dāng)你購(gòu)買相同的產(chǎn)品時(shí),購(gòu)物車數(shù)量增加 1,但當(dāng)我點(diǎn)擊按鈕時(shí)它增加 2 而不是 1@RequestMapping(value = "/add/{movieId}", method = RequestMethod.PUT)@ResponseStatus(value = HttpStatus.NO_CONTENT)public int addItem(@PathVariable(value = "movieId") int movieId,@AuthenticationPrincipal User activeUser){ Customer customer = customerService.getCustomerByUsername(activeUser.getUsername()); Cart cart = customer.getCart(); Movie movie = movieService.getMovieById(movieId); List<CartItem> cartItems = cart.getCartItems(); for (CartItem item : cartItems) { if(movie.getMovieId()==item.getMovie().getMovieId()){ CartItem cartItem = item; cartItem.setQuantity(cartItem.getQuantity()+1); cartItem.setTotalPrice(movie.getMoviePrice()*cartItem.getQuantity()); cartItemService.addCartItem(cartItem); return 0; } } CartItem cartItem = new CartItem(); cartItem.setMovie(movie); cartItem.setQuantity(1); cartItem.setTotalPrice(movie.getMoviePrice()*cartItem.getQuantity()); cartItem.setCart(cart); cartItemService.addCartItem(cartItem); return 0;}角度部分$scope.addToCart = function (movieId){ $http.put("/rest/cart/add/"+movieId).success(function(){ alert("Movie successfully add to the cart"); });};按鈕<button class="btn btn-rose btn-round" ng-controller="cartCtrl" ng-click="addToCart('${movie.movieId}')">Add to Cart <i class="material-icons">shopping_cart</i></button>
1 回答

喵喵時(shí)光機(jī)
TA貢獻(xiàn)1846條經(jīng)驗(yàn) 獲得超7個(gè)贊
因?yàn)?code>for你試圖找到具有相同 ID 的電影而不是增加它的數(shù)量,所以你為什么要運(yùn)行cartItemService.addCartItem(cartItem)
?您應(yīng)該將其更改為更新實(shí)體,例如cartItemService.updateCartItem(cartItem)
.
添加回答
舉報(bào)
0/150
提交
取消