Browse Source

bug修复

master
luoweijian 2 months ago
parent
commit
3d79a4aaaf
  1. 2
      src/main/java/com/project/classicpaper/application/ClassicCategoryApplicationService.java
  2. 44
      src/main/java/com/project/classicpaper/application/impl/ClassicCategoryApplicationServiceImpl.java
  3. 5
      src/main/java/com/project/classicpaper/controller/ClassicCategoryController.java

2
src/main/java/com/project/classicpaper/application/ClassicCategoryApplicationService.java

@ -11,5 +11,7 @@ public interface ClassicCategoryApplicationService {
Result<ClassicCategoryDTO> save(ClassicCategoryDTO dto) throws Exception; Result<ClassicCategoryDTO> save(ClassicCategoryDTO dto) throws Exception;
Result<ClassicCategoryDTO> rename(Long id, String name) throws Exception;
Result<String> delete(Long id) throws Exception; Result<String> delete(Long id) throws Exception;
} }

44
src/main/java/com/project/classicpaper/application/impl/ClassicCategoryApplicationServiceImpl.java

@ -49,7 +49,9 @@ public class ClassicCategoryApplicationServiceImpl implements ClassicCategoryApp
Map<Long, List<ClassicPaperSetDTO>> paperSetMap = Boolean.TRUE.equals(withPaperSet) ? new HashMap<>() : null; Map<Long, List<ClassicPaperSetDTO>> paperSetMap = Boolean.TRUE.equals(withPaperSet) ? new HashMap<>() : null;
for (ClassicPaperSetEntity set : allPaperSets) { for (ClassicPaperSetEntity set : allPaperSets) {
Long catId = set.getClassicCategoryId(); Long catId = set.getClassicCategoryId();
if (catId == null) continue; if (catId == null) {
continue;
}
countMap.merge(catId, 1, Integer::sum); countMap.merge(catId, 1, Integer::sum);
if (paperSetMap != null) { if (paperSetMap != null) {
//按分类ID分组套题组 //按分类ID分组套题组
@ -163,6 +165,46 @@ public class ClassicCategoryApplicationServiceImpl implements ClassicCategoryApp
return Result.success(entity.toDTO(ClassicCategoryDTO::new)); return Result.success(entity.toDTO(ClassicCategoryDTO::new));
} }
@Override
public Result<ClassicCategoryDTO> rename(Long id, String name) throws Exception {
// 1. 参数校验
if (id == null) {
throw new BusinessErrorException("分类ID不能为空");
}
if (StrUtil.isBlank(name)) {
throw new BusinessErrorException("分类名称不能为空");
}
if (StrUtil.length(name) > 50) {
throw new BusinessErrorException("分类名称过长");
}
// 2. 分类存在性校验
ClassicCategoryEntity entity = classicCategoryBaseService.getById(id);
if (entity == null) {
throw new BusinessErrorException("分类不存在");
}
// 3. 名称未变更则直接返回
if (StrUtil.equals(entity.getName(), name)) {
return Result.success(entity.toDTO(ClassicCategoryDTO::new));
}
// 4. 同父级下重名校验
LambdaQueryWrapper<ClassicCategoryEntity> duplicateQuery = new LambdaQueryWrapper<ClassicCategoryEntity>()
.eq(ClassicCategoryEntity::getName, name)
.eq(ClassicCategoryEntity::getParentId, entity.getParentId())
.ne(ClassicCategoryEntity::getId, id);
if (classicCategoryBaseService.count(duplicateQuery) > 0) {
throw new BusinessErrorException("同级下已存在同名分类");
}
// 5. 只更新 name 字段,避免覆盖其他字段
entity.setName(name);
classicCategoryBaseService.updateById(entity);
return Result.success(entity.toDTO(ClassicCategoryDTO::new));
}
@Override @Override
public Result<String> delete(Long id) throws Exception { public Result<String> delete(Long id) throws Exception {
ClassicCategoryEntity category = classicCategoryBaseService.getById(id); ClassicCategoryEntity category = classicCategoryBaseService.getById(id);

5
src/main/java/com/project/classicpaper/controller/ClassicCategoryController.java

@ -31,6 +31,11 @@ public class ClassicCategoryController {
return classicCategoryApplicationService.save(dto); return classicCategoryApplicationService.save(dto);
} }
@PostMapping("/rename")
public Result<ClassicCategoryDTO> rename(Long id, String name) throws Exception {
return classicCategoryApplicationService.rename(id, name);
}
@PostMapping("/delete") @PostMapping("/delete")
public Result<String> delete(Long id) throws Exception { public Result<String> delete(Long id) throws Exception {
return classicCategoryApplicationService.delete(id); return classicCategoryApplicationService.delete(id);

Loading…
Cancel
Save