diff --git a/.gitignore b/.gitignore index f6e0209..69b2a5f 100644 --- a/.gitignore +++ b/.gitignore @@ -48,3 +48,4 @@ nbdist/ /agileboot-admin/src/main/resources/application-prod.yml +/.svn diff --git a/agileboot-admin/src/main/java/com/agileboot/admin/controller/shop/ShopCategoryController.java b/agileboot-admin/src/main/java/com/agileboot/admin/controller/shop/ShopCategoryController.java index 47a1ee9..e3b1f70 100644 --- a/agileboot-admin/src/main/java/com/agileboot/admin/controller/shop/ShopCategoryController.java +++ b/agileboot-admin/src/main/java/com/agileboot/admin/controller/shop/ShopCategoryController.java @@ -10,11 +10,23 @@ import com.agileboot.domain.shop.category.query.SearchShopCategoryQuery; import io.swagger.v3.oas.annotations.Operation; import java.util.List; import lombok.RequiredArgsConstructor; +import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.agileboot.common.core.base.BaseController; +import com.agileboot.admin.customize.aop.accessLog.AccessLog; +import com.agileboot.common.enums.common.BusinessTypeEnum; +import com.agileboot.domain.common.command.BulkOperationCommand; +import com.agileboot.domain.shop.category.command.AddCategoryCommand; +import com.agileboot.domain.shop.category.command.UpdateCategoryCommand; +import javax.validation.constraints.NotNull; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; /** *

@@ -51,5 +63,34 @@ public class ShopCategoryController extends BaseController { List page = categoryApplicationService.getCategoryAll(); return ResponseDTO.ok(page); } + + @Operation(summary = "新增分类") +// @PreAuthorize("@permission.has('shop:category:add')") + @AccessLog(title = "分类管理", businessType = BusinessTypeEnum.ADD) + @PostMapping + public ResponseDTO add(@Validated @RequestBody AddCategoryCommand command) { + categoryApplicationService.addCategory(command); + return ResponseDTO.ok(); + } + + @Operation(summary = "删除分类") +// @PreAuthorize("@permission.has('shop:category:remove')") + @AccessLog(title = "分类管理", businessType = BusinessTypeEnum.DELETE) + @DeleteMapping("/{categoryIds}") + public ResponseDTO remove(@PathVariable @NotNull List categoryIds) { + BulkOperationCommand bulkDeleteCommand = new BulkOperationCommand<>(categoryIds); + categoryApplicationService.deleteCategory(bulkDeleteCommand); + return ResponseDTO.ok(); + } + + @Operation(summary = "修改分类") +// @PreAuthorize("@permission.has('shop:category:edit')") + @AccessLog(title = "分类管理", businessType = BusinessTypeEnum.MODIFY) + @PutMapping("/{categoryId}") + public ResponseDTO edit(@PathVariable Long categoryId, @Validated @RequestBody UpdateCategoryCommand command) { + command.setCategoryId(categoryId); + categoryApplicationService.updateCategory(command); + return ResponseDTO.ok(); + } } diff --git a/agileboot-domain/src/main/java/com/agileboot/domain/shop/category/CategoryApplicationService.java b/agileboot-domain/src/main/java/com/agileboot/domain/shop/category/CategoryApplicationService.java index d8d3bc2..394f665 100644 --- a/agileboot-domain/src/main/java/com/agileboot/domain/shop/category/CategoryApplicationService.java +++ b/agileboot-domain/src/main/java/com/agileboot/domain/shop/category/CategoryApplicationService.java @@ -10,11 +10,17 @@ import java.util.List; import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import com.agileboot.domain.common.command.BulkOperationCommand; +import com.agileboot.domain.shop.category.command.AddCategoryCommand; +import com.agileboot.domain.shop.category.command.UpdateCategoryCommand; +import com.agileboot.domain.shop.category.model.CategoryModel; +import com.agileboot.domain.shop.category.model.CategoryModelFactory; @Service @RequiredArgsConstructor public class CategoryApplicationService { private final ShopCategoryService shopCategoryService; + private final CategoryModelFactory categoryModelFactory; public PageDTO getCategoryList(SearchShopCategoryQuery query) { Page categoryPage = shopCategoryService.getCategoryList(query); @@ -30,5 +36,32 @@ public class CategoryApplicationService { .map(ShopCategoryDTO::new) .collect(Collectors.toList()); } + public void addCategory(AddCategoryCommand command) { + CategoryModel model = categoryModelFactory.create(); + model.loadAddCommand(command); + /* 暂未实现的校验逻辑 + model.checkCategoryNameUnique(); + */ + + model.insert(); + } + + public void updateCategory(UpdateCategoryCommand command) { + CategoryModel model = categoryModelFactory.loadById(command.getCategoryId()); + model.loadUpdateCommand(command); + + /* 暂未实现的校验逻辑 + model.checkCategoryNameUnique(); + */ + + model.updateById(); + } + + public void deleteCategory(BulkOperationCommand command) { + for (Long categoryId : command.getIds()) { + CategoryModel model = categoryModelFactory.loadById(categoryId); + model.deleteById(); + } + } } \ No newline at end of file diff --git a/agileboot-domain/src/main/java/com/agileboot/domain/shop/category/command/AddCategoryCommand.java b/agileboot-domain/src/main/java/com/agileboot/domain/shop/category/command/AddCategoryCommand.java new file mode 100644 index 0000000..595bfb4 --- /dev/null +++ b/agileboot-domain/src/main/java/com/agileboot/domain/shop/category/command/AddCategoryCommand.java @@ -0,0 +1,11 @@ +package com.agileboot.domain.shop.category.command; + +import com.agileboot.domain.shop.category.db.ShopCategoryEntity; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@EqualsAndHashCode(callSuper = true) +@Data +public class AddCategoryCommand extends ShopCategoryEntity { + +} diff --git a/agileboot-domain/src/main/java/com/agileboot/domain/shop/category/command/UpdateCategoryCommand.java b/agileboot-domain/src/main/java/com/agileboot/domain/shop/category/command/UpdateCategoryCommand.java new file mode 100644 index 0000000..1fec469 --- /dev/null +++ b/agileboot-domain/src/main/java/com/agileboot/domain/shop/category/command/UpdateCategoryCommand.java @@ -0,0 +1,10 @@ +package com.agileboot.domain.shop.category.command; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +@EqualsAndHashCode(callSuper = true) +@Data +public class UpdateCategoryCommand extends AddCategoryCommand { + +} diff --git a/agileboot-domain/src/main/java/com/agileboot/domain/shop/category/model/CategoryModel.java b/agileboot-domain/src/main/java/com/agileboot/domain/shop/category/model/CategoryModel.java new file mode 100644 index 0000000..cfac579 --- /dev/null +++ b/agileboot-domain/src/main/java/com/agileboot/domain/shop/category/model/CategoryModel.java @@ -0,0 +1,45 @@ +package com.agileboot.domain.shop.category.model; + +import cn.hutool.core.bean.BeanUtil; +import com.agileboot.domain.shop.category.command.AddCategoryCommand; +import com.agileboot.domain.shop.category.command.UpdateCategoryCommand; +import com.agileboot.domain.shop.category.db.ShopCategoryEntity; +import com.agileboot.domain.shop.category.db.ShopCategoryService; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@EqualsAndHashCode(callSuper = true) +@Data +public class CategoryModel extends ShopCategoryEntity { + + private ShopCategoryService categoryService; + + public CategoryModel(ShopCategoryEntity entity, ShopCategoryService categoryService) { + this(categoryService); + if (entity != null) { + BeanUtil.copyProperties(entity, this); + } + } + + public CategoryModel(ShopCategoryService categoryService) { + this.categoryService = categoryService; + } + + public void loadAddCommand(AddCategoryCommand command) { + if (command != null) { + BeanUtil.copyProperties(command, this, "categoryId"); + } + } + + public void loadUpdateCommand(UpdateCategoryCommand command) { + if (command != null) { + BeanUtil.copyProperties(command, this); + } + } + +/* public void checkCategoryNameUnique() { + if (categoryService.isCategoryNameDuplicated(getCategoryName(), getCategoryId())) { + throw new ApiException(ErrorCode.FAILED, "分类名称已存在"); + } + }*/ +} \ No newline at end of file diff --git a/agileboot-domain/src/main/java/com/agileboot/domain/shop/category/model/CategoryModelFactory.java b/agileboot-domain/src/main/java/com/agileboot/domain/shop/category/model/CategoryModelFactory.java new file mode 100644 index 0000000..1b169a7 --- /dev/null +++ b/agileboot-domain/src/main/java/com/agileboot/domain/shop/category/model/CategoryModelFactory.java @@ -0,0 +1,27 @@ +package com.agileboot.domain.shop.category.model; + +import com.agileboot.common.exception.ApiException; +import com.agileboot.common.exception.error.ErrorCode; +import com.agileboot.domain.shop.category.db.ShopCategoryEntity; +import com.agileboot.domain.shop.category.db.ShopCategoryService; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class CategoryModelFactory { + + private final ShopCategoryService categoryService; + + public CategoryModel loadById(Long categoryId) { + ShopCategoryEntity entity = categoryService.getById(categoryId); + if (entity == null) { + throw new ApiException(ErrorCode.Business.COMMON_OBJECT_NOT_FOUND, categoryId, "商品分类"); + } + return new CategoryModel(entity, categoryService); + } + + public CategoryModel create() { + return new CategoryModel(categoryService); + } +} \ No newline at end of file