feat(新增): 删除购物车内容,清空购物车,批量删除购物车
This commit is contained in:
parent
6fd45be415
commit
5b6a1a910c
|
@ -9,7 +9,6 @@ import io.swagger.annotations.ApiOperation;
|
|||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.List;
|
||||
|
||||
@Api(tags = "购物车相关接口")
|
||||
|
@ -37,7 +36,7 @@ public class CartApiController {
|
|||
|
||||
@ApiOperation(value = "清空购物车")
|
||||
@DeleteMapping("deleteAllCart")
|
||||
public Result<CartInfo> deleteAllCart(HttpServletRequest request) {
|
||||
public Result<CartInfo> deleteAllCart() {
|
||||
Long userId = BaseContext.getUserId();
|
||||
cartInfoService.deleteAllCart(userId);
|
||||
return Result.success();
|
||||
|
@ -45,9 +44,9 @@ public class CartApiController {
|
|||
|
||||
@ApiOperation(value = "批量删除购物车")
|
||||
@PostMapping("batchDeleteCart")
|
||||
public Result<CartInfo> batchDeleteCart(@RequestBody List<Long> skuIdList, HttpServletRequest request) {
|
||||
public Result<CartInfo> batchDeleteCart(@RequestBody List<Long> skuIdList) {
|
||||
Long userId = BaseContext.getUserId();
|
||||
cartInfoService.batchDeleteCart(userId);
|
||||
cartInfoService.batchDeleteCart(userId, skuIdList);
|
||||
return Result.success();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package com.atguigu.ssyx.cart.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface CartInfoService {
|
||||
|
||||
/**
|
||||
|
@ -30,6 +32,7 @@ public interface CartInfoService {
|
|||
* * 批量删除购物车
|
||||
*
|
||||
* @param userId 用户id
|
||||
* @param skuIdList 商品的id
|
||||
*/
|
||||
void batchDeleteCart(Long userId);
|
||||
void batchDeleteCart(Long userId, List<Long> skuIdList);
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@ import org.springframework.data.redis.core.RedisTemplate;
|
|||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class CartInfoServiceImpl implements CartInfoService {
|
||||
|
@ -90,7 +91,10 @@ public class CartInfoServiceImpl implements CartInfoService {
|
|||
*/
|
||||
@Override
|
||||
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
|
||||
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 skuIdList 商品的id
|
||||
*/
|
||||
@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()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue