Beispiel #1
0
  /**
   * Adds item to the cart
   *
   * @param productId
   * @param quantity
   * @param mode
   * @param model
   * @param request
   */
  @RequestMapping(value = "/add/{productid}", method = RequestMethod.PUT)
  @ResponseStatus(value = HttpStatus.NO_CONTENT)
  public void addToCart(
      @PathVariable("productid") int productId,
      @RequestParam("quantity") int quantity,
      @RequestParam("mode") String mode,
      Model model,
      HttpServletRequest request) {

    // get cart id from session
    String cartId = request.getSession(true).getId();
    Cart cart = cartService.read(cartId);

    if (cart == null) {
      cart = cartService.create(new Cart(cartId));
    }

    // get product infomation
    Product product = productService.getProductById(productId);
    if (product == null) {
      throw new ProductNotFoundException(productId, "{product.not.found}");
    }

    CartItem item = new CartItem(product);
    item.setQuantity(quantity);
    cart.addCartItem(item, mode);
    cartService.update(cartId, cart);
  }
Beispiel #2
0
  /**
   * Displays cart items of user
   *
   * @param cartId
   * @param model
   * @return
   */
  @RequestMapping(value = "/view/{id}", method = RequestMethod.GET)
  public String showCart(@PathVariable("id") String cartId, Model model) {
    model.addAttribute("cartId", cartId);
    Cart cart = cartService.read(cartId);

    if (cart == null) {
      cart = new Cart(cartId);
      cartService.create(cart);
    }
    model.addAttribute("cart", cart);
    return "cart";
  }