Esempio n. 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);
  }
Esempio n. 2
0
  /**
   * Removes item from cart
   *
   * @param productId
   * @param model
   * @param request
   */
  @RequestMapping(value = "/remove/{productid}", method = RequestMethod.DELETE)
  @ResponseStatus(value = HttpStatus.NO_CONTENT)
  public void removeFromCart(
      @PathVariable("productid") int productId, Model model, HttpServletRequest request) {

    String cartId = request.getSession().getId();
    Cart cart = cartService.read(cartId);
    if (cart != null) {
      Product product = productService.getProductById(productId);
      if (product == null) {
        throw new ProductNotFoundException(productId, "{product.not.found}");
      }

      cart.removeCartItem(new CartItem(product));
      cartService.update(cartId, cart);
    }
  }