商品收藏相关修改

This commit is contained in:
jason 2023-05-14 23:39:05 +08:00
parent 7fef594390
commit 7a792a7d48
5 changed files with 63 additions and 57 deletions

View File

@ -2,10 +2,15 @@ package cn.iocoder.yudao.module.product.controller.app.favorite;
import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoritePageReqVO; import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoritePageReqVO;
import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteReqVO; import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteReqVO;
import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteRespVO; import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteRespVO;
import cn.iocoder.yudao.module.product.convert.favorite.ProductFavoriteConvert;
import cn.iocoder.yudao.module.product.dal.dataobject.favorite.ProductFavoriteDO;
import cn.iocoder.yudao.module.product.dal.dataobject.spu.ProductSpuDO;
import cn.iocoder.yudao.module.product.service.favorite.ProductFavoriteService; import cn.iocoder.yudao.module.product.service.favorite.ProductFavoriteService;
import cn.iocoder.yudao.module.product.service.spu.ProductSpuService;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@ -13,6 +18,9 @@ import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.validation.Valid; import javax.validation.Valid;
import java.util.List;
import java.util.Objects;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
@ -23,30 +31,46 @@ public class AppFavoriteController {
@Resource @Resource
private ProductFavoriteService productFavoriteService; private ProductFavoriteService productFavoriteService;
@Resource
private ProductSpuService productSpuService;
@PostMapping(value = "/create") @PostMapping(value = "/create")
@Operation(summary = "商品收藏") @Operation(summary = "商品收藏")
public CommonResult<Boolean> createFavorite(@RequestBody @Valid AppFavoriteReqVO reqVO) { //@PreAuthenticated TODO 暂时注释
public CommonResult<Long> createFavorite(@RequestBody @Valid AppFavoriteReqVO reqVO) {
return success(productFavoriteService.createFavorite(getLoginUserId(), reqVO)); return success(productFavoriteService.createFavorite(getLoginUserId(), reqVO));
} }
@DeleteMapping(value = "/delete") @DeleteMapping(value = "/delete")
@Operation(summary = "取消商品收藏") @Operation(summary = "取消商品收藏")
public CommonResult<Boolean> deleteFavorite(@RequestBody @Valid AppFavoriteReqVO reqVO) { public CommonResult<Boolean> deleteFavorite(@RequestBody @Valid AppFavoriteReqVO reqVO) {
return success(productFavoriteService.deleteFavorite(getLoginUserId(), reqVO)); productFavoriteService.deleteFavorite(getLoginUserId(), reqVO);
return success(Boolean.TRUE);
} }
@GetMapping(value = "/page") @GetMapping(value = "/page")
@Operation(summary = "分页获取商品收藏列表") @Operation(summary = "分页获取商品收藏列表")
public CommonResult<PageResult<AppFavoriteRespVO>> getFavoritePage(AppFavoritePageReqVO reqVO) { public CommonResult<PageResult<AppFavoriteRespVO>> getFavoritePage(AppFavoritePageReqVO reqVO) {
return success(productFavoriteService.getFavoritePage(getLoginUserId(),reqVO)); PageResult<ProductFavoriteDO> favorites = productFavoriteService.getFavoritePage(getLoginUserId(), reqVO);
if (favorites.getTotal() <= 0) {
return success(PageResult.empty());
}
List<ProductFavoriteDO> productFavoriteList = favorites.getList();
// 得到商品 spu 信息
List<Long> spuIds = CollectionUtils.convertList(productFavoriteList, ProductFavoriteDO::getSpuId);
List<ProductSpuDO> spuList = productSpuService.getSpuList(spuIds);
//转换 VO
PageResult<AppFavoriteRespVO> pageResult = new PageResult<>(favorites.getTotal());
pageResult.setList(ProductFavoriteConvert.INSTANCE.convertList(productFavoriteList, spuList));
return success(pageResult);
} }
@GetMapping(value = "/exits") @GetMapping(value = "/exits")
@Operation(summary = "检查是否收藏过商品") @Operation(summary = "检查是否收藏过商品")
public CommonResult<Boolean> isFavoriteExists(AppFavoriteReqVO reqVO) { public CommonResult<Boolean> isFavoriteExists(AppFavoriteReqVO reqVO) {
return success(productFavoriteService.checkFavorite(getLoginUserId(), reqVO)); ProductFavoriteDO favoriteDO = productFavoriteService.getFavorite(getLoginUserId(), reqVO);
return success(Objects.nonNull(favoriteDO));
} }
} }

View File

@ -9,7 +9,7 @@ import javax.validation.constraints.NotNull;
import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED; import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED;
@Schema(description = "用户 APP - 商品收藏 Request VO") // 用于收藏取消收藏 @Schema(description = "用户 APP - 商品收藏 Request VO") // 用于收藏取消收藏, 获取收藏
@Data @Data
public class AppFavoriteReqVO { public class AppFavoriteReqVO {

View File

@ -1,5 +1,6 @@
package cn.iocoder.yudao.module.product.convert.favorite; package cn.iocoder.yudao.module.product.convert.favorite;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteReqVO; import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteReqVO;
import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteRespVO; import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteRespVO;
import cn.iocoder.yudao.module.product.dal.dataobject.favorite.ProductFavoriteDO; import cn.iocoder.yudao.module.product.dal.dataobject.favorite.ProductFavoriteDO;
@ -8,13 +9,28 @@ import org.mapstruct.Mapper;
import org.mapstruct.Mapping; import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers; import org.mapstruct.factory.Mappers;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Mapper @Mapper
public interface ProductFavoriteConvert { public interface ProductFavoriteConvert {
ProductFavoriteConvert INSTANCE = Mappers.getMapper(ProductFavoriteConvert.class); ProductFavoriteConvert INSTANCE = Mappers.getMapper(ProductFavoriteConvert.class);
ProductFavoriteDO convert(Long userId, AppFavoriteReqVO reqVO); ProductFavoriteDO convert(Long userId, AppFavoriteReqVO reqVO);
@Mapping(target = "id", source = "favoriteDO.id") @Mapping(target = "id", source = "favoriteDO.id")
@Mapping(target = "spuName", source = "spuDO.name") @Mapping(target = "spuName", source = "spuDO.name")
AppFavoriteRespVO convert(ProductSpuDO spuDO, ProductFavoriteDO favoriteDO); AppFavoriteRespVO convert(ProductSpuDO spuDO, ProductFavoriteDO favoriteDO);
default List<AppFavoriteRespVO> convertList(List<ProductFavoriteDO> productFavoriteDOList, List<ProductSpuDO> productSpuDOList) {
List<AppFavoriteRespVO> resultList = new ArrayList<>(productFavoriteDOList.size());
Map<Long, ProductSpuDO> spuMap = CollectionUtils.convertMap(productSpuDOList, ProductSpuDO::getId);
for (ProductFavoriteDO item : productFavoriteDOList) {
ProductSpuDO spuDO = spuMap.get(item.getSpuId());
resultList.add(convert(spuDO, item));
}
return resultList;
}
} }

View File

@ -4,6 +4,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoritePageReqVO; import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoritePageReqVO;
import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteReqVO; import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteReqVO;
import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteRespVO; import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteRespVO;
import cn.iocoder.yudao.module.product.dal.dataobject.favorite.ProductFavoriteDO;
import javax.validation.Valid; import javax.validation.Valid;
@ -20,7 +21,7 @@ public interface ProductFavoriteService {
* @param userId 用户 id * @param userId 用户 id
* @param reqVO 请求 vo * @param reqVO 请求 vo
*/ */
Boolean createFavorite(Long userId, @Valid AppFavoriteReqVO reqVO); Long createFavorite(Long userId, @Valid AppFavoriteReqVO reqVO);
/** /**
* 取消商品收藏 * 取消商品收藏
@ -28,25 +29,22 @@ public interface ProductFavoriteService {
* @param userId 用户 id * @param userId 用户 id
* @param reqVO 请求 vo * @param reqVO 请求 vo
*/ */
Boolean deleteFavorite(Long userId, @Valid AppFavoriteReqVO reqVO); void deleteFavorite(Long userId, @Valid AppFavoriteReqVO reqVO);
// TODO @JasonVO 拼接可以交给 Controller Service 尽量简洁
/** /**
* 分页查询用户收藏列表 * 分页查询用户收藏列表
* *
* @param userId 用户 id * @param userId 用户 id
* @param reqVO 请求 vo * @param reqVO 请求 vo
*/ */
PageResult<AppFavoriteRespVO> getFavoritePage(Long userId, @Valid AppFavoritePageReqVO reqVO); PageResult<ProductFavoriteDO> getFavoritePage(Long userId, @Valid AppFavoritePageReqVO reqVO);
// TODO @Jason 这个方法最好返回 FavoriteDO 就好Service 要通用
/** /**
* 检查是否收藏过商品 * 获取收藏过商品
* *
* @param userId 用户id * @param userId 用户id
* @param reqVO 请求 vo * @param reqVO 请求 vo
* @return true: 已收藏 false: 未收藏
*/ */
Boolean checkFavorite(Long userId, @Valid AppFavoriteReqVO reqVO); ProductFavoriteDO getFavorite(Long userId, @Valid AppFavoriteReqVO reqVO);
} }

View File

@ -35,68 +35,36 @@ import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.FAVORITE_
public class ProductFavoriteServiceImpl implements ProductFavoriteService { public class ProductFavoriteServiceImpl implements ProductFavoriteService {
@Resource @Resource
private ProductSpuService productSpuService; private ProductFavoriteMapper productFavoriteMapper;
// TODO @JASONProductFavoriteMapper 最好全一点哈productFavoriteMapper
@Resource
private ProductFavoriteMapper mapper;
@Override @Override
public Boolean createFavorite(Long userId, @Valid AppFavoriteReqVO reqVO) { public Long createFavorite(Long userId, @Valid AppFavoriteReqVO reqVO) {
// TODO @JASONuserId 校验可以去掉直接在 Controller 要求登录通过 @PreAuthenticated 注解 ProductFavoriteDO favoriteDO = productFavoriteMapper.selectByUserAndSpuAndType(userId, reqVO.getSpuId(), reqVO.getType());
Assert.notNull(userId, "the userId must not be null");
ProductFavoriteDO favoriteDO = mapper.selectByUserAndSpuAndType(userId, reqVO.getSpuId(), reqVO.getType());
if (Objects.nonNull(favoriteDO)) { if (Objects.nonNull(favoriteDO)) {
throw exception(FAVORITE_EXISTS); throw exception(FAVORITE_EXISTS);
} }
ProductFavoriteDO entity = ProductFavoriteConvert.INSTANCE.convert(userId, reqVO); ProductFavoriteDO entity = ProductFavoriteConvert.INSTANCE.convert(userId, reqVO);
mapper.insert(entity); productFavoriteMapper.insert(entity);
// TODO @JASON可以返回 Long id return entity.getId();
return Boolean.TRUE;
} }
@Override @Override
public Boolean deleteFavorite(Long userId, @Valid AppFavoriteReqVO reqVO) { public void deleteFavorite(Long userId, @Valid AppFavoriteReqVO reqVO) {
Assert.notNull(userId, "the userId must not be null "); ProductFavoriteDO favoriteDO = productFavoriteMapper.selectByUserAndSpuAndType(userId, reqVO.getSpuId(), reqVO.getType());
ProductFavoriteDO favoriteDO = mapper.selectByUserAndSpuAndType(userId, reqVO.getSpuId(), reqVO.getType());
if (Objects.isNull(favoriteDO)) { if (Objects.isNull(favoriteDO)) {
throw exception(FAVORITE_NOT_EXISTS); throw exception(FAVORITE_NOT_EXISTS);
} }
mapper.deleteById(favoriteDO.getId()); productFavoriteMapper.deleteById(favoriteDO.getId());
// TODO 可以不返回
return Boolean.TRUE;
} }
@Override @Override
public PageResult<AppFavoriteRespVO> getFavoritePage(Long userId, @Valid AppFavoritePageReqVO reqVO) { public PageResult<ProductFavoriteDO> getFavoritePage(Long userId, @Valid AppFavoritePageReqVO reqVO) {
Assert.notNull(userId, "the userId must not be null "); return productFavoriteMapper.selectPageByUserAndType(userId, reqVO.getType(), reqVO);
PageResult<ProductFavoriteDO> favorites = mapper.selectPageByUserAndType(userId, reqVO.getType(), reqVO);
// TODO @Jasonif return 更好例如说如果判断是空 return
if (favorites.getTotal() > 0) {
PageResult<AppFavoriteRespVO> pageResult = new PageResult<>(favorites.getTotal());
List<ProductFavoriteDO> list = favorites.getList();
// 得到商品 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);
return pageResult;
}else {
return PageResult.empty();
}
} }
@Override @Override
public Boolean checkFavorite(Long userId, @Valid AppFavoriteReqVO reqVO) { public ProductFavoriteDO getFavorite(Long userId, @Valid AppFavoriteReqVO reqVO) {
Assert.notNull(userId, "the userId must not be null "); return productFavoriteMapper.selectByUserAndSpuAndType(userId, reqVO.getSpuId(), reqVO.getType());
ProductFavoriteDO favoriteDO = mapper.selectByUserAndSpuAndType(userId, reqVO.getSpuId(), reqVO.getType());
return Objects.nonNull(favoriteDO);
} }
} }