diff --git a/xdclass-product-service/src/main/java/net/jieyuu/controller/CartController.java b/xdclass-product-service/src/main/java/net/jieyuu/controller/CartController.java index e8371bc..be5a379 100644 --- a/xdclass-product-service/src/main/java/net/jieyuu/controller/CartController.java +++ b/xdclass-product-service/src/main/java/net/jieyuu/controller/CartController.java @@ -41,6 +41,14 @@ public class CartController { return JsonData.buildSuccess(); } + @ApiOperation("添加到购物车") + @PostMapping("/change") + public JsonData changeItemNum(@ApiParam("购物项") @RequestBody CartItemRequest cartItemRequest) { + cartService.changeItemNum(cartItemRequest); + return JsonData.buildSuccess(); + } + + @ApiOperation("清空购物车") @DeleteMapping("/clear") @@ -55,4 +63,11 @@ public class CartController { CartVO cartVO = cartService.getMyCart(); return JsonData.buildSuccess(cartVO); } + + @ApiOperation("删除购物项") + @DeleteMapping("/delete/{product_id}") + public JsonData deleteItem(@ApiParam(value = "商品id", required = true) @PathVariable("product_id") long productId) { + cartService.deleteItem(productId); + return JsonData.buildSuccess(); + } } \ No newline at end of file diff --git a/xdclass-product-service/src/main/java/net/jieyuu/service/CartService.java b/xdclass-product-service/src/main/java/net/jieyuu/service/CartService.java index 0445c34..27b9bf9 100644 --- a/xdclass-product-service/src/main/java/net/jieyuu/service/CartService.java +++ b/xdclass-product-service/src/main/java/net/jieyuu/service/CartService.java @@ -22,4 +22,18 @@ public interface CartService { * @return */ CartVO getMyCart(); + + /** + * 删除购物项 + * + * @param productId + */ + void deleteItem(long productId); + + /** + * 修改购物车商品数量 + * + * @param cartItemRequest + */ + void changeItemNum(CartItemRequest cartItemRequest); } diff --git a/xdclass-product-service/src/main/java/net/jieyuu/service/impl/CartServiceImpl.java b/xdclass-product-service/src/main/java/net/jieyuu/service/impl/CartServiceImpl.java index fe0baec..32ce48a 100644 --- a/xdclass-product-service/src/main/java/net/jieyuu/service/impl/CartServiceImpl.java +++ b/xdclass-product-service/src/main/java/net/jieyuu/service/impl/CartServiceImpl.java @@ -8,7 +8,6 @@ import net.jieyuu.enums.BizCodeEnum; import net.jieyuu.exception.BizException; import net.jieyuu.interceptor.LoginInterceptor; import net.jieyuu.model.LoginUser; -import net.jieyuu.model.ProductDO; import net.jieyuu.request.CartItemRequest; import net.jieyuu.service.CartService; import net.jieyuu.service.ProductService; @@ -95,6 +94,32 @@ public class CartServiceImpl implements CartService { return cartVO; } + @Override + public void deleteItem(long productId) { + + BoundHashOperations myCartOps = getMyCartOps(); + myCartOps.delete(productId); + } + + /** + * 修改商品数量 + * + * @param cartItemRequest + */ + @Override + public void changeItemNum(CartItemRequest cartItemRequest) { + BoundHashOperations myCartOps = getMyCartOps(); + Object cartObj = myCartOps.get(cartItemRequest.getProductId()); + if (cartObj == null || cartItemRequest.getBuyNum() <= 0) { + throw new BizException(BizCodeEnum.CART_FILE); + } + String obj = (String) cartObj; + + CartItemVO cartItemVO = JSON.parseObject(obj, CartItemVO.class); + cartItemVO.setBuyNum(cartItemVO.getBuyNum()); + myCartOps.put(cartItemRequest.getProductId(), JSON.toJSONString(cartItemVO)); + } + /** * 获取最新购物项 *