@RequestMapping(value = "/{user}/addItem", method = RequestMethod.POST) public @ResponseBody Cart add(@PathVariable String user, @RequestBody Item item) { CartBO cartBO = cartBODao.getCart(user); if (cartBO == null) { cartBO = cartBODao.addCart(user); } item.setUser(user); item.setCartId(cartBO.getId()); Product product = getProductsDao().getProduct(item.getProductId()); item.setName(product.getName()); Item dbItem = getItemDao().getItem(item); if (dbItem == null) { item.setTotal(new BigDecimal(product.getPrice().doubleValue() * item.getQuantity())); getItemDao().save(item); } else { item.setQuantity(dbItem.getQuantity() + item.getQuantity()); item.setTotal(item.getTotal().add(dbItem.getTotal())); getItemDao().update(item); } return preparCart(item); }
@RequestMapping(value = "/{user}/removeItem", method = RequestMethod.POST) public @ResponseBody Cart removeItem(@PathVariable String user, @RequestBody String id) { CartBO cartBO = cartBODao.getCart(user); Item item = new Item(); item.setProductId(id); item.setUser(user); item.setCartId(cartBO.getId()); getItemDao().remove(item); return preparCart(item); }
@RequestMapping(value = "/{user}/updateItem", method = RequestMethod.PUT) public @ResponseBody Cart update(@PathVariable String user, @RequestBody Item item) { item.setUser(user); CartBO cartBO = cartBODao.getCart(user); item.setCartId(cartBO.getId()); Product product = getProductsDao().getProduct(item.getProductId()); item.setTotal(new BigDecimal(product.getPrice().doubleValue() * item.getQuantity())); getItemDao().update(item); return preparCart(item); }
@RequestMapping(value = "/{user}/getCart", method = RequestMethod.GET) public @ResponseBody Cart getCart(@PathVariable String user) { CartBO cartBO = cartBODao.getCart(user); if (cartBO == null) { return new Cart(); } Item item = new Item(); item.setUser(user); item.setCartId(cartBO.getId()); return preparCart(item); }
@RequestMapping(value = "/{user}/cancelCart", method = RequestMethod.DELETE) public @ResponseBody Cart cancelCart(@PathVariable String user) { CartBO cartBO = cartBODao.getCart(user); Item item = new Item(); item.setUser(user); item.setCartId(cartBO.getId()); List<Item> items = itemDao.getAllItem(item); for (Item itemX : items) { getItemDao().remove(itemX); } cartBODao.deleteCart(user); return new Cart(); }