From cd1b13900e21e6b14318068be3f2d4ddbb809eab Mon Sep 17 00:00:00 2001 From: bunny <1319900154@qq.com> Date: Thu, 28 Mar 2024 14:48:51 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=96=B0=E5=A2=9E):=20=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E8=B4=AD=E7=89=A9=E8=BD=A6=E5=95=86=E5=93=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: bunny <1319900154@qq.com> --- .../cart/controller/CartController.java | 20 +++++++++++++++---- .../com/atguigu/cart/service/CartService.java | 7 +++++++ .../cart/service/impl/CartServiceImpl.java | 15 ++++++++++++++ 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/spzx-service/service-cart/src/main/java/com/atguigu/cart/controller/CartController.java b/spzx-service/service-cart/src/main/java/com/atguigu/cart/controller/CartController.java index d217a27..2dd5e43 100644 --- a/spzx-service/service-cart/src/main/java/com/atguigu/cart/controller/CartController.java +++ b/spzx-service/service-cart/src/main/java/com/atguigu/cart/controller/CartController.java @@ -8,10 +8,7 @@ import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.tags.Tag; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import java.util.List; @@ -36,4 +33,19 @@ public class CartController { List cartInfoList = cartService.getCartList(); return Result.build(cartInfoList, ResultCodeEnum.SUCCESS); } + + @Operation(summary = "删除购物车商品") + @DeleteMapping("auth/deleteCart/{skuId}") + public Result deleteCart(@Parameter(name = "skuId", description = "商品skuId", required = true) @PathVariable("skuId") Long skuId) { + cartService.deleteCart(skuId); + return Result.success(); + } + + @Operation(summary = "更新购物车商品选中状态") + @GetMapping("/auth/checkCart/{skuId}/{isChecked}") + public Result checkCart(@Parameter(name = "skuId", description = "商品skuId", required = true) @PathVariable(value = "skuId") Long skuId, + @Parameter(name = "isChecked", description = "是否选中 1:选中 0:取消选中", required = true) @PathVariable(value = "isChecked") Integer isChecked) { + cartService.checkCart(skuId, isChecked); + return Result.build(null, ResultCodeEnum.SUCCESS); + } } \ No newline at end of file diff --git a/spzx-service/service-cart/src/main/java/com/atguigu/cart/service/CartService.java b/spzx-service/service-cart/src/main/java/com/atguigu/cart/service/CartService.java index e572a18..1a422fe 100644 --- a/spzx-service/service-cart/src/main/java/com/atguigu/cart/service/CartService.java +++ b/spzx-service/service-cart/src/main/java/com/atguigu/cart/service/CartService.java @@ -19,4 +19,11 @@ public interface CartService { * @return 购物车列表 */ List getCartList(); + + /** + * 删除购物车商品 + * + * @param skuId 商品的ID值 + */ + void deleteCart(Long skuId); } diff --git a/spzx-service/service-cart/src/main/java/com/atguigu/cart/service/impl/CartServiceImpl.java b/spzx-service/service-cart/src/main/java/com/atguigu/cart/service/impl/CartServiceImpl.java index ab84d51..6ee2bf1 100644 --- a/spzx-service/service-cart/src/main/java/com/atguigu/cart/service/impl/CartServiceImpl.java +++ b/spzx-service/service-cart/src/main/java/com/atguigu/cart/service/impl/CartServiceImpl.java @@ -88,4 +88,19 @@ public class CartServiceImpl implements CartService { return new ArrayList<>(); } + + /** + * 删除购物车商品 + * + * @param skuId 商品的ID值 + */ + @Override + public void deleteCart(Long skuId) { + // 获取当前登录的用户数据 + Long userId = BaseContext.getUserInfo().getId(); + String cartKey = userId.toString(); + + // 获取缓存对象 + redisTemplate.opsForHash().delete(cartKey, String.valueOf(skuId)); + } }