mall + product:简化收藏逻辑,去掉 type 字段

This commit is contained in:
YunaiV 2023-06-09 20:44:00 +08:00
parent 25bea4920c
commit 84b365a707
10 changed files with 30 additions and 102 deletions

View File

@ -1,36 +0,0 @@
package cn.iocoder.yudao.module.product.enums.favorite;
import cn.iocoder.yudao.framework.common.core.IntArrayValuable;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.util.Arrays;
/**
* 商品收藏的类型枚举
*
* @author jason
*/
@Getter
@AllArgsConstructor
public enum ProductFavoriteTypeEnum implements IntArrayValuable {
COLLECT(1,"收藏"),
THUMBS_UP(2, "点赞");
public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(ProductFavoriteTypeEnum::getType).toArray();
/**
* 类型
*/
private final Integer type;
/**
* 描述
*/
private final String desc;
@Override
public int[] array() {
return ARRAYS;
}
}

View File

@ -3,6 +3,7 @@ package cn.iocoder.yudao.module.product.controller.app.favorite;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
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.security.core.annotations.PreAuthenticated;
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;
@ -35,21 +36,23 @@ public class AppFavoriteController {
private ProductSpuService productSpuService; private ProductSpuService productSpuService;
@PostMapping(value = "/create") @PostMapping(value = "/create")
@Operation(summary = "商品收藏") @Operation(summary = "添加商品收藏")
//@PreAuthenticated TODO 暂时注释 @PreAuthenticated
public CommonResult<Long> createFavorite(@RequestBody @Valid AppFavoriteReqVO reqVO) { public CommonResult<Long> createFavorite(@RequestBody @Valid AppFavoriteReqVO reqVO) {
return success(productFavoriteService.createFavorite(getLoginUserId(), reqVO)); return success(productFavoriteService.createFavorite(getLoginUserId(), reqVO.getSpuId()));
} }
@DeleteMapping(value = "/delete") @DeleteMapping(value = "/delete")
@Operation(summary = "取消商品收藏") @Operation(summary = "取消商品收藏")
@PreAuthenticated
public CommonResult<Boolean> deleteFavorite(@RequestBody @Valid AppFavoriteReqVO reqVO) { public CommonResult<Boolean> deleteFavorite(@RequestBody @Valid AppFavoriteReqVO reqVO) {
productFavoriteService.deleteFavorite(getLoginUserId(), reqVO); productFavoriteService.deleteFavorite(getLoginUserId(), reqVO.getSpuId());
return success(Boolean.TRUE); return success(Boolean.TRUE);
} }
@GetMapping(value = "/page") @GetMapping(value = "/page")
@Operation(summary = "分页获取商品收藏列表") @Operation(summary = "分页获取商品收藏列表")
@PreAuthenticated
public CommonResult<PageResult<AppFavoriteRespVO>> getFavoritePage(AppFavoritePageReqVO reqVO) { public CommonResult<PageResult<AppFavoriteRespVO>> getFavoritePage(AppFavoritePageReqVO reqVO) {
PageResult<ProductFavoriteDO> favoritePage = productFavoriteService.getFavoritePage(getLoginUserId(), reqVO); PageResult<ProductFavoriteDO> favoritePage = productFavoriteService.getFavoritePage(getLoginUserId(), reqVO);
if (CollUtil.isEmpty(favoritePage.getList())) { if (CollUtil.isEmpty(favoritePage.getList())) {
@ -67,11 +70,11 @@ public class AppFavoriteController {
return success(pageResult); return success(pageResult);
} }
@GetMapping(value = "/exits") @GetMapping(value = "/exits")
@Operation(summary = "检查是否收藏过商品") @Operation(summary = "检查是否收藏过商品")
@PreAuthenticated
public CommonResult<Boolean> isFavoriteExists(AppFavoriteReqVO reqVO) { public CommonResult<Boolean> isFavoriteExists(AppFavoriteReqVO reqVO) {
ProductFavoriteDO favoriteDO = productFavoriteService.getFavorite(getLoginUserId(), reqVO); ProductFavoriteDO favoriteDO = productFavoriteService.getFavorite(getLoginUserId(), reqVO.getSpuId());
return success(Objects.nonNull(favoriteDO)); return success(Objects.nonNull(favoriteDO));
} }

View File

@ -1,22 +1,10 @@
package cn.iocoder.yudao.module.product.controller.app.favorite.vo; package cn.iocoder.yudao.module.product.controller.app.favorite.vo;
import cn.iocoder.yudao.framework.common.pojo.PageParam; import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.validation.InEnum;
import cn.iocoder.yudao.module.product.enums.favorite.ProductFavoriteTypeEnum;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data; import lombok.Data;
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 @Data
public class AppFavoritePageReqVO extends PageParam { public class AppFavoritePageReqVO extends PageParam {
@Schema(description = "类型", requiredMode = REQUIRED, example = "1")
@NotNull(message = "类型不能为空")
@InEnum(ProductFavoriteTypeEnum.class)
private Integer type;
} }

View File

@ -1,7 +1,5 @@
package cn.iocoder.yudao.module.product.controller.app.favorite.vo; package cn.iocoder.yudao.module.product.controller.app.favorite.vo;
import cn.iocoder.yudao.framework.common.validation.InEnum;
import cn.iocoder.yudao.module.product.enums.favorite.ProductFavoriteTypeEnum;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data; import lombok.Data;
@ -17,9 +15,4 @@ public class AppFavoriteReqVO {
@NotNull(message = "商品 SPU 编号不能为空") @NotNull(message = "商品 SPU 编号不能为空")
private Long spuId; private Long spuId;
@Schema(description = "类型", requiredMode = REQUIRED, example = "1")
@NotNull(message = "类型不能为空")
@InEnum(ProductFavoriteTypeEnum.class)
private Integer type;
} }

View File

@ -14,9 +14,6 @@ public class AppFavoriteRespVO {
@Schema(description = "商品 SPU 编号", requiredMode = REQUIRED, example = "29502") @Schema(description = "商品 SPU 编号", requiredMode = REQUIRED, example = "29502")
private Long spuId; private Long spuId;
@Schema(description = "类型", requiredMode = REQUIRED, example = "1")
private Integer type;
// ========== 商品相关字段 ========== // ========== 商品相关字段 ==========
@Schema(description = "商品 SPU 名称", example = "赵六") @Schema(description = "商品 SPU 名称", example = "赵六")

View File

@ -1,6 +1,5 @@
package cn.iocoder.yudao.module.product.convert.favorite; package cn.iocoder.yudao.module.product.convert.favorite;
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;
import cn.iocoder.yudao.module.product.dal.dataobject.spu.ProductSpuDO; import cn.iocoder.yudao.module.product.dal.dataobject.spu.ProductSpuDO;
@ -19,7 +18,7 @@ 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, Long spuId);
@Mapping(target = "id", source = "favorite.id") @Mapping(target = "id", source = "favorite.id")
@Mapping(target = "spuName", source = "spu.name") @Mapping(target = "spuName", source = "spu.name")

View File

@ -2,7 +2,6 @@ package cn.iocoder.yudao.module.product.dal.dataobject.favorite;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO; import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.module.product.dal.dataobject.spu.ProductSpuDO; import cn.iocoder.yudao.module.product.dal.dataobject.spu.ProductSpuDO;
import cn.iocoder.yudao.module.product.enums.favorite.ProductFavoriteTypeEnum;
import com.baomidou.mybatisplus.annotation.KeySequence; import com.baomidou.mybatisplus.annotation.KeySequence;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
@ -40,11 +39,5 @@ public class ProductFavoriteDO extends BaseDO {
* 关联 {@link ProductSpuDO#getId()} * 关联 {@link ProductSpuDO#getId()}
*/ */
private Long spuId; private Long spuId;
/**
* 类型
*
* 枚举 {@link ProductFavoriteTypeEnum}
*/
private Integer type;
} }

View File

@ -1,9 +1,8 @@
package cn.iocoder.yudao.module.product.dal.mysql.favorite; package cn.iocoder.yudao.module.product.dal.mysql.favorite;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoritePageReqVO;
import cn.iocoder.yudao.module.product.dal.dataobject.favorite.ProductFavoriteDO; import cn.iocoder.yudao.module.product.dal.dataobject.favorite.ProductFavoriteDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
@ -11,17 +10,14 @@ import org.apache.ibatis.annotations.Mapper;
@Mapper @Mapper
public interface ProductFavoriteMapper extends BaseMapperX<ProductFavoriteDO> { public interface ProductFavoriteMapper extends BaseMapperX<ProductFavoriteDO> {
default ProductFavoriteDO selectByUserAndSpuAndType(Long userId, Long spuId, Integer type) { default ProductFavoriteDO selectByUserIdAndSpuId(Long userId, Long spuId) {
return selectOne(new LambdaQueryWrapperX<ProductFavoriteDO>() return selectOne(ProductFavoriteDO::getUserId, userId,
.eq(ProductFavoriteDO::getUserId, userId) ProductFavoriteDO::getSpuId, spuId);
.eq(ProductFavoriteDO::getSpuId, spuId)
.eq(ProductFavoriteDO::getType, type));
} }
default PageResult<ProductFavoriteDO> selectPageByUserAndType(Long userId, Integer type, PageParam pageParam) { default PageResult<ProductFavoriteDO> selectPageByUserAndType(Long userId, AppFavoritePageReqVO reqVO) {
return selectPage(pageParam, new LambdaQueryWrapper<ProductFavoriteDO>() return selectPage(reqVO, new LambdaQueryWrapper<ProductFavoriteDO>()
.eq(ProductFavoriteDO::getUserId, userId) .eq(ProductFavoriteDO::getUserId, userId)
.eq(ProductFavoriteDO::getType, type)
.orderByDesc(ProductFavoriteDO::getId)); .orderByDesc(ProductFavoriteDO::getId));
} }

View File

@ -2,8 +2,6 @@ package cn.iocoder.yudao.module.product.service.favorite;
import cn.iocoder.yudao.framework.common.pojo.PageResult; 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.AppFavoriteRespVO;
import cn.iocoder.yudao.module.product.dal.dataobject.favorite.ProductFavoriteDO; import cn.iocoder.yudao.module.product.dal.dataobject.favorite.ProductFavoriteDO;
import javax.validation.Valid; import javax.validation.Valid;
@ -19,17 +17,17 @@ public interface ProductFavoriteService {
* 创建商品收藏 * 创建商品收藏
* *
* @param userId 用户 id * @param userId 用户 id
* @param reqVO 请求 vo * @param spuId SPU 编号
*/ */
Long createFavorite(Long userId, @Valid AppFavoriteReqVO reqVO); Long createFavorite(Long userId, Long spuId);
/** /**
* 取消商品收藏 * 取消商品收藏
* *
* @param userId 用户 id * @param userId 用户 id
* @param reqVO 请求 vo * @param spuId SPU 编号
*/ */
void deleteFavorite(Long userId, @Valid AppFavoriteReqVO reqVO); void deleteFavorite(Long userId, Long spuId);
/** /**
* 分页查询用户收藏列表 * 分页查询用户收藏列表
@ -43,8 +41,8 @@ public interface ProductFavoriteService {
* 获取收藏过商品 * 获取收藏过商品
* *
* @param userId 用户id * @param userId 用户id
* @param reqVO 请求 vo * @param spuId SPU 编号
*/ */
ProductFavoriteDO getFavorite(Long userId, @Valid AppFavoriteReqVO reqVO); ProductFavoriteDO getFavorite(Long userId, Long spuId);
} }

View File

@ -2,7 +2,6 @@ package cn.iocoder.yudao.module.product.service.favorite;
import cn.iocoder.yudao.framework.common.pojo.PageResult; 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.convert.favorite.ProductFavoriteConvert; 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.favorite.ProductFavoriteDO;
import cn.iocoder.yudao.module.product.dal.mysql.favorite.ProductFavoriteMapper; import cn.iocoder.yudao.module.product.dal.mysql.favorite.ProductFavoriteMapper;
@ -30,22 +29,20 @@ public class ProductFavoriteServiceImpl implements ProductFavoriteService {
private ProductFavoriteMapper productFavoriteMapper; private ProductFavoriteMapper productFavoriteMapper;
@Override @Override
public Long createFavorite(Long userId, @Valid AppFavoriteReqVO reqVO) { public Long createFavorite(Long userId, Long spuId) {
ProductFavoriteDO favorite = productFavoriteMapper.selectByUserAndSpuAndType( ProductFavoriteDO favorite = productFavoriteMapper.selectByUserIdAndSpuId(userId, spuId);
userId, reqVO.getSpuId(), reqVO.getType());
if (Objects.nonNull(favorite)) { if (Objects.nonNull(favorite)) {
throw exception(FAVORITE_EXISTS); throw exception(FAVORITE_EXISTS);
} }
ProductFavoriteDO entity = ProductFavoriteConvert.INSTANCE.convert(userId, reqVO); ProductFavoriteDO entity = ProductFavoriteConvert.INSTANCE.convert(userId, spuId);
productFavoriteMapper.insert(entity); productFavoriteMapper.insert(entity);
return entity.getId(); return entity.getId();
} }
@Override @Override
public void deleteFavorite(Long userId, @Valid AppFavoriteReqVO reqVO) { public void deleteFavorite(Long userId, Long spuId) {
ProductFavoriteDO favorite = productFavoriteMapper.selectByUserAndSpuAndType( ProductFavoriteDO favorite = productFavoriteMapper.selectByUserIdAndSpuId(userId, spuId);
userId, reqVO.getSpuId(), reqVO.getType());
if (Objects.isNull(favorite)) { if (Objects.isNull(favorite)) {
throw exception(FAVORITE_NOT_EXISTS); throw exception(FAVORITE_NOT_EXISTS);
} }
@ -55,12 +52,12 @@ public class ProductFavoriteServiceImpl implements ProductFavoriteService {
@Override @Override
public PageResult<ProductFavoriteDO> getFavoritePage(Long userId, @Valid AppFavoritePageReqVO reqVO) { public PageResult<ProductFavoriteDO> getFavoritePage(Long userId, @Valid AppFavoritePageReqVO reqVO) {
return productFavoriteMapper.selectPageByUserAndType(userId, reqVO.getType(), reqVO); return productFavoriteMapper.selectPageByUserAndType(userId, reqVO);
} }
@Override @Override
public ProductFavoriteDO getFavorite(Long userId, @Valid AppFavoriteReqVO reqVO) { public ProductFavoriteDO getFavorite(Long userId, Long spuId) {
return productFavoriteMapper.selectByUserAndSpuAndType(userId, reqVO.getSpuId(), reqVO.getType()); return productFavoriteMapper.selectByUserIdAndSpuId(userId, spuId);
} }
} }