课程分类代码优化

This commit is contained in:
none 2023-02-25 10:35:24 +08:00
parent 75fa2abb32
commit 138e1f5b98
4 changed files with 34 additions and 24 deletions

View File

@ -34,9 +34,6 @@ public class CourseCategoryController {
@Autowired
private ApplicationContext ctx;
@Autowired
private CourseCategoryBus courseCategoryBus;
@GetMapping("/index")
public JsonResponse index() {
Map<Integer, List<CourseCategory>> categories = categoryService.all().stream().collect(Collectors.groupingBy(CourseCategory::getParentId));
@ -56,22 +53,8 @@ public class CourseCategoryController {
@BackendPermissionMiddleware(slug = BPermissionConstant.COURSE_CATEGORY)
@PostMapping("/create")
public JsonResponse store(@RequestBody @Validated CourseCategoryRequest request) throws NotFoundException {
String parentChain = "";
if (request.getParentId() != 0) {
parentChain = courseCategoryBus.compParentChain(request.getParentId());
}
CourseCategory category = new CourseCategory();
category.setName(request.getName());
category.setParentId(request.getParentId());
category.setParentChain(parentChain);
category.setSort(request.getSort());
category.setCreatedAt(new Date());
category.setUpdatedAt(new Date());
categoryService.save(category);
public JsonResponse store(@RequestBody @Validated CourseCategoryRequest req) throws NotFoundException {
categoryService.create(req.getName(), req.getParentId(), req.getSort());
return JsonResponse.success();
}
@ -84,9 +67,9 @@ public class CourseCategoryController {
@BackendPermissionMiddleware(slug = BPermissionConstant.COURSE_CATEGORY)
@PutMapping("/{id}")
public JsonResponse update(@PathVariable Integer id, @RequestBody CourseCategoryRequest request) throws NotFoundException {
public JsonResponse update(@PathVariable Integer id, @RequestBody CourseCategoryRequest req) throws NotFoundException {
CourseCategory category = categoryService.findOrFail(id);
categoryService.update(category, request.getName(), request.getParentId(), request.getSort());
categoryService.update(category, req.getName(), req.getParentId(), req.getSort());
return JsonResponse.success();
}

View File

@ -1,6 +1,7 @@
package xyz.playedu.api.request.backend;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
@ -17,15 +18,16 @@ public class CourseCategoryRequest implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
@NotNull(message = "请输入分类名")
@NotNull(message = "name参数不存在")
@NotBlank(message = "请输入分类名")
@Length(min = 1, max = 20, message = "分类名长度在1-20个字符之间")
private String name;
@JsonProperty("parent_id")
@NotNull(message = "请选择上级分类")
@NotNull(message = "parent_id参数不存在")
private Integer parentId;
@NotNull(message = "请输入排序值")
@NotNull(message = "sort参数不存在")
private Integer sort;
}

View File

@ -23,4 +23,6 @@ public interface CourseCategoryService extends IService<CourseCategory> {
void update(CourseCategory category, String name, Integer parentId, Integer sort) throws NotFoundException;
void create(String name, Integer parentId, Integer sort) throws NotFoundException;
}

View File

@ -1,6 +1,7 @@
package xyz.playedu.api.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import xyz.playedu.api.bus.CourseCategoryBus;
import xyz.playedu.api.domain.CourseCategory;
@ -10,6 +11,7 @@ import xyz.playedu.api.mapper.CourseCategoryMapper;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
@ -20,6 +22,9 @@ import java.util.List;
@Service
public class CourseCategoryServiceImpl extends ServiceImpl<CourseCategoryMapper, CourseCategory> implements CourseCategoryService {
@Autowired
private CourseCategoryBus categoryBus;
@Override
public List<CourseCategory> listByParentId(Integer id) {
return list(query().getWrapper().eq("parent_id", id).orderByAsc("sort"));
@ -112,6 +117,24 @@ public class CourseCategoryServiceImpl extends ServiceImpl<CourseCategoryMapper,
updateBatchById(updateRows);
}
@Override
public void create(String name, Integer parentId, Integer sort) throws NotFoundException {
String parentChain = "";
if (parentId != 0) {
parentChain = categoryBus.compParentChain(parentId);
}
CourseCategory category = new CourseCategory();
category.setName(name);
category.setParentId(parentId);
category.setParentChain(parentChain);
category.setSort(sort);
category.setCreatedAt(new Date());
category.setUpdatedAt(new Date());
save(category);
}
}