分类增删改接口
This commit is contained in:
parent
f44a6a5da5
commit
416f73aa61
|
@ -48,3 +48,4 @@ nbdist/
|
|||
|
||||
/agileboot-admin/src/main/resources/application-prod.yml
|
||||
|
||||
/.svn
|
||||
|
|
|
@ -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;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
|
@ -51,5 +63,34 @@ public class ShopCategoryController extends BaseController {
|
|||
List<ShopCategoryDTO> page = categoryApplicationService.getCategoryAll();
|
||||
return ResponseDTO.ok(page);
|
||||
}
|
||||
|
||||
@Operation(summary = "新增分类")
|
||||
// @PreAuthorize("@permission.has('shop:category:add')")
|
||||
@AccessLog(title = "分类管理", businessType = BusinessTypeEnum.ADD)
|
||||
@PostMapping
|
||||
public ResponseDTO<Void> 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<Void> remove(@PathVariable @NotNull List<Long> categoryIds) {
|
||||
BulkOperationCommand<Long> 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<Void> edit(@PathVariable Long categoryId, @Validated @RequestBody UpdateCategoryCommand command) {
|
||||
command.setCategoryId(categoryId);
|
||||
categoryApplicationService.updateCategory(command);
|
||||
return ResponseDTO.ok();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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<ShopCategoryDTO> getCategoryList(SearchShopCategoryQuery<ShopCategoryEntity> query) {
|
||||
Page<ShopCategoryEntity> 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<Long> command) {
|
||||
for (Long categoryId : command.getIds()) {
|
||||
CategoryModel model = categoryModelFactory.loadById(categoryId);
|
||||
model.deleteById();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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 {
|
||||
|
||||
}
|
|
@ -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 {
|
||||
|
||||
}
|
|
@ -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, "分类名称已存在");
|
||||
}
|
||||
}*/
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue