From 467811a42eeb7f7bb54c3b6d7c73b11c13f786a9 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sun, 14 May 2023 09:07:45 +0800 Subject: [PATCH] =?UTF-8?q?REVIEW=20=E5=95=86=E5=93=81=E6=94=B6=E8=97=8F?= =?UTF-8?q?=E7=9A=84=E9=80=BB=E8=BE=91=20v2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/favorite/AppFavoriteController.java | 26 ++++++------- .../app/favorite/vo/AppFavoriteReqVO.java | 2 +- .../app/favorite/vo/AppFavoriteRespVO.java | 2 +- .../mysql/favorite/ProductFavoriteMapper.java | 1 + .../favorite/ProductFavoriteService.java | 39 +++++++++++-------- .../favorite/ProductFavoriteServiceImpl.java | 25 ++++++++---- 6 files changed, 55 insertions(+), 40 deletions(-) diff --git a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/app/favorite/AppFavoriteController.java b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/app/favorite/AppFavoriteController.java index 1b085e144..4359dd124 100644 --- a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/app/favorite/AppFavoriteController.java +++ b/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 { @PostMapping(value = "/create") @Operation(summary = "商品收藏") - public CommonResult create(@RequestBody @Valid AppFavoriteReqVO reqVO) { - Long loginUserId = getLoginUserId(); - return success(productFavoriteService.create(loginUserId, reqVO)); + public CommonResult createFavorite(@RequestBody @Valid AppFavoriteReqVO reqVO) { + return success(productFavoriteService.createFavorite(getLoginUserId(), reqVO)); } @DeleteMapping(value = "/delete") - @Operation(summary = "取消商品收藏(通过商品详情)") - public CommonResult delete(@RequestBody @Valid AppFavoriteReqVO reqVO) { - Long loginUserId = getLoginUserId(); - return success(productFavoriteService.delete(loginUserId,reqVO)); + @Operation(summary = "取消商品收藏") + public CommonResult deleteFavorite(@RequestBody @Valid AppFavoriteReqVO reqVO) { + return success(productFavoriteService.deleteFavorite(getLoginUserId(), reqVO)); } @GetMapping(value = "/page") @Operation(summary = "分页获取商品收藏列表") - public CommonResult> page(AppFavoritePageReqVO reqVO) { - Long userId = getLoginUserId(); - return success(productFavoriteService.page(userId,reqVO)); + public CommonResult> getFavoritePage(AppFavoritePageReqVO reqVO) { + return success(productFavoriteService.getFavoritePage(getLoginUserId(),reqVO)); } - @GetMapping(value = "/checkFavorite") + + @GetMapping(value = "/exits") @Operation(summary = "检查是否收藏过商品") - public CommonResult checkFavorite(AppFavoriteReqVO reqVO) { - Long userId = getLoginUserId(); - return success(productFavoriteService.checkFavorite(userId,reqVO)); + public CommonResult isFavoriteExists(AppFavoriteReqVO reqVO) { + return success(productFavoriteService.checkFavorite(getLoginUserId(), reqVO)); } + } diff --git a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/app/favorite/vo/AppFavoriteReqVO.java b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/app/favorite/vo/AppFavoriteReqVO.java index 292750a7b..95fa8e867 100644 --- a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/app/favorite/vo/AppFavoriteReqVO.java +++ b/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; 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 { diff --git a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/app/favorite/vo/AppFavoriteRespVO.java b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/app/favorite/vo/AppFavoriteRespVO.java index da6937c5b..a0f81a909 100644 --- a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/app/favorite/vo/AppFavoriteRespVO.java +++ b/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; 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; diff --git a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/mysql/favorite/ProductFavoriteMapper.java b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/mysql/favorite/ProductFavoriteMapper.java index b9a8c8353..33e027aa6 100644 --- a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/mysql/favorite/ProductFavoriteMapper.java +++ b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/mysql/favorite/ProductFavoriteMapper.java @@ -31,4 +31,5 @@ public interface ProductFavoriteMapper extends BaseMapperX { .eq(ProductFavoriteDO::getType, type) .orderByDesc(ProductFavoriteDO::getId)); } + } diff --git a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/favorite/ProductFavoriteService.java b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/favorite/ProductFavoriteService.java index cf245f781..262cef1af 100644 --- a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/favorite/ProductFavoriteService.java +++ b/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 { /** * 创建商品收藏 - * @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 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 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); + } diff --git a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/favorite/ProductFavoriteServiceImpl.java b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/favorite/ProductFavoriteServiceImpl.java index d2c741045..77d592f6d 100644 --- a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/favorite/ProductFavoriteServiceImpl.java +++ b/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; 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 page(Long userId, @Valid AppFavoritePageReqVO reqVO) { + public PageResult getFavoritePage(Long userId, @Valid AppFavoritePageReqVO reqVO) { Assert.notNull(userId, "the userId must not be null "); PageResult favorites = mapper.selectPageByUserAndType(userId, reqVO.getType(), reqVO); + // TODO @Jason:if return 更好;例如说,如果判断是空,就 return 掉; if (favorites.getTotal() > 0) { PageResult pageResult = new PageResult<>(favorites.getTotal()); List list = favorites.getList(); - //得到商品spu 信息 + // 得到商品 spu 信息 List spuIds = CollectionUtils.convertList(list, ProductFavoriteDO::getSpuId); + // TODO @Jason:这里可以用默认的方法,不用 , val -> val 拉 Map spuMap = CollectionUtils.convertMap(productSpuService.getSpuList(spuIds), ProductSpuDO::getId, val -> val); List 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);