Commit 7a792a7d48306b2cd8091a28f0ccf743840bbb35
1 parent
7fef5943
商品收藏相关修改
Showing
5 changed files
with
63 additions
and
57 deletions
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/app/favorite/AppFavoriteController.java
| @@ -2,10 +2,15 @@ package cn.iocoder.yudao.module.product.controller.app.favorite; | @@ -2,10 +2,15 @@ package cn.iocoder.yudao.module.product.controller.app.favorite; | ||
| 2 | 2 | ||
| 3 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; | 3 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| 4 | import cn.iocoder.yudao.framework.common.pojo.PageResult; | 4 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 5 | +import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; | ||
| 5 | import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoritePageReqVO; | 6 | import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoritePageReqVO; |
| 6 | import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteReqVO; | 7 | import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteReqVO; |
| 7 | import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteRespVO; | 8 | import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteRespVO; |
| 9 | +import cn.iocoder.yudao.module.product.convert.favorite.ProductFavoriteConvert; | ||
| 10 | +import cn.iocoder.yudao.module.product.dal.dataobject.favorite.ProductFavoriteDO; | ||
| 11 | +import cn.iocoder.yudao.module.product.dal.dataobject.spu.ProductSpuDO; | ||
| 8 | import cn.iocoder.yudao.module.product.service.favorite.ProductFavoriteService; | 12 | import cn.iocoder.yudao.module.product.service.favorite.ProductFavoriteService; |
| 13 | +import cn.iocoder.yudao.module.product.service.spu.ProductSpuService; | ||
| 9 | import io.swagger.v3.oas.annotations.Operation; | 14 | import io.swagger.v3.oas.annotations.Operation; |
| 10 | import io.swagger.v3.oas.annotations.tags.Tag; | 15 | import io.swagger.v3.oas.annotations.tags.Tag; |
| 11 | import org.springframework.web.bind.annotation.*; | 16 | import org.springframework.web.bind.annotation.*; |
| @@ -13,6 +18,9 @@ import org.springframework.web.bind.annotation.*; | @@ -13,6 +18,9 @@ import org.springframework.web.bind.annotation.*; | ||
| 13 | import javax.annotation.Resource; | 18 | import javax.annotation.Resource; |
| 14 | import javax.validation.Valid; | 19 | import javax.validation.Valid; |
| 15 | 20 | ||
| 21 | +import java.util.List; | ||
| 22 | +import java.util.Objects; | ||
| 23 | + | ||
| 16 | import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; | 24 | import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; |
| 17 | import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; | 25 | import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; |
| 18 | 26 | ||
| @@ -23,30 +31,46 @@ public class AppFavoriteController { | @@ -23,30 +31,46 @@ public class AppFavoriteController { | ||
| 23 | 31 | ||
| 24 | @Resource | 32 | @Resource |
| 25 | private ProductFavoriteService productFavoriteService; | 33 | private ProductFavoriteService productFavoriteService; |
| 34 | + @Resource | ||
| 35 | + private ProductSpuService productSpuService; | ||
| 26 | 36 | ||
| 27 | @PostMapping(value = "/create") | 37 | @PostMapping(value = "/create") |
| 28 | @Operation(summary = "商品收藏") | 38 | @Operation(summary = "商品收藏") |
| 29 | - public CommonResult<Boolean> createFavorite(@RequestBody @Valid AppFavoriteReqVO reqVO) { | 39 | + //@PreAuthenticated TODO 暂时注释 |
| 40 | + public CommonResult<Long> createFavorite(@RequestBody @Valid AppFavoriteReqVO reqVO) { | ||
| 30 | return success(productFavoriteService.createFavorite(getLoginUserId(), reqVO)); | 41 | return success(productFavoriteService.createFavorite(getLoginUserId(), reqVO)); |
| 31 | } | 42 | } |
| 32 | 43 | ||
| 33 | @DeleteMapping(value = "/delete") | 44 | @DeleteMapping(value = "/delete") |
| 34 | @Operation(summary = "取消商品收藏") | 45 | @Operation(summary = "取消商品收藏") |
| 35 | public CommonResult<Boolean> deleteFavorite(@RequestBody @Valid AppFavoriteReqVO reqVO) { | 46 | public CommonResult<Boolean> deleteFavorite(@RequestBody @Valid AppFavoriteReqVO reqVO) { |
| 36 | - return success(productFavoriteService.deleteFavorite(getLoginUserId(), reqVO)); | 47 | + productFavoriteService.deleteFavorite(getLoginUserId(), reqVO); |
| 48 | + return success(Boolean.TRUE); | ||
| 37 | } | 49 | } |
| 38 | 50 | ||
| 39 | @GetMapping(value = "/page") | 51 | @GetMapping(value = "/page") |
| 40 | @Operation(summary = "分页获取商品收藏列表") | 52 | @Operation(summary = "分页获取商品收藏列表") |
| 41 | public CommonResult<PageResult<AppFavoriteRespVO>> getFavoritePage(AppFavoritePageReqVO reqVO) { | 53 | public CommonResult<PageResult<AppFavoriteRespVO>> getFavoritePage(AppFavoritePageReqVO reqVO) { |
| 42 | - return success(productFavoriteService.getFavoritePage(getLoginUserId(),reqVO)); | 54 | + PageResult<ProductFavoriteDO> favorites = productFavoriteService.getFavoritePage(getLoginUserId(), reqVO); |
| 55 | + if (favorites.getTotal() <= 0) { | ||
| 56 | + return success(PageResult.empty()); | ||
| 57 | + } | ||
| 58 | + List<ProductFavoriteDO> productFavoriteList = favorites.getList(); | ||
| 59 | + // 得到商品 spu 信息 | ||
| 60 | + List<Long> spuIds = CollectionUtils.convertList(productFavoriteList, ProductFavoriteDO::getSpuId); | ||
| 61 | + List<ProductSpuDO> spuList = productSpuService.getSpuList(spuIds); | ||
| 62 | + //转换 VO | ||
| 63 | + PageResult<AppFavoriteRespVO> pageResult = new PageResult<>(favorites.getTotal()); | ||
| 64 | + pageResult.setList(ProductFavoriteConvert.INSTANCE.convertList(productFavoriteList, spuList)); | ||
| 65 | + return success(pageResult); | ||
| 43 | } | 66 | } |
| 44 | 67 | ||
| 45 | 68 | ||
| 46 | @GetMapping(value = "/exits") | 69 | @GetMapping(value = "/exits") |
| 47 | @Operation(summary = "检查是否收藏过商品") | 70 | @Operation(summary = "检查是否收藏过商品") |
| 48 | public CommonResult<Boolean> isFavoriteExists(AppFavoriteReqVO reqVO) { | 71 | public CommonResult<Boolean> isFavoriteExists(AppFavoriteReqVO reqVO) { |
| 49 | - return success(productFavoriteService.checkFavorite(getLoginUserId(), reqVO)); | 72 | + ProductFavoriteDO favoriteDO = productFavoriteService.getFavorite(getLoginUserId(), reqVO); |
| 73 | + return success(Objects.nonNull(favoriteDO)); | ||
| 50 | } | 74 | } |
| 51 | 75 | ||
| 52 | } | 76 | } |
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,7 +9,7 @@ import javax.validation.constraints.NotNull; | ||
| 9 | 9 | ||
| 10 | import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED; | 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 | @Data | 13 | @Data |
| 14 | public class AppFavoriteReqVO { | 14 | public class AppFavoriteReqVO { |
| 15 | 15 |
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/convert/favorite/ProductFavoriteConvert.java
| 1 | package cn.iocoder.yudao.module.product.convert.favorite; | 1 | package cn.iocoder.yudao.module.product.convert.favorite; |
| 2 | 2 | ||
| 3 | +import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; | ||
| 3 | import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteReqVO; | 4 | import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteReqVO; |
| 4 | import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteRespVO; | 5 | import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteRespVO; |
| 5 | import cn.iocoder.yudao.module.product.dal.dataobject.favorite.ProductFavoriteDO; | 6 | import cn.iocoder.yudao.module.product.dal.dataobject.favorite.ProductFavoriteDO; |
| @@ -8,13 +9,28 @@ import org.mapstruct.Mapper; | @@ -8,13 +9,28 @@ import org.mapstruct.Mapper; | ||
| 8 | import org.mapstruct.Mapping; | 9 | import org.mapstruct.Mapping; |
| 9 | import org.mapstruct.factory.Mappers; | 10 | import org.mapstruct.factory.Mappers; |
| 10 | 11 | ||
| 12 | +import java.util.ArrayList; | ||
| 13 | +import java.util.List; | ||
| 14 | +import java.util.Map; | ||
| 15 | + | ||
| 11 | @Mapper | 16 | @Mapper |
| 12 | public interface ProductFavoriteConvert { | 17 | public interface ProductFavoriteConvert { |
| 13 | 18 | ||
| 14 | ProductFavoriteConvert INSTANCE = Mappers.getMapper(ProductFavoriteConvert.class); | 19 | ProductFavoriteConvert INSTANCE = Mappers.getMapper(ProductFavoriteConvert.class); |
| 15 | 20 | ||
| 16 | ProductFavoriteDO convert(Long userId, AppFavoriteReqVO reqVO); | 21 | ProductFavoriteDO convert(Long userId, AppFavoriteReqVO reqVO); |
| 22 | + | ||
| 17 | @Mapping(target = "id", source = "favoriteDO.id") | 23 | @Mapping(target = "id", source = "favoriteDO.id") |
| 18 | @Mapping(target = "spuName", source = "spuDO.name") | 24 | @Mapping(target = "spuName", source = "spuDO.name") |
| 19 | AppFavoriteRespVO convert(ProductSpuDO spuDO, ProductFavoriteDO favoriteDO); | 25 | AppFavoriteRespVO convert(ProductSpuDO spuDO, ProductFavoriteDO favoriteDO); |
| 26 | + | ||
| 27 | + default List<AppFavoriteRespVO> convertList(List<ProductFavoriteDO> productFavoriteDOList, List<ProductSpuDO> productSpuDOList) { | ||
| 28 | + List<AppFavoriteRespVO> resultList = new ArrayList<>(productFavoriteDOList.size()); | ||
| 29 | + Map<Long, ProductSpuDO> spuMap = CollectionUtils.convertMap(productSpuDOList, ProductSpuDO::getId); | ||
| 30 | + for (ProductFavoriteDO item : productFavoriteDOList) { | ||
| 31 | + ProductSpuDO spuDO = spuMap.get(item.getSpuId()); | ||
| 32 | + resultList.add(convert(spuDO, item)); | ||
| 33 | + } | ||
| 34 | + return resultList; | ||
| 35 | + } | ||
| 20 | } | 36 | } |
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/favorite/ProductFavoriteService.java
| @@ -4,6 +4,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult; | @@ -4,6 +4,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult; | ||
| 4 | import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoritePageReqVO; | 4 | import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoritePageReqVO; |
| 5 | import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteReqVO; | 5 | import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteReqVO; |
| 6 | import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteRespVO; | 6 | import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteRespVO; |
| 7 | +import cn.iocoder.yudao.module.product.dal.dataobject.favorite.ProductFavoriteDO; | ||
| 7 | 8 | ||
| 8 | import javax.validation.Valid; | 9 | import javax.validation.Valid; |
| 9 | 10 | ||
| @@ -20,7 +21,7 @@ public interface ProductFavoriteService { | @@ -20,7 +21,7 @@ public interface ProductFavoriteService { | ||
| 20 | * @param userId 用户 id | 21 | * @param userId 用户 id |
| 21 | * @param reqVO 请求 vo | 22 | * @param reqVO 请求 vo |
| 22 | */ | 23 | */ |
| 23 | - Boolean createFavorite(Long userId, @Valid AppFavoriteReqVO reqVO); | 24 | + Long createFavorite(Long userId, @Valid AppFavoriteReqVO reqVO); |
| 24 | 25 | ||
| 25 | /** | 26 | /** |
| 26 | * 取消商品收藏 | 27 | * 取消商品收藏 |
| @@ -28,25 +29,22 @@ public interface ProductFavoriteService { | @@ -28,25 +29,22 @@ public interface ProductFavoriteService { | ||
| 28 | * @param userId 用户 id | 29 | * @param userId 用户 id |
| 29 | * @param reqVO 请求 vo | 30 | * @param reqVO 请求 vo |
| 30 | */ | 31 | */ |
| 31 | - Boolean deleteFavorite(Long userId, @Valid AppFavoriteReqVO reqVO); | 32 | + void deleteFavorite(Long userId, @Valid AppFavoriteReqVO reqVO); |
| 32 | 33 | ||
| 33 | - // TODO @Jason:VO 拼接,可以交给 Controller 哈,Service 尽量简洁 | ||
| 34 | /** | 34 | /** |
| 35 | * 分页查询用户收藏列表 | 35 | * 分页查询用户收藏列表 |
| 36 | * | 36 | * |
| 37 | * @param userId 用户 id | 37 | * @param userId 用户 id |
| 38 | * @param reqVO 请求 vo | 38 | * @param reqVO 请求 vo |
| 39 | */ | 39 | */ |
| 40 | - PageResult<AppFavoriteRespVO> getFavoritePage(Long userId, @Valid AppFavoritePageReqVO reqVO); | 40 | + PageResult<ProductFavoriteDO> getFavoritePage(Long userId, @Valid AppFavoritePageReqVO reqVO); |
| 41 | 41 | ||
| 42 | - // TODO @Jason 这个方法最好返回 FavoriteDO 就好。Service 要通用; | ||
| 43 | /** | 42 | /** |
| 44 | - * 检查是否收藏过商品 | 43 | + * 获取收藏过商品 |
| 45 | * | 44 | * |
| 46 | * @param userId 用户id | 45 | * @param userId 用户id |
| 47 | * @param reqVO 请求 vo | 46 | * @param reqVO 请求 vo |
| 48 | - * @return true: 已收藏 false: 未收藏 | ||
| 49 | */ | 47 | */ |
| 50 | - Boolean checkFavorite(Long userId, @Valid AppFavoriteReqVO reqVO); | 48 | + ProductFavoriteDO getFavorite(Long userId, @Valid AppFavoriteReqVO reqVO); |
| 51 | 49 | ||
| 52 | } | 50 | } |
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/favorite/ProductFavoriteServiceImpl.java
| @@ -35,68 +35,36 @@ import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.FAVORITE_ | @@ -35,68 +35,36 @@ import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.FAVORITE_ | ||
| 35 | public class ProductFavoriteServiceImpl implements ProductFavoriteService { | 35 | public class ProductFavoriteServiceImpl implements ProductFavoriteService { |
| 36 | 36 | ||
| 37 | @Resource | 37 | @Resource |
| 38 | - private ProductSpuService productSpuService; | ||
| 39 | - | ||
| 40 | - // TODO @JASON:ProductFavoriteMapper 最好全一点哈。productFavoriteMapper | ||
| 41 | - @Resource | ||
| 42 | - private ProductFavoriteMapper mapper; | 38 | + private ProductFavoriteMapper productFavoriteMapper; |
| 43 | 39 | ||
| 44 | @Override | 40 | @Override |
| 45 | - public Boolean createFavorite(Long userId, @Valid AppFavoriteReqVO reqVO) { | ||
| 46 | - // TODO @JASON:userId 校验可以去掉,直接在 Controller 要求登录,通过 @PreAuthenticated 注解 | ||
| 47 | - Assert.notNull(userId, "the userId must not be null"); | ||
| 48 | - ProductFavoriteDO favoriteDO = mapper.selectByUserAndSpuAndType(userId, reqVO.getSpuId(), reqVO.getType()); | 41 | + public Long createFavorite(Long userId, @Valid AppFavoriteReqVO reqVO) { |
| 42 | + ProductFavoriteDO favoriteDO = productFavoriteMapper.selectByUserAndSpuAndType(userId, reqVO.getSpuId(), reqVO.getType()); | ||
| 49 | if (Objects.nonNull(favoriteDO)) { | 43 | if (Objects.nonNull(favoriteDO)) { |
| 50 | throw exception(FAVORITE_EXISTS); | 44 | throw exception(FAVORITE_EXISTS); |
| 51 | } | 45 | } |
| 52 | ProductFavoriteDO entity = ProductFavoriteConvert.INSTANCE.convert(userId, reqVO); | 46 | ProductFavoriteDO entity = ProductFavoriteConvert.INSTANCE.convert(userId, reqVO); |
| 53 | - mapper.insert(entity); | ||
| 54 | - // TODO @JASON:可以返回 Long id; | ||
| 55 | - return Boolean.TRUE; | 47 | + productFavoriteMapper.insert(entity); |
| 48 | + return entity.getId(); | ||
| 56 | } | 49 | } |
| 57 | 50 | ||
| 58 | @Override | 51 | @Override |
| 59 | - public Boolean deleteFavorite(Long userId, @Valid AppFavoriteReqVO reqVO) { | ||
| 60 | - Assert.notNull(userId, "the userId must not be null "); | ||
| 61 | - ProductFavoriteDO favoriteDO = mapper.selectByUserAndSpuAndType(userId, reqVO.getSpuId(), reqVO.getType()); | 52 | + public void deleteFavorite(Long userId, @Valid AppFavoriteReqVO reqVO) { |
| 53 | + ProductFavoriteDO favoriteDO = productFavoriteMapper.selectByUserAndSpuAndType(userId, reqVO.getSpuId(), reqVO.getType()); | ||
| 62 | if (Objects.isNull(favoriteDO)) { | 54 | if (Objects.isNull(favoriteDO)) { |
| 63 | throw exception(FAVORITE_NOT_EXISTS); | 55 | throw exception(FAVORITE_NOT_EXISTS); |
| 64 | } | 56 | } |
| 65 | - mapper.deleteById(favoriteDO.getId()); | ||
| 66 | - // TODO 可以不返回 | ||
| 67 | - return Boolean.TRUE; | 57 | + productFavoriteMapper.deleteById(favoriteDO.getId()); |
| 68 | } | 58 | } |
| 69 | 59 | ||
| 70 | @Override | 60 | @Override |
| 71 | - public PageResult<AppFavoriteRespVO> getFavoritePage(Long userId, @Valid AppFavoritePageReqVO reqVO) { | ||
| 72 | - Assert.notNull(userId, "the userId must not be null "); | ||
| 73 | - PageResult<ProductFavoriteDO> favorites = mapper.selectPageByUserAndType(userId, reqVO.getType(), reqVO); | ||
| 74 | - // TODO @Jason:if return 更好;例如说,如果判断是空,就 return 掉; | ||
| 75 | - if (favorites.getTotal() > 0) { | ||
| 76 | - PageResult<AppFavoriteRespVO> pageResult = new PageResult<>(favorites.getTotal()); | ||
| 77 | - List<ProductFavoriteDO> list = favorites.getList(); | ||
| 78 | - // 得到商品 spu 信息 | ||
| 79 | - List<Long> spuIds = CollectionUtils.convertList(list, ProductFavoriteDO::getSpuId); | ||
| 80 | - // TODO @Jason:这里可以用默认的方法,不用 , val -> val 拉 | ||
| 81 | - Map<Long, ProductSpuDO> spuMap = CollectionUtils.convertMap(productSpuService.getSpuList(spuIds), ProductSpuDO::getId, val -> val); | ||
| 82 | - List<AppFavoriteRespVO> resultList = new ArrayList<>(list.size()); | ||
| 83 | - for (ProductFavoriteDO item : list) { | ||
| 84 | - ProductSpuDO spuDO = spuMap.get(item.getSpuId()); | ||
| 85 | - // TODO @Jason:可以让 convert 整个 convert 放到 mapstruct 里。convert 就是用于各种数据转换,不带逻辑的那种。 | ||
| 86 | - resultList.add(ProductFavoriteConvert.INSTANCE.convert(spuDO, item)); | ||
| 87 | - } | ||
| 88 | - pageResult.setList(resultList); | ||
| 89 | - return pageResult; | ||
| 90 | - }else { | ||
| 91 | - return PageResult.empty(); | ||
| 92 | - } | 61 | + public PageResult<ProductFavoriteDO> getFavoritePage(Long userId, @Valid AppFavoritePageReqVO reqVO) { |
| 62 | + return productFavoriteMapper.selectPageByUserAndType(userId, reqVO.getType(), reqVO); | ||
| 93 | } | 63 | } |
| 94 | 64 | ||
| 95 | @Override | 65 | @Override |
| 96 | - public Boolean checkFavorite(Long userId, @Valid AppFavoriteReqVO reqVO) { | ||
| 97 | - Assert.notNull(userId, "the userId must not be null "); | ||
| 98 | - ProductFavoriteDO favoriteDO = mapper.selectByUserAndSpuAndType(userId, reqVO.getSpuId(), reqVO.getType()); | ||
| 99 | - return Objects.nonNull(favoriteDO); | 66 | + public ProductFavoriteDO getFavorite(Long userId, @Valid AppFavoriteReqVO reqVO) { |
| 67 | + return productFavoriteMapper.selectByUserAndSpuAndType(userId, reqVO.getSpuId(), reqVO.getType()); | ||
| 100 | } | 68 | } |
| 101 | 69 | ||
| 102 | } | 70 | } |