feat(新增): 删除购物车内容,清空购物车,批量删除购物车

This commit is contained in:
bunny 2024-04-10 13:06:41 +08:00
parent 6fd45be415
commit 5b6a1a910c
3 changed files with 32 additions and 11 deletions

View File

@ -9,7 +9,6 @@ import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List; import java.util.List;
@Api(tags = "购物车相关接口") @Api(tags = "购物车相关接口")
@ -37,7 +36,7 @@ public class CartApiController {
@ApiOperation(value = "清空购物车") @ApiOperation(value = "清空购物车")
@DeleteMapping("deleteAllCart") @DeleteMapping("deleteAllCart")
public Result<CartInfo> deleteAllCart(HttpServletRequest request) { public Result<CartInfo> deleteAllCart() {
Long userId = BaseContext.getUserId(); Long userId = BaseContext.getUserId();
cartInfoService.deleteAllCart(userId); cartInfoService.deleteAllCart(userId);
return Result.success(); return Result.success();
@ -45,9 +44,9 @@ public class CartApiController {
@ApiOperation(value = "批量删除购物车") @ApiOperation(value = "批量删除购物车")
@PostMapping("batchDeleteCart") @PostMapping("batchDeleteCart")
public Result<CartInfo> batchDeleteCart(@RequestBody List<Long> skuIdList, HttpServletRequest request) { public Result<CartInfo> batchDeleteCart(@RequestBody List<Long> skuIdList) {
Long userId = BaseContext.getUserId(); Long userId = BaseContext.getUserId();
cartInfoService.batchDeleteCart(userId); cartInfoService.batchDeleteCart(userId, skuIdList);
return Result.success(); return Result.success();
} }
} }

View File

@ -1,5 +1,7 @@
package com.atguigu.ssyx.cart.service; package com.atguigu.ssyx.cart.service;
import java.util.List;
public interface CartInfoService { public interface CartInfoService {
/** /**
@ -29,7 +31,8 @@ public interface CartInfoService {
/** /**
* * 批量删除购物车 * * 批量删除购物车
* *
* @param userId 用户id * @param userId 用户id
* @param skuIdList 商品的id
*/ */
void batchDeleteCart(Long userId); void batchDeleteCart(Long userId, List<Long> skuIdList);
} }

View File

@ -15,6 +15,7 @@ import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Date; import java.util.Date;
import java.util.List;
@Service @Service
public class CartInfoServiceImpl implements CartInfoService { public class CartInfoServiceImpl implements CartInfoService {
@ -90,7 +91,10 @@ public class CartInfoServiceImpl implements CartInfoService {
*/ */
@Override @Override
public void deleteCart(Long skuId, Long userId) { public void deleteCart(Long skuId, Long userId) {
BoundHashOperations<String, String, CartInfo> hashOperations = redisTemplate.boundHashOps(module.getCartKey(skuId));
if (Boolean.TRUE.equals(hashOperations.hasKey(skuId.toString()))) {
hashOperations.delete(skuId.toString());
}
} }
/** /**
@ -100,16 +104,31 @@ public class CartInfoServiceImpl implements CartInfoService {
*/ */
@Override @Override
public void deleteAllCart(Long userId) { public void deleteAllCart(Long userId) {
String cartKey = module.getCartKey(userId);
BoundHashOperations<String, String, CartInfo> hashOperations = redisTemplate.boundHashOps(cartKey);
List<CartInfo> cartInfoList = hashOperations.values();
// cartInfoList 不为空
if (cartInfoList != null) {
cartInfoList.forEach(cartInfo -> {
Long skuId = cartInfo.getSkuId();
hashOperations.delete(skuId);
});
}
} }
/** /**
* * 批量删除购物车 * * 批量删除购物车
* *
* @param userId 用户id * @param userId 用户id
* @param skuIdList 商品的id
*/ */
@Override @Override
public void batchDeleteCart(Long userId) { public void batchDeleteCart(Long userId, List<Long> skuIdList) {
String cartKey = module.getCartKey(userId);
BoundHashOperations<String, String, CartInfo> hashOperations = redisTemplate.boundHashOps(cartKey);
// 如果购物车中列表不为空
if (skuIdList != null) {
skuIdList.forEach(skuId -> hashOperations.delete(skuId.toString()));
}
} }
} }