部门管理

This commit is contained in:
none
2023-02-19 12:15:57 +08:00
parent 166dc4c1a2
commit 9c97b774a0
15 changed files with 514 additions and 6 deletions

View File

@@ -6,6 +6,7 @@ import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import xyz.playedu.api.exception.NotFoundException;
import xyz.playedu.api.exception.ServiceException;
import xyz.playedu.api.types.JsonResponse;
@@ -14,10 +15,10 @@ import java.util.List;
@RestControllerAdvice
public class ExceptionController {
// @ExceptionHandler(Exception.class)
// public JsonResponse<String> exceptionHandler(Exception e) {
// return JsonResponse.error("系统错误", 500);
// }
@ExceptionHandler(Exception.class)
public JsonResponse exceptionHandler(Exception e) {
return JsonResponse.error("系统错误", 500);
}
@ExceptionHandler(ServiceException.class)
public JsonResponse serviceExceptionHandler(ServiceException e) {
@@ -45,4 +46,9 @@ public class ExceptionController {
return JsonResponse.error("请求method错误", 400);
}
@ExceptionHandler(NotFoundException.class)
public JsonResponse serviceExceptionHandler(NotFoundException e) {
return JsonResponse.error(e.getMessage(), 404);
}
}

View File

@@ -0,0 +1,84 @@
package xyz.playedu.api.controller.backend;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import xyz.playedu.api.bus.DepartmentBus;
import xyz.playedu.api.domain.Department;
import xyz.playedu.api.exception.NotFoundException;
import xyz.playedu.api.request.backend.DepartmentRequest;
import xyz.playedu.api.service.DepartmentService;
import xyz.playedu.api.types.JsonResponse;
import java.util.Date;
import java.util.List;
/**
* @Author 杭州白书科技有限公司
* @create 2023/2/19 10:33
*/
@RestController
@Slf4j
@RequestMapping("/backend/v1/department")
public class DepartmentController {
@Autowired
private DepartmentService departmentService;
@Autowired
private DepartmentBus departmentBus;
@GetMapping("/index")
public JsonResponse index() {
List<Department> data = departmentService.list();
return JsonResponse.data(data);
}
@GetMapping("/create")
public JsonResponse create(@RequestParam(name = "parent_id", defaultValue = "0") Integer parentId) {
List<Department> data = departmentService.listByParentId(parentId);
return JsonResponse.data(data);
}
@PostMapping("/create")
public JsonResponse store(@RequestBody DepartmentRequest request) throws NotFoundException {
String parentChain = "";
if (request.getParentId() != 0) {
parentChain = departmentBus.compParentChain(request.getParentId());
}
Department department = new Department();
department.setName(request.getName());
department.setParentId(request.getParentId());
department.setParentChain(parentChain);
department.setSort(request.getSort());
department.setCreatedAt(new Date());
department.setUpdatedAt(new Date());
departmentService.save(department);
return JsonResponse.success();
}
@GetMapping("/{id}")
public JsonResponse edit(@PathVariable Integer id) throws NotFoundException {
Department department = departmentService.findOrFail(id);
return JsonResponse.data(department);
}
@PutMapping("/{id}")
public JsonResponse update(@PathVariable Integer id, @RequestBody DepartmentRequest request) throws NotFoundException {
Department department = departmentService.findOrFail(id);
departmentService.update(department, request.getName(), request.getParentId(), request.getSort());
return JsonResponse.success();
}
@DeleteMapping("/{id}")
public JsonResponse destroy(@PathVariable Integer id) throws NotFoundException {
Department department = departmentService.findOrFail(id);
departmentService.deleteById(department.getId());
return JsonResponse.success();
}
}