REVIEW 商品收藏的逻辑

This commit is contained in:
YunaiV 2023-05-05 23:38:31 +08:00
parent cd86620b74
commit 96e2bf020f
10 changed files with 43 additions and 43 deletions

View File

@ -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, "商品收藏不存在");

View File

@ -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();
/**
* 类型
*/

View File

@ -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 @jasonpage分页
@GetMapping(value = "/collectList")
@Operation(summary = "商品收藏列表")
public CommonResult<PageResult<AppFavoriteRespVO>> pageCollectList(AppFavoritePageReqVO reqVO) {
return success(productFavoriteService.pageCollectList(reqVO));
}
// TODO @json需要在给一个用户查询某个商品是否收藏详情页要用
}

View File

@ -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;
}

View File

@ -9,12 +9,7 @@ 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 {
@ -22,8 +17,9 @@ public class AppFavoriteReqVO {
@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;
}

View File

@ -1,22 +1,26 @@
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 @jasonrequired true
@Schema(description = "编号", example = "1")
private Long id;
// TODO @jasonrequired true
@Schema(description = "商品 SPU 编号", example = "29502")
private Long spuId;
// TODO @jasonrequired true
@Schema(description = "类型", example = "1")
private Integer type;
// ========== 商品相关字段 ==========
@Schema(description = "商品 SPU 名称", example = "赵六")
private String spuName;
@ -26,6 +30,4 @@ public class AppFavoriteRespVO {
@Schema(description = "商品单价", example = "100")
private Integer price;
@Schema(description = "类型 1:收藏 2点赞", example = "1")
private Integer type;
}

View File

@ -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 {

View File

@ -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;

View File

@ -12,11 +12,6 @@ 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> {
@ -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);

View File

@ -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 @jasonuserId 要从 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);
}
}