feat(新增): 查询购物车

Signed-off-by: bunny <1319900154@qq.com>
This commit is contained in:
bunny 2024-03-28 14:47:27 +08:00
parent 0e658cc8f0
commit c92f246341
4 changed files with 53 additions and 0 deletions

View File

@ -47,6 +47,10 @@ spring:
uri: lb://service-order
predicates:
- Path=/*/order/**
- id: service-cart
uri: lb://service-cart
predicates:
- Path=/*/order/cart/**
data:
redis:
host: ${bunny.redis.host}

View File

@ -1,7 +1,9 @@
package com.atguigu.cart.controller;
import com.atguigu.cart.service.CartService;
import com.atguigu.spzx.model.entity.h5.CartInfo;
import com.atguigu.spzx.model.vo.result.Result;
import com.atguigu.spzx.model.vo.result.ResultCodeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
@ -11,6 +13,8 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@Tag(name = "购物车接口")
@RestController
@RequestMapping("api/order/cart")
@ -25,4 +29,11 @@ public class CartController {
cartService.addToCart(skuId, skuNum);
return Result.success();
}
@Operation(summary = "查询购物车")
@GetMapping("auth/cartList")
public Result<List<CartInfo>> cartList() {
List<CartInfo> cartInfoList = cartService.getCartList();
return Result.build(cartInfoList, ResultCodeEnum.SUCCESS);
}
}

View File

@ -1,5 +1,9 @@
package com.atguigu.cart.service;
import com.atguigu.spzx.model.entity.h5.CartInfo;
import java.util.List;
public interface CartService {
/**
* 添加购物车
@ -8,4 +12,11 @@ public interface CartService {
* @param skuNum 商品数量
*/
void addToCart(Long skuId, Integer skuNum);
/**
* 查询购物车
*
* @return 购物车列表
*/
List<CartInfo> getCartList();
}

View File

@ -1,6 +1,7 @@
package com.atguigu.cart.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.atguigu.cart.service.CartService;
import com.atguigu.context.BaseContext;
import com.atguigu.feign.product.ProductFeignClient;
@ -11,7 +12,10 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class CartServiceImpl implements CartService {
@ -61,4 +65,27 @@ public class CartServiceImpl implements CartService {
// 将商品数据存储到购物车中
redisTemplate.opsForHash().put(cartKey, String.valueOf(skuId), JSON.toJSONString(cartInfo));
}
/**
* 查询购物车
*
* @return 购物车列表
*/
@Override
public List<CartInfo> getCartList() {
// 获取当前登录的用户信息
Long userId = BaseContext.getUserInfo().getId();
String cartKey = userId.toString();
// 获取数据
List<Object> cartInfoList = redisTemplate.opsForHash().values(cartKey);
if (!CollectionUtils.isEmpty(cartInfoList)) {
return cartInfoList.stream().map(cartInfoJSON -> JSON.parseObject(cartInfoJSON.toString(), CartInfo.class))
.sorted((o1, o2) -> o2.getCreateTime().compareTo(o1.getCreateTime()))
.collect(Collectors.toList());
}
return new ArrayList<>();
}
}