MALL:简化商品分类的 VO 转化

This commit is contained in:
YunaiV 2024-01-13 22:58:24 +08:00
parent d58da7b2b0
commit ed34b18d82
11 changed files with 50 additions and 121 deletions

View File

@ -1,22 +1,21 @@
package cn.iocoder.yudao.module.product.controller.admin.category;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryCreateReqVO;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryListReqVO;
import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryRespVO;
import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryUpdateReqVO;
import cn.iocoder.yudao.module.product.convert.category.ProductCategoryConvert;
import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategorySaveReqVO;
import cn.iocoder.yudao.module.product.dal.dataobject.category.ProductCategoryDO;
import cn.iocoder.yudao.module.product.service.category.ProductCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import java.util.Comparator;
import java.util.List;
@ -34,14 +33,14 @@ public class ProductCategoryController {
@PostMapping("/create")
@Operation(summary = "创建商品分类")
@PreAuthorize("@ss.hasPermission('product:category:create')")
public CommonResult<Long> createCategory(@Valid @RequestBody ProductCategoryCreateReqVO createReqVO) {
public CommonResult<Long> createCategory(@Valid @RequestBody ProductCategorySaveReqVO createReqVO) {
return success(categoryService.createCategory(createReqVO));
}
@PutMapping("/update")
@Operation(summary = "更新商品分类")
@PreAuthorize("@ss.hasPermission('product:category:update')")
public CommonResult<Boolean> updateCategory(@Valid @RequestBody ProductCategoryUpdateReqVO updateReqVO) {
public CommonResult<Boolean> updateCategory(@Valid @RequestBody ProductCategorySaveReqVO updateReqVO) {
categoryService.updateCategory(updateReqVO);
return success(true);
}
@ -61,7 +60,7 @@ public class ProductCategoryController {
@PreAuthorize("@ss.hasPermission('product:category:query')")
public CommonResult<ProductCategoryRespVO> getCategory(@RequestParam("id") Long id) {
ProductCategoryDO category = categoryService.getCategory(id);
return success(ProductCategoryConvert.INSTANCE.convert(category));
return success(BeanUtils.toBean(category, ProductCategoryRespVO.class));
}
@GetMapping("/list")
@ -70,7 +69,7 @@ public class ProductCategoryController {
public CommonResult<List<ProductCategoryRespVO>> getCategoryList(@Valid ProductCategoryListReqVO listReqVO) {
List<ProductCategoryDO> list = categoryService.getCategoryList(listReqVO);
list.sort(Comparator.comparing(ProductCategoryDO::getSort));
return success(ProductCategoryConvert.INSTANCE.convertList(list));
return success(BeanUtils.toBean(list, ProductCategoryRespVO.class));
}
}

View File

@ -1,19 +0,0 @@
package cn.iocoder.yudao.module.product.controller.admin.category.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import jakarta.validation.constraints.NotBlank;
@Schema(description = "管理后台 - 商品分类创建 Request VO")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class ProductCategoryCreateReqVO extends ProductCategoryBaseVO {
@Schema(description = "分类描述", example = "描述")
private String description;
}

View File

@ -2,20 +2,34 @@ package cn.iocoder.yudao.module.product.controller.admin.category.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import java.time.LocalDateTime;
@Schema(description = "管理后台 - 商品分类 Response VO")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class ProductCategoryRespVO extends ProductCategoryBaseVO {
public class ProductCategoryRespVO {
@Schema(description = "分类编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "2")
private Long id;
@Schema(description = "父分类编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
private Long parentId;
@Schema(description = "分类名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "办公文具")
private String name;
@Schema(description = "移动端分类图", requiredMode = Schema.RequiredMode.REQUIRED)
private String picUrl;
@Schema(description = "分类排序", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
private Integer sort;
@Schema(description = "开启状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "0")
private Integer status;
@Schema(description = "分类描述", example = "描述")
private String description;
@Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED)
private LocalDateTime createTime;

View File

@ -1,17 +1,16 @@
package cn.iocoder.yudao.module.product.controller.admin.category.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
/**
* 商品分类 Base VO提供给添加修改详细的子 VO 使用
* 如果子 VO 存在差异的字段请不要添加到这里影响 Swagger 文档生成
*/
@Schema(description = "管理后台 - 商品分类新增/更新 Request VO")
@Data
public class ProductCategoryBaseVO {
public class ProductCategorySaveReqVO {
@Schema(description = "分类编号", example = "2")
private Long id;
@Schema(description = "父分类编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
@NotNull(message = "父分类编号不能为空")
@ -25,9 +24,6 @@ public class ProductCategoryBaseVO {
@NotBlank(message = "移动端分类图不能为空")
private String picUrl;
@Schema(description = "PC 端分类图")
private String bigPicUrl;
@Schema(description = "分类排序", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
private Integer sort;
@ -35,4 +31,7 @@ public class ProductCategoryBaseVO {
@NotNull(message = "开启状态不能为空")
private Integer status;
@Schema(description = "分类描述", example = "描述")
private String description;
}

View File

@ -1,24 +0,0 @@
package cn.iocoder.yudao.module.product.controller.admin.category.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
@Schema(description = "管理后台 - 商品分类更新 Request VO")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class ProductCategoryUpdateReqVO extends ProductCategoryBaseVO {
@Schema(description = "分类编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "2")
@NotNull(message = "分类编号不能为空")
private Long id;
@Schema(description = "分类描述", example = "描述")
private String description;
}

View File

@ -4,7 +4,6 @@ import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.product.controller.app.category.vo.AppCategoryRespVO;
import cn.iocoder.yudao.module.product.convert.category.ProductCategoryConvert;
import cn.iocoder.yudao.module.product.dal.dataobject.category.ProductCategoryDO;
import cn.iocoder.yudao.module.product.service.category.ProductCategoryService;
import io.swagger.v3.oas.annotations.Operation;

View File

@ -1,31 +0,0 @@
package cn.iocoder.yudao.module.product.convert.category;
import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryCreateReqVO;
import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryRespVO;
import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryUpdateReqVO;
import cn.iocoder.yudao.module.product.controller.app.category.vo.AppCategoryRespVO;
import cn.iocoder.yudao.module.product.dal.dataobject.category.ProductCategoryDO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import java.util.List;
/**
* 商品分类 Convert
*
* @author 芋道源码
*/
@Mapper
public interface ProductCategoryConvert {
ProductCategoryConvert INSTANCE = Mappers.getMapper(ProductCategoryConvert.class);
ProductCategoryDO convert(ProductCategoryCreateReqVO bean);
ProductCategoryDO convert(ProductCategoryUpdateReqVO bean);
ProductCategoryRespVO convert(ProductCategoryDO bean);
List<ProductCategoryRespVO> convertList(List<ProductCategoryDO> list);
}

View File

@ -48,12 +48,6 @@ public class ProductCategoryDO extends BaseDO {
* 建议 180*180 分辨率
*/
private String picUrl;
/**
* PC 端分类图
*
* 建议 468*340 分辨率
*/
private String bigPicUrl;
/**
* 分类排序
*/

View File

@ -1,11 +1,10 @@
package cn.iocoder.yudao.module.product.service.category;
import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryCreateReqVO;
import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryListReqVO;
import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryUpdateReqVO;
import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategorySaveReqVO;
import cn.iocoder.yudao.module.product.dal.dataobject.category.ProductCategoryDO;
import jakarta.validation.Valid;
import java.util.Collection;
import java.util.List;
@ -22,14 +21,14 @@ public interface ProductCategoryService {
* @param createReqVO 创建信息
* @return 编号
*/
Long createCategory(@Valid ProductCategoryCreateReqVO createReqVO);
Long createCategory(@Valid ProductCategorySaveReqVO createReqVO);
/**
* 更新商品分类
*
* @param updateReqVO 更新信息
*/
void updateCategory(@Valid ProductCategoryUpdateReqVO updateReqVO);
void updateCategory(@Valid ProductCategorySaveReqVO updateReqVO);
/**
* 删除商品分类

View File

@ -3,18 +3,17 @@ package cn.iocoder.yudao.module.product.service.category;
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryCreateReqVO;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryListReqVO;
import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryUpdateReqVO;
import cn.iocoder.yudao.module.product.convert.category.ProductCategoryConvert;
import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategorySaveReqVO;
import cn.iocoder.yudao.module.product.dal.dataobject.category.ProductCategoryDO;
import cn.iocoder.yudao.module.product.dal.mysql.category.ProductCategoryMapper;
import cn.iocoder.yudao.module.product.service.spu.ProductSpuService;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import jakarta.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;
@ -40,26 +39,26 @@ public class ProductCategoryServiceImpl implements ProductCategoryService {
private ProductSpuService productSpuService;
@Override
public Long createCategory(ProductCategoryCreateReqVO createReqVO) {
public Long createCategory(ProductCategorySaveReqVO createReqVO) {
// 校验父分类存在
validateParentProductCategory(createReqVO.getParentId());
// 插入
ProductCategoryDO category = ProductCategoryConvert.INSTANCE.convert(createReqVO);
ProductCategoryDO category = BeanUtils.toBean(createReqVO, ProductCategoryDO.class);
productCategoryMapper.insert(category);
// 返回
return category.getId();
}
@Override
public void updateCategory(ProductCategoryUpdateReqVO updateReqVO) {
public void updateCategory(ProductCategorySaveReqVO updateReqVO) {
// 校验分类是否存在
validateProductCategoryExists(updateReqVO.getId());
// 校验父分类存在
validateParentProductCategory(updateReqVO.getParentId());
// 更新
ProductCategoryDO updateObj = ProductCategoryConvert.INSTANCE.convert(updateReqVO);
ProductCategoryDO updateObj = BeanUtils.toBean(updateReqVO, ProductCategoryDO.class);
productCategoryMapper.updateById(updateObj);
}

View File

@ -4,7 +4,7 @@ import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryCreateReqVO;
import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryListReqVO;
import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryUpdateReqVO;
import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategorySaveReqVO;
import cn.iocoder.yudao.module.product.dal.dataobject.category.ProductCategoryDO;
import cn.iocoder.yudao.module.product.dal.mysql.category.ProductCategoryMapper;
import org.junit.jupiter.api.Disabled;
@ -65,7 +65,7 @@ public class ProductCategoryServiceImplTest extends BaseDbUnitTest {
ProductCategoryDO dbCategory = randomPojo(ProductCategoryDO.class);
productCategoryMapper.insert(dbCategory);// @Sql: 先插入出一条存在的数据
// 准备参数
ProductCategoryUpdateReqVO reqVO = randomPojo(ProductCategoryUpdateReqVO.class, o -> {
ProductCategorySaveReqVO reqVO = randomPojo(ProductCategorySaveReqVO.class, o -> {
o.setId(dbCategory.getId()); // 设置更新的 ID
});
// mock 父类
@ -82,7 +82,7 @@ public class ProductCategoryServiceImplTest extends BaseDbUnitTest {
@Test
public void testUpdateCategory_notExists() {
// 准备参数
ProductCategoryUpdateReqVO reqVO = randomPojo(ProductCategoryUpdateReqVO.class);
ProductCategorySaveReqVO reqVO = randomPojo(ProductCategorySaveReqVO.class);
// 调用, 并断言异常
assertServiceException(() -> productCategoryService.updateCategory(reqVO), CATEGORY_NOT_EXISTS);