コード例 #1
0
ファイル: CartController.java プロジェクト: vsay01/WAAProject
  /**
   * 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);
  }
コード例 #2
0
ファイル: CartController.java プロジェクト: vsay01/WAAProject
  /**
   * 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";
  }
コード例 #3
0
ファイル: CartController.java プロジェクト: vsay01/WAAProject
  /**
   * 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);
    }
  }