mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2024-11-22 23:31:52 +08:00
REVIEW 商品收藏的逻辑
This commit is contained in:
parent
cd86620b74
commit
96e2bf020f
@ -48,7 +48,7 @@ public interface ErrorCodeConstants {
|
||||
ErrorCode COMMENT_ERROR_OPT = new ErrorCode(1008007002, "商品评价非法操作");
|
||||
ErrorCode COMMENT_ADDITIONAL_EXISTS = new ErrorCode(1008007003, "商品追加评价已存在");
|
||||
|
||||
// ========== 喜爱商品 1008008000 ==========
|
||||
// ========== 商品 收藏 1008008000 ==========
|
||||
ErrorCode COLLECTION_EXISTS = new ErrorCode(1008008000, "该商品已经被收藏");
|
||||
ErrorCode COLLECTION_NOT_EXISTS = new ErrorCode(1008008001, "商品收藏不存在");
|
||||
|
||||
|
@ -7,17 +7,19 @@ 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();
|
||||
|
||||
/**
|
||||
* 类型
|
||||
*/
|
||||
|
@ -18,10 +18,7 @@ import java.util.Objects;
|
||||
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
||||
import static cn.iocoder.yudao.module.product.enums.favorite.ProductFavoriteTypeEnum.COLLECT;
|
||||
|
||||
/**
|
||||
* @author jason
|
||||
*/
|
||||
@Tag(name = "用户 APP - 喜爱商品")
|
||||
@Tag(name = "用户 APP - 商品收藏")
|
||||
@RestController
|
||||
@RequestMapping("/product/favorite")
|
||||
public class AppFavoriteController {
|
||||
@ -29,6 +26,7 @@ public class AppFavoriteController {
|
||||
@Resource
|
||||
private ProductFavoriteService productFavoriteService;
|
||||
|
||||
// TODO @jason:创建;create
|
||||
@PostMapping(value = "/collect")
|
||||
@Operation(summary = "商品收藏")
|
||||
public CommonResult<Boolean> collect(@RequestBody @Valid AppFavoriteReqVO reqVO) {
|
||||
@ -36,16 +34,22 @@ public class AppFavoriteController {
|
||||
return success(productFavoriteService.collect(reqVO));
|
||||
}
|
||||
|
||||
// TODO @jason:创建;delete;使用 @DeleteMapping
|
||||
@PostMapping(value = "/cancelCollect")
|
||||
@Operation(summary = "取消商品收藏(通过商品详情)")
|
||||
public CommonResult<Boolean> cancelCollect(@RequestBody @Valid AppFavoriteReqVO reqVO) {
|
||||
// TODO @jason:是不是不用校验呀?
|
||||
Assert.isTrue(Objects.equals(COLLECT.getType(), reqVO.getType()), "参数type 不匹配");
|
||||
return success(productFavoriteService.cancelCollect(reqVO));
|
||||
}
|
||||
|
||||
// TODO @jason:page;分页
|
||||
@GetMapping(value = "/collectList")
|
||||
@Operation(summary = "商品收藏列表")
|
||||
public CommonResult<PageResult<AppFavoriteRespVO>> pageCollectList(AppFavoritePageReqVO reqVO) {
|
||||
return success(productFavoriteService.pageCollectList(reqVO));
|
||||
}
|
||||
|
||||
// TODO @json:需要在给一个,用户查询某个商品是否收藏;详情页要用
|
||||
|
||||
}
|
||||
|
@ -10,15 +10,13 @@ import javax.validation.constraints.NotNull;
|
||||
|
||||
import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED;
|
||||
|
||||
/**
|
||||
* @author jason
|
||||
*/
|
||||
@Schema(description = "用户APP - 喜爱商品分页查询 Request VO")
|
||||
@Schema(description = "用户APP - 商品收藏分页查询 Request VO")
|
||||
@Data
|
||||
public class AppFavoritePageReqVO extends PageParam {
|
||||
|
||||
@Schema(description = "类型 1:收藏 2:点赞", requiredMode = REQUIRED, example = "1")
|
||||
@Schema(description = "类型", requiredMode = REQUIRED, example = "1")
|
||||
@NotNull(message = "类型不能为空")
|
||||
@InEnum(ProductFavoriteTypeEnum.class)
|
||||
private Integer type;
|
||||
|
||||
}
|
||||
|
@ -9,21 +9,17 @@ import javax.validation.constraints.NotNull;
|
||||
|
||||
import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @author jason
|
||||
*/
|
||||
@Schema(description = "用户APP - 喜爱商品创建 Request VO")
|
||||
@Schema(description = "用户 APP - 商品收藏创建 Request VO")
|
||||
@Data
|
||||
public class AppFavoriteReqVO {
|
||||
|
||||
@Schema(description = "商品SPU编号", requiredMode = REQUIRED, example = "29502")
|
||||
@NotNull(message = "商品SPU编号不能为空")
|
||||
@Schema(description = "商品 SPU 编号", requiredMode = REQUIRED, example = "29502")
|
||||
@NotNull(message = "商品 SPU 编号不能为空")
|
||||
private Long spuId;
|
||||
|
||||
@Schema(description = "类型 1:收藏 2:点赞", requiredMode = REQUIRED, example = "1")
|
||||
@Schema(description = "类型", requiredMode = REQUIRED, example = "1")
|
||||
@NotNull(message = "类型不能为空")
|
||||
@InEnum(ProductFavoriteTypeEnum.class)
|
||||
private Integer type;
|
||||
|
||||
}
|
||||
|
@ -1,23 +1,27 @@
|
||||
package cn.iocoder.yudao.module.product.controller.app.favorite.vo;
|
||||
|
||||
import cn.iocoder.yudao.module.product.dal.dataobject.sku.ProductSkuDO;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author jason
|
||||
*/
|
||||
@Schema(description = "用户APP - 喜爱商品 Response VO")
|
||||
@Schema(description = "用户APP - 商品收藏 Response VO")
|
||||
@Data
|
||||
public class AppFavoriteRespVO {
|
||||
|
||||
// TODO @jason:required true 哈
|
||||
@Schema(description = "编号", example = "1")
|
||||
private Long id;
|
||||
|
||||
@Schema(description = "商品SPU编号", example = "29502")
|
||||
// TODO @jason:required true 哈
|
||||
@Schema(description = "商品 SPU 编号", example = "29502")
|
||||
private Long spuId;
|
||||
|
||||
@Schema(description = "商品SPU名称", example = "赵六")
|
||||
// TODO @jason:required true 哈
|
||||
@Schema(description = "类型", example = "1")
|
||||
private Integer type;
|
||||
|
||||
// ========== 商品相关字段 ==========
|
||||
|
||||
@Schema(description = "商品 SPU 名称", example = "赵六")
|
||||
private String spuName;
|
||||
|
||||
@Schema(description = "商品封面图", example = "https://domain/pic.png")
|
||||
@ -26,6 +30,4 @@ public class AppFavoriteRespVO {
|
||||
@Schema(description = "商品单价", example = "100")
|
||||
private Integer price;
|
||||
|
||||
@Schema(description = "类型 1:收藏 2:点赞", example = "1")
|
||||
private Integer type;
|
||||
}
|
||||
|
@ -5,11 +5,6 @@ import cn.iocoder.yudao.module.product.dal.dataobject.favorite.ProductFavoriteDO
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
/**
|
||||
* 喜爱商品 Convert
|
||||
*
|
||||
* @author jason
|
||||
*/
|
||||
@Mapper
|
||||
public interface ProductFavoriteConvert {
|
||||
|
||||
|
@ -20,7 +20,7 @@ import lombok.*;
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class ProductFavoriteDO extends TenantBaseDO {
|
||||
public class ProductFavoriteDO extends TenantBaseDO { // TODO @jason:如无必要,使用 BaseDO 哈。例如说 tenant_id 在业务里,是否需要使用到
|
||||
|
||||
/**
|
||||
* 编号,主键自增
|
||||
@ -40,7 +40,7 @@ public class ProductFavoriteDO extends TenantBaseDO {
|
||||
*/
|
||||
private Long spuId;
|
||||
/**
|
||||
* 类型 1 收藏;2 点赞
|
||||
* 类型 1 收藏;2 点赞 // TODO @jason:不要注释 1 收藏 2 点赞;而是注释好,它对应的枚举类
|
||||
*/
|
||||
private Integer type;
|
||||
|
||||
|
@ -12,15 +12,10 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
/**
|
||||
* 喜爱商品 Mapper
|
||||
*
|
||||
* @author jason
|
||||
*/
|
||||
@Mapper
|
||||
public interface ProductFavoriteMapper extends BaseMapperX<ProductFavoriteDO> {
|
||||
|
||||
default ProductFavoriteDO selectByUserAndSpuAndType(Long userId, Long spuId, Integer type){
|
||||
default ProductFavoriteDO selectByUserAndSpuAndType(Long userId, Long spuId, Integer type) {
|
||||
Assert.notNull(userId, "the userId argument must not be null");
|
||||
Assert.notNull(spuId, "the spuId argument must not be null");
|
||||
Assert.notNull(type, "the type argument must not be null");
|
||||
@ -36,6 +31,7 @@ public interface ProductFavoriteMapper extends BaseMapperX<ProductFavoriteDO> {
|
||||
return new PageResult<>(page.getRecords(), page.getTotal());
|
||||
}
|
||||
|
||||
// TODO @jason:内存中拼接哈。这样好兼容更多的 db 类型;
|
||||
Page<AppFavoriteRespVO> selectFavoriteProductList(Page<AppFavoriteRespVO> page,
|
||||
@Param("userId") Long userId,
|
||||
@Param("type") Integer type);
|
||||
|
@ -20,7 +20,7 @@ import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.COLLECTIO
|
||||
import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.COLLECTION_NOT_EXISTS;
|
||||
|
||||
/**
|
||||
* 喜爱商品 Service 实现类
|
||||
* 商品收藏 Service 实现类
|
||||
*
|
||||
* @author jason
|
||||
*/
|
||||
@ -33,11 +33,15 @@ public class ProductFavoriteServiceImpl implements ProductFavoriteService {
|
||||
|
||||
@Override
|
||||
public Boolean collect(@Valid AppFavoriteReqVO reqVO) {
|
||||
// TODO @jason:userId 要从 Controller 传递过来,Service 不能有转台
|
||||
Long userId = getLoginUserId();
|
||||
// TODO @jason:代码缩进不对;
|
||||
ProductFavoriteDO favoriteDO = mapper.selectByUserAndSpuAndType(userId, reqVO.getSpuId(), reqVO.getType());
|
||||
if (Objects.nonNull(favoriteDO)) {
|
||||
throw exception(COLLECTION_EXISTS);
|
||||
}
|
||||
|
||||
// TODO @jason:插入只有成功,不用判断 1
|
||||
ProductFavoriteDO entity = ProductFavoriteConvert.INSTANCE.convert(userId, reqVO);
|
||||
int count = mapper.insert(entity);
|
||||
return count == 1;
|
||||
@ -45,11 +49,13 @@ public class ProductFavoriteServiceImpl implements ProductFavoriteService {
|
||||
|
||||
@Override
|
||||
public Boolean cancelCollect(@Valid AppFavoriteReqVO reqVO) {
|
||||
// TODO @jason:代码缩进不对;
|
||||
Long loginUserId = getLoginUserId();
|
||||
ProductFavoriteDO favoriteDO = mapper.selectByUserAndSpuAndType(loginUserId, reqVO.getSpuId(), reqVO.getType());
|
||||
if (Objects.isNull(favoriteDO)) {
|
||||
throw exception(COLLECTION_NOT_EXISTS);
|
||||
}
|
||||
// TODO @jason:插入只有成功,不用判断 1
|
||||
int count = mapper.deleteById(favoriteDO.getId());
|
||||
return count == 1;
|
||||
}
|
||||
@ -59,4 +65,5 @@ public class ProductFavoriteServiceImpl implements ProductFavoriteService {
|
||||
Long userId = getLoginUserId();
|
||||
return mapper.selectPageByUserAndType(userId, reqVO.getType(), reqVO);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user