Dlaczego cart.cartItems jest puste i nic mi się na stronie nie wyświetla?
Cart.jsp
<tr ng-repeat="item in cart.cartItems">
<td>{{item.product.idProdukt}}-{{item.product.nazwa}}</td>
<td>{{item.quantity}}</td>
<td>{{item.quantity}}</td>
</tr>
CartItem.java
public class CartItem {
private Produkt product;
private int quantity;
private BigDecimal totalPrice;
public CartItem() {
}
public CartItem(Produkt product) {
super();
this.product=product;
this.quantity=1;
this.totalPrice = BigDecimal.valueOf(product.Magazyn().get(0).getCena());
}
public Produkt getProduct() {
return product;
}
public void setProduct(Produkt product) {
this.product = product;
this.updateTotalPrice();
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
this.updateTotalPrice();
}
public BigDecimal getTotalPrice() {
return totalPrice;
}
public void updateTotalPrice() {
totalPrice = BigDecimal.valueOf(product.Magazyn().get(0).getCena()).multiply(new BigDecimal(this.quantity));
}
Cart.java
public class Cart {
private String cartId;
private Map<String,CartItem> cartItems;
private BigDecimal grandTotal;
public Cart(){
cartItems = new HashMap<String, CartItem>();
grandTotal = new BigDecimal(0);
}
public Cart(String cartId) {
this();
this.cartId = cartId;
}
public String getCartId() {
return cartId;
}
public void setCartId(String cartId) {
this.cartId = cartId;
}
public Map<String, CartItem> getCartItems() {
return cartItems;
}
public void setCartItems(Map<String, CartItem> cartItems) {
this.cartItems = cartItems;
}
public BigDecimal getGrandTotal() {
return grandTotal;
}
public void addCartItem(CartItem item) {
String productId = String.valueOf(item.getProduct().getIdProdukt());
if(cartItems.containsKey(productId)) {
CartItem existingCartItem = cartItems.get(productId);
existingCartItem.setQuantity(existingCartItem.getQuantity()+item.getQuantity());
cartItems.put(productId, existingCartItem);
}
else {
cartItems.put(productId, item);
}
updateGrandTotal();
}
public void updateGrandTotal() {
grandTotal = new BigDecimal(0);
for(CartItem item : cartItems.values()) {
grandTotal = grandTotal.add(item.getTotalPrice());
}
}
CartRepository.java
@Repository
public class InMemoryCartRepository implements CartRepository{
private Map<String, Cart> listOfCarts;
public InMemoryCartRepository() {
listOfCarts = new HashMap<String,Cart>();
}
public Cart create(Cart cart) {
if(listOfCarts.keySet().contains(cart.getCartId())) {
throw new IllegalArgumentException(String.format("Nie można utworzyć koszyka. Koszyk o wskazanym id (%) już istnieje.",cart.getCartId()));
}
listOfCarts.put(cart.getCartId(), cart);
return cart;
}
public Cart read(String cartId) {
return listOfCarts.get(cartId);
}
public void update(String cartId, Cart cart) {
if(!listOfCarts.keySet().contains(cartId)) {
throw new IllegalArgumentException(String.format("Nie można zaktualizować koszyka. Koszyk o wskazanym id (%) nie istnieje.",cartId));
}
listOfCarts.put(cartId, cart);
}
}
CartRestController.java
@Controller
@RequestMapping(value = "rest/cart")
public class CartRestController {
@Autowired
private CartService cartService;
@Autowired
private ProduktService productService;
@RequestMapping(method = RequestMethod.POST)
public @ResponseBody Cart create(@RequestBody Cart cart) {
return cartService.create(cart);
}
@RequestMapping(value = "/{cartId}", method = RequestMethod.GET)
public @ResponseBody Cart read(@PathVariable(value = "cartId") String cartId) {
return cartService.read(cartId);
}
@RequestMapping(value = "/{cartId}", method = RequestMethod.PUT)
@ResponseStatus(value = HttpStatus.NO_CONTENT)
public void update(@PathVariable(value = "cartId") String cartId, @RequestBody Cart cart) {
cartService.update(cartId, cart);
}
@RequestMapping(value = "/{cartId}", method = RequestMethod.DELETE)
@ResponseStatus(value = HttpStatus.NO_CONTENT)
public void delete(@PathVariable(value = "cartId") String cartId) {
cartService.delete(cartId);
}
@RequestMapping(value = "/add/{productId}", method = RequestMethod.PUT)
@ResponseStatus(value = HttpStatus.NO_CONTENT)
public void addItem(@PathVariable String productId, HttpServletRequest request) {
String sessionId = request.getSession(true).getId();
Cart cart = cartService.read(sessionId);
if(cart== null) {
cart = cartService.create(new Cart(sessionId));
}
int pom=Integer.valueOf(productId);
Produkt product = productService.findByidProdukt(pom);
cart.addCartItem(new CartItem(product));
cartService.update(sessionId, cart);
}
@RequestMapping(value = "/remove/{productId}", method = RequestMethod.PUT)
@ResponseStatus(value = HttpStatus.NO_CONTENT)
public void removeItem(@PathVariable String productId, HttpServletRequest request) {
String sessionId = request.getSession(true).getId();
Cart cart = cartService.read(sessionId);
if(cart== null) {
cart = cartService.create(new Cart(sessionId));
}
int pom=Integer.valueOf(productId);
Produkt product = productService.findByidProdukt(pom);
cart.removeCartItem(new CartItem(product));
cartService.update(sessionId, cart);
}