Пример #1
0
  @RequestMapping(path = "/updateCart", method = RequestMethod.POST)
  @ResponseBody
  public String updateCart(@RequestBody CartItem cartItem, HttpSession session) {
    String response = "";
    System.out.println(cartItem);
    // verify content
    System.out.println(cartItem.getItemId());
    System.out.println(cartItem.getItemName());
    System.out.println(cartItem.getQty());
    System.out.println(cartItem.getItemPrice());
    System.out.println(cartItem.getLinePrice());
    cartItem.setLinePrice(cartItem.getItemPrice() * cartItem.getQty());

    // response=itemId+"-"+qty;

    // update cart
    Cart cart = (Cart) session.getAttribute("CART");
    cart.addItem(cartItem);
    cart.refresh();
    Gson gson = new Gson();
    /*
    Product p=new Product();
    p.setPrice(BigDecimal.valueOf(2000.50));
    p.setName("Red Fedora Robot");
    cartItem.setItem(p);
     */
    // cartItem.setName(cartItem.getItemName());
    // cartItem.setItemPrice(itemPrice);
    session.setAttribute("CART", cart);
    // response=gson.toJson(cartItem);
    response = gson.toJson(cart);
    return response;
  }
Пример #2
0
  @RequestMapping(path = "/checkOut", method = RequestMethod.POST)
  @ResponseBody
  public String checkOut(@RequestBody Customer customer, HttpSession session) {
    String response = null;
    System.out.print("custName " + customer.getName() + "," + customer.getEmail());
    customer.setCustomerId(customer.getEmail());
    Gson gson = new Gson();
    Order order = new Order();
    // save customer
    // customer=customerService.add(customer);
    /*		List<Customer> currentCustomer=customerService.query("BY_EMAIL",customer.getCustomerId());
    		if (currentCustomer.size()==1) {

    			customer=currentCustomer.get(0);
    		}
    */
    order.setCustomer(customer);
    // hack
    order.setCustomerId(customer.getCustomerId());
    order.setOrderDate(new Date());
    order.setName("ORD_" + customer.getName());
    Cart cart = (Cart) session.getAttribute("CART");
    order.setTotalPrice(cart.getTotalPrice());
    // save customer to session
    session.setAttribute("CUST", customer);
    Iterator<CartItem> cartItems = cart.getItems().iterator();

    while (cartItems.hasNext()) {
      CartItem cartItem = cartItems.next();
      OrderItem orderItem = new OrderItem();
      orderItem.setName(cartItem.getItemName());
      Product product = new Product();
      product.setId(Long.valueOf(cartItem.getItemId()));
      orderItem.setProduct(product);
      orderItem.setQty(cartItem.getQty());
      orderItem.setLineCost(BigDecimal.valueOf(cartItem.getLinePrice()));
      orderItem.setOrder(order);
      order.getItems().add(orderItem);
      cartItems.remove();
    }
    orderService.add(order);
    // clear cart items
    // cart.setItems(new ArrayList<CartItem>());
    cart.setTotalPrice(BigDecimal.ZERO);
    session.setAttribute("CART", cart);
    return gson.toJson(customer);
  }