我已经实现了一个功能,当你购买相同的产品时,购物车数量增加 1,但当我点击按钮时它增加 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>
喵喵时光机
相关分类