mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2025-01-19 03:30:06 +08:00
REVIEW 商品收藏的逻辑 v2
This commit is contained in:
parent
0ffa7ff4c8
commit
467811a42e
@ -26,29 +26,27 @@ public class AppFavoriteController {
|
||||
|
||||
@PostMapping(value = "/create")
|
||||
@Operation(summary = "商品收藏")
|
||||
public CommonResult<Boolean> create(@RequestBody @Valid AppFavoriteReqVO reqVO) {
|
||||
Long loginUserId = getLoginUserId();
|
||||
return success(productFavoriteService.create(loginUserId, reqVO));
|
||||
public CommonResult<Boolean> createFavorite(@RequestBody @Valid AppFavoriteReqVO reqVO) {
|
||||
return success(productFavoriteService.createFavorite(getLoginUserId(), reqVO));
|
||||
}
|
||||
|
||||
@DeleteMapping(value = "/delete")
|
||||
@Operation(summary = "取消商品收藏(通过商品详情)")
|
||||
public CommonResult<Boolean> delete(@RequestBody @Valid AppFavoriteReqVO reqVO) {
|
||||
Long loginUserId = getLoginUserId();
|
||||
return success(productFavoriteService.delete(loginUserId,reqVO));
|
||||
@Operation(summary = "取消商品收藏")
|
||||
public CommonResult<Boolean> deleteFavorite(@RequestBody @Valid AppFavoriteReqVO reqVO) {
|
||||
return success(productFavoriteService.deleteFavorite(getLoginUserId(), reqVO));
|
||||
}
|
||||
|
||||
@GetMapping(value = "/page")
|
||||
@Operation(summary = "分页获取商品收藏列表")
|
||||
public CommonResult<PageResult<AppFavoriteRespVO>> page(AppFavoritePageReqVO reqVO) {
|
||||
Long userId = getLoginUserId();
|
||||
return success(productFavoriteService.page(userId,reqVO));
|
||||
public CommonResult<PageResult<AppFavoriteRespVO>> getFavoritePage(AppFavoritePageReqVO reqVO) {
|
||||
return success(productFavoriteService.getFavoritePage(getLoginUserId(),reqVO));
|
||||
}
|
||||
|
||||
@GetMapping(value = "/checkFavorite")
|
||||
|
||||
@GetMapping(value = "/exits")
|
||||
@Operation(summary = "检查是否收藏过商品")
|
||||
public CommonResult<Boolean> checkFavorite(AppFavoriteReqVO reqVO) {
|
||||
Long userId = getLoginUserId();
|
||||
return success(productFavoriteService.checkFavorite(userId,reqVO));
|
||||
public CommonResult<Boolean> isFavoriteExists(AppFavoriteReqVO reqVO) {
|
||||
return success(productFavoriteService.checkFavorite(getLoginUserId(), reqVO));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ import javax.validation.constraints.NotNull;
|
||||
|
||||
import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED;
|
||||
|
||||
@Schema(description = "用户 APP - 商品收藏 Request VO")
|
||||
@Schema(description = "用户 APP - 商品收藏 Request VO") // 用于收藏、取消收藏
|
||||
@Data
|
||||
public class AppFavoriteReqVO {
|
||||
|
||||
|
@ -2,7 +2,6 @@ package cn.iocoder.yudao.module.product.controller.app.favorite.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED;
|
||||
|
||||
@Schema(description = "用户APP - 商品收藏 Response VO")
|
||||
@ -19,6 +18,7 @@ public class AppFavoriteRespVO {
|
||||
private Integer type;
|
||||
|
||||
// ========== 商品相关字段 ==========
|
||||
|
||||
@Schema(description = "商品 SPU 名称", example = "赵六")
|
||||
private String spuName;
|
||||
|
||||
|
@ -31,4 +31,5 @@ public interface ProductFavoriteMapper extends BaseMapperX<ProductFavoriteDO> {
|
||||
.eq(ProductFavoriteDO::getType, type)
|
||||
.orderByDesc(ProductFavoriteDO::getId));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -16,30 +16,37 @@ public interface ProductFavoriteService {
|
||||
|
||||
/**
|
||||
* 创建商品收藏
|
||||
* @param userId 用户id
|
||||
* @param reqVO 请求vo
|
||||
*/
|
||||
Boolean create(Long userId, @Valid AppFavoriteReqVO reqVO);
|
||||
|
||||
/**
|
||||
* 取消商品收藏 (通过商品详情页面)
|
||||
* @param userId 用户id
|
||||
* @param reqVO 请求vo
|
||||
*/
|
||||
Boolean delete(Long userId, @Valid AppFavoriteReqVO reqVO);
|
||||
|
||||
/**
|
||||
* 分页查询用户收藏列表
|
||||
* @param userId 用户id
|
||||
*
|
||||
* @param userId 用户 id
|
||||
* @param reqVO 请求 vo
|
||||
*/
|
||||
PageResult<AppFavoriteRespVO> page(Long userId, @Valid AppFavoritePageReqVO reqVO);
|
||||
Boolean createFavorite(Long userId, @Valid AppFavoriteReqVO reqVO);
|
||||
|
||||
/**
|
||||
* 取消商品收藏
|
||||
*
|
||||
* @param userId 用户 id
|
||||
* @param reqVO 请求 vo
|
||||
*/
|
||||
Boolean deleteFavorite(Long userId, @Valid AppFavoriteReqVO reqVO);
|
||||
|
||||
// TODO @Jason:VO 拼接,可以交给 Controller 哈,Service 尽量简洁
|
||||
/**
|
||||
* 分页查询用户收藏列表
|
||||
*
|
||||
* @param userId 用户 id
|
||||
* @param reqVO 请求 vo
|
||||
*/
|
||||
PageResult<AppFavoriteRespVO> getFavoritePage(Long userId, @Valid AppFavoritePageReqVO reqVO);
|
||||
|
||||
// TODO @Jason 这个方法最好返回 FavoriteDO 就好。Service 要通用;
|
||||
/**
|
||||
* 检查是否收藏过商品
|
||||
*
|
||||
* @param userId 用户id
|
||||
* @param reqVO 请求 vo
|
||||
* @return true: 已收藏 false: 未收藏
|
||||
*/
|
||||
Boolean checkFavorite(Long userId, @Valid AppFavoriteReqVO reqVO);
|
||||
|
||||
}
|
||||
|
@ -22,8 +22,8 @@ import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.PRODUCT_FAVORITE_EXISTS;
|
||||
import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.PRODUCT_FAVORITE_NOT_EXISTS;
|
||||
import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.FAVORITE_EXISTS;
|
||||
import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.FAVORITE_NOT_EXISTS;
|
||||
|
||||
/**
|
||||
* 商品收藏 Service 实现类
|
||||
@ -33,47 +33,56 @@ import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.PRODUCT_F
|
||||
@Service
|
||||
@Validated
|
||||
public class ProductFavoriteServiceImpl implements ProductFavoriteService {
|
||||
|
||||
@Resource
|
||||
private ProductSpuService productSpuService;
|
||||
|
||||
// TODO @JASON:ProductFavoriteMapper 最好全一点哈。productFavoriteMapper
|
||||
@Resource
|
||||
private ProductFavoriteMapper mapper;
|
||||
|
||||
@Override
|
||||
public Boolean create(Long userId, @Valid AppFavoriteReqVO reqVO) {
|
||||
public Boolean createFavorite(Long userId, @Valid AppFavoriteReqVO reqVO) {
|
||||
// TODO @JASON:userId 校验可以去掉,直接在 Controller 要求登录,通过 @PreAuthenticated 注解
|
||||
Assert.notNull(userId, "the userId must not be null");
|
||||
ProductFavoriteDO favoriteDO = mapper.selectByUserAndSpuAndType(userId, reqVO.getSpuId(), reqVO.getType());
|
||||
if (Objects.nonNull(favoriteDO)) {
|
||||
throw exception(PRODUCT_FAVORITE_EXISTS);
|
||||
throw exception(FAVORITE_EXISTS);
|
||||
}
|
||||
ProductFavoriteDO entity = ProductFavoriteConvert.INSTANCE.convert(userId, reqVO);
|
||||
mapper.insert(entity);
|
||||
// TODO @JASON:可以返回 Long id;
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean delete(Long userId, @Valid AppFavoriteReqVO reqVO) {
|
||||
public Boolean deleteFavorite(Long userId, @Valid AppFavoriteReqVO reqVO) {
|
||||
Assert.notNull(userId, "the userId must not be null ");
|
||||
ProductFavoriteDO favoriteDO = mapper.selectByUserAndSpuAndType(userId, reqVO.getSpuId(), reqVO.getType());
|
||||
if (Objects.isNull(favoriteDO)) {
|
||||
throw exception(PRODUCT_FAVORITE_NOT_EXISTS);
|
||||
throw exception(FAVORITE_NOT_EXISTS);
|
||||
}
|
||||
mapper.deleteById(favoriteDO.getId());
|
||||
// TODO 可以不返回
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageResult<AppFavoriteRespVO> page(Long userId, @Valid AppFavoritePageReqVO reqVO) {
|
||||
public PageResult<AppFavoriteRespVO> getFavoritePage(Long userId, @Valid AppFavoritePageReqVO reqVO) {
|
||||
Assert.notNull(userId, "the userId must not be null ");
|
||||
PageResult<ProductFavoriteDO> favorites = mapper.selectPageByUserAndType(userId, reqVO.getType(), reqVO);
|
||||
// TODO @Jason:if return 更好;例如说,如果判断是空,就 return 掉;
|
||||
if (favorites.getTotal() > 0) {
|
||||
PageResult<AppFavoriteRespVO> pageResult = new PageResult<>(favorites.getTotal());
|
||||
List<ProductFavoriteDO> list = favorites.getList();
|
||||
//得到商品spu 信息
|
||||
// 得到商品 spu 信息
|
||||
List<Long> spuIds = CollectionUtils.convertList(list, ProductFavoriteDO::getSpuId);
|
||||
// TODO @Jason:这里可以用默认的方法,不用 , val -> val 拉
|
||||
Map<Long, ProductSpuDO> spuMap = CollectionUtils.convertMap(productSpuService.getSpuList(spuIds), ProductSpuDO::getId, val -> val);
|
||||
List<AppFavoriteRespVO> resultList = new ArrayList<>(list.size());
|
||||
for (ProductFavoriteDO item : list) {
|
||||
ProductSpuDO spuDO = spuMap.get(item.getSpuId());
|
||||
// TODO @Jason:可以让 convert 整个 convert 放到 mapstruct 里。convert 就是用于各种数据转换,不带逻辑的那种。
|
||||
resultList.add(ProductFavoriteConvert.INSTANCE.convert(spuDO, item));
|
||||
}
|
||||
pageResult.setList(resultList);
|
||||
|
Loading…
Reference in New Issue
Block a user