Commit 467811a42eeb7f7bb54c3b6d7c73b11c13f786a9
1 parent
0ffa7ff4
REVIEW 商品收藏的逻辑 v2
Showing
6 changed files
with
48 additions
and
33 deletions
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/app/favorite/AppFavoriteController.java
| ... | ... | @@ -26,29 +26,27 @@ public class AppFavoriteController { |
| 26 | 26 | |
| 27 | 27 | @PostMapping(value = "/create") |
| 28 | 28 | @Operation(summary = "商品收藏") |
| 29 | - public CommonResult<Boolean> create(@RequestBody @Valid AppFavoriteReqVO reqVO) { | |
| 30 | - Long loginUserId = getLoginUserId(); | |
| 31 | - return success(productFavoriteService.create(loginUserId, reqVO)); | |
| 29 | + public CommonResult<Boolean> createFavorite(@RequestBody @Valid AppFavoriteReqVO reqVO) { | |
| 30 | + return success(productFavoriteService.createFavorite(getLoginUserId(), reqVO)); | |
| 32 | 31 | } |
| 33 | 32 | |
| 34 | 33 | @DeleteMapping(value = "/delete") |
| 35 | - @Operation(summary = "取消商品收藏(通过商品详情)") | |
| 36 | - public CommonResult<Boolean> delete(@RequestBody @Valid AppFavoriteReqVO reqVO) { | |
| 37 | - Long loginUserId = getLoginUserId(); | |
| 38 | - return success(productFavoriteService.delete(loginUserId,reqVO)); | |
| 34 | + @Operation(summary = "取消商品收藏") | |
| 35 | + public CommonResult<Boolean> deleteFavorite(@RequestBody @Valid AppFavoriteReqVO reqVO) { | |
| 36 | + return success(productFavoriteService.deleteFavorite(getLoginUserId(), reqVO)); | |
| 39 | 37 | } |
| 40 | 38 | |
| 41 | 39 | @GetMapping(value = "/page") |
| 42 | 40 | @Operation(summary = "分页获取商品收藏列表") |
| 43 | - public CommonResult<PageResult<AppFavoriteRespVO>> page(AppFavoritePageReqVO reqVO) { | |
| 44 | - Long userId = getLoginUserId(); | |
| 45 | - return success(productFavoriteService.page(userId,reqVO)); | |
| 41 | + public CommonResult<PageResult<AppFavoriteRespVO>> getFavoritePage(AppFavoritePageReqVO reqVO) { | |
| 42 | + return success(productFavoriteService.getFavoritePage(getLoginUserId(),reqVO)); | |
| 46 | 43 | } |
| 47 | 44 | |
| 48 | - @GetMapping(value = "/checkFavorite") | |
| 45 | + | |
| 46 | + @GetMapping(value = "/exits") | |
| 49 | 47 | @Operation(summary = "检查是否收藏过商品") |
| 50 | - public CommonResult<Boolean> checkFavorite(AppFavoriteReqVO reqVO) { | |
| 51 | - Long userId = getLoginUserId(); | |
| 52 | - return success(productFavoriteService.checkFavorite(userId,reqVO)); | |
| 48 | + public CommonResult<Boolean> isFavoriteExists(AppFavoriteReqVO reqVO) { | |
| 49 | + return success(productFavoriteService.checkFavorite(getLoginUserId(), reqVO)); | |
| 53 | 50 | } |
| 51 | + | |
| 54 | 52 | } | ... | ... |
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/app/favorite/vo/AppFavoriteReqVO.java
| ... | ... | @@ -9,7 +9,7 @@ import javax.validation.constraints.NotNull; |
| 9 | 9 | |
| 10 | 10 | import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED; |
| 11 | 11 | |
| 12 | -@Schema(description = "用户 APP - 商品收藏 Request VO") | |
| 12 | +@Schema(description = "用户 APP - 商品收藏 Request VO") // 用于收藏、取消收藏 | |
| 13 | 13 | @Data |
| 14 | 14 | public class AppFavoriteReqVO { |
| 15 | 15 | ... | ... |
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/app/favorite/vo/AppFavoriteRespVO.java
| ... | ... | @@ -2,7 +2,6 @@ package cn.iocoder.yudao.module.product.controller.app.favorite.vo; |
| 2 | 2 | |
| 3 | 3 | import io.swagger.v3.oas.annotations.media.Schema; |
| 4 | 4 | import lombok.Data; |
| 5 | - | |
| 6 | 5 | import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED; |
| 7 | 6 | |
| 8 | 7 | @Schema(description = "用户APP - 商品收藏 Response VO") |
| ... | ... | @@ -19,6 +18,7 @@ public class AppFavoriteRespVO { |
| 19 | 18 | private Integer type; |
| 20 | 19 | |
| 21 | 20 | // ========== 商品相关字段 ========== |
| 21 | + | |
| 22 | 22 | @Schema(description = "商品 SPU 名称", example = "赵六") |
| 23 | 23 | private String spuName; |
| 24 | 24 | ... | ... |
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/mysql/favorite/ProductFavoriteMapper.java
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/favorite/ProductFavoriteService.java
| ... | ... | @@ -16,30 +16,37 @@ public interface ProductFavoriteService { |
| 16 | 16 | |
| 17 | 17 | /** |
| 18 | 18 | * 创建商品收藏 |
| 19 | - * @param userId 用户id | |
| 20 | - * @param reqVO 请求vo | |
| 19 | + * | |
| 20 | + * @param userId 用户 id | |
| 21 | + * @param reqVO 请求 vo | |
| 21 | 22 | */ |
| 22 | - Boolean create(Long userId, @Valid AppFavoriteReqVO reqVO); | |
| 23 | + Boolean createFavorite(Long userId, @Valid AppFavoriteReqVO reqVO); | |
| 23 | 24 | |
| 24 | 25 | /** |
| 25 | - * 取消商品收藏 (通过商品详情页面) | |
| 26 | - * @param userId 用户id | |
| 27 | - * @param reqVO 请求vo | |
| 26 | + * 取消商品收藏 | |
| 27 | + * | |
| 28 | + * @param userId 用户 id | |
| 29 | + * @param reqVO 请求 vo | |
| 28 | 30 | */ |
| 29 | - Boolean delete(Long userId, @Valid AppFavoriteReqVO reqVO); | |
| 31 | + Boolean deleteFavorite(Long userId, @Valid AppFavoriteReqVO reqVO); | |
| 30 | 32 | |
| 33 | + // TODO @Jason:VO 拼接,可以交给 Controller 哈,Service 尽量简洁 | |
| 31 | 34 | /** |
| 32 | 35 | * 分页查询用户收藏列表 |
| 33 | - * @param userId 用户id | |
| 36 | + * | |
| 37 | + * @param userId 用户 id | |
| 34 | 38 | * @param reqVO 请求 vo |
| 35 | 39 | */ |
| 36 | - PageResult<AppFavoriteRespVO> page(Long userId, @Valid AppFavoritePageReqVO reqVO); | |
| 40 | + PageResult<AppFavoriteRespVO> getFavoritePage(Long userId, @Valid AppFavoritePageReqVO reqVO); | |
| 37 | 41 | |
| 42 | + // TODO @Jason 这个方法最好返回 FavoriteDO 就好。Service 要通用; | |
| 38 | 43 | /** |
| 39 | 44 | * 检查是否收藏过商品 |
| 45 | + * | |
| 40 | 46 | * @param userId 用户id |
| 41 | 47 | * @param reqVO 请求 vo |
| 42 | 48 | * @return true: 已收藏 false: 未收藏 |
| 43 | 49 | */ |
| 44 | 50 | Boolean checkFavorite(Long userId, @Valid AppFavoriteReqVO reqVO); |
| 51 | + | |
| 45 | 52 | } | ... | ... |
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/favorite/ProductFavoriteServiceImpl.java
| ... | ... | @@ -22,8 +22,8 @@ import java.util.Map; |
| 22 | 22 | import java.util.Objects; |
| 23 | 23 | |
| 24 | 24 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| 25 | -import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.PRODUCT_FAVORITE_EXISTS; | |
| 26 | -import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.PRODUCT_FAVORITE_NOT_EXISTS; | |
| 25 | +import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.FAVORITE_EXISTS; | |
| 26 | +import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.FAVORITE_NOT_EXISTS; | |
| 27 | 27 | |
| 28 | 28 | /** |
| 29 | 29 | * 商品收藏 Service 实现类 |
| ... | ... | @@ -33,47 +33,56 @@ import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.PRODUCT_F |
| 33 | 33 | @Service |
| 34 | 34 | @Validated |
| 35 | 35 | public class ProductFavoriteServiceImpl implements ProductFavoriteService { |
| 36 | + | |
| 36 | 37 | @Resource |
| 37 | 38 | private ProductSpuService productSpuService; |
| 39 | + | |
| 40 | + // TODO @JASON:ProductFavoriteMapper 最好全一点哈。productFavoriteMapper | |
| 38 | 41 | @Resource |
| 39 | 42 | private ProductFavoriteMapper mapper; |
| 40 | 43 | |
| 41 | 44 | @Override |
| 42 | - public Boolean create(Long userId, @Valid AppFavoriteReqVO reqVO) { | |
| 45 | + public Boolean createFavorite(Long userId, @Valid AppFavoriteReqVO reqVO) { | |
| 46 | + // TODO @JASON:userId 校验可以去掉,直接在 Controller 要求登录,通过 @PreAuthenticated 注解 | |
| 43 | 47 | Assert.notNull(userId, "the userId must not be null"); |
| 44 | 48 | ProductFavoriteDO favoriteDO = mapper.selectByUserAndSpuAndType(userId, reqVO.getSpuId(), reqVO.getType()); |
| 45 | 49 | if (Objects.nonNull(favoriteDO)) { |
| 46 | - throw exception(PRODUCT_FAVORITE_EXISTS); | |
| 50 | + throw exception(FAVORITE_EXISTS); | |
| 47 | 51 | } |
| 48 | 52 | ProductFavoriteDO entity = ProductFavoriteConvert.INSTANCE.convert(userId, reqVO); |
| 49 | 53 | mapper.insert(entity); |
| 54 | + // TODO @JASON:可以返回 Long id; | |
| 50 | 55 | return Boolean.TRUE; |
| 51 | 56 | } |
| 52 | 57 | |
| 53 | 58 | @Override |
| 54 | - public Boolean delete(Long userId, @Valid AppFavoriteReqVO reqVO) { | |
| 59 | + public Boolean deleteFavorite(Long userId, @Valid AppFavoriteReqVO reqVO) { | |
| 55 | 60 | Assert.notNull(userId, "the userId must not be null "); |
| 56 | 61 | ProductFavoriteDO favoriteDO = mapper.selectByUserAndSpuAndType(userId, reqVO.getSpuId(), reqVO.getType()); |
| 57 | 62 | if (Objects.isNull(favoriteDO)) { |
| 58 | - throw exception(PRODUCT_FAVORITE_NOT_EXISTS); | |
| 63 | + throw exception(FAVORITE_NOT_EXISTS); | |
| 59 | 64 | } |
| 60 | 65 | mapper.deleteById(favoriteDO.getId()); |
| 66 | + // TODO 可以不返回 | |
| 61 | 67 | return Boolean.TRUE; |
| 62 | 68 | } |
| 63 | 69 | |
| 64 | 70 | @Override |
| 65 | - public PageResult<AppFavoriteRespVO> page(Long userId, @Valid AppFavoritePageReqVO reqVO) { | |
| 71 | + public PageResult<AppFavoriteRespVO> getFavoritePage(Long userId, @Valid AppFavoritePageReqVO reqVO) { | |
| 66 | 72 | Assert.notNull(userId, "the userId must not be null "); |
| 67 | 73 | PageResult<ProductFavoriteDO> favorites = mapper.selectPageByUserAndType(userId, reqVO.getType(), reqVO); |
| 74 | + // TODO @Jason:if return 更好;例如说,如果判断是空,就 return 掉; | |
| 68 | 75 | if (favorites.getTotal() > 0) { |
| 69 | 76 | PageResult<AppFavoriteRespVO> pageResult = new PageResult<>(favorites.getTotal()); |
| 70 | 77 | List<ProductFavoriteDO> list = favorites.getList(); |
| 71 | - //得到商品spu 信息 | |
| 78 | + // 得到商品 spu 信息 | |
| 72 | 79 | List<Long> spuIds = CollectionUtils.convertList(list, ProductFavoriteDO::getSpuId); |
| 80 | + // TODO @Jason:这里可以用默认的方法,不用 , val -> val 拉 | |
| 73 | 81 | Map<Long, ProductSpuDO> spuMap = CollectionUtils.convertMap(productSpuService.getSpuList(spuIds), ProductSpuDO::getId, val -> val); |
| 74 | 82 | List<AppFavoriteRespVO> resultList = new ArrayList<>(list.size()); |
| 75 | 83 | for (ProductFavoriteDO item : list) { |
| 76 | 84 | ProductSpuDO spuDO = spuMap.get(item.getSpuId()); |
| 85 | + // TODO @Jason:可以让 convert 整个 convert 放到 mapstruct 里。convert 就是用于各种数据转换,不带逻辑的那种。 | |
| 77 | 86 | resultList.add(ProductFavoriteConvert.INSTANCE.convert(spuDO, item)); |
| 78 | 87 | } |
| 79 | 88 | pageResult.setList(resultList); | ... | ... |