Browse Source

# feat: 题目模块接口实现(Controller)

yangyi 2 tháng trước cách đây
mục cha
commit
fd21730e53

+ 51 - 0
src/main/java/space/anyi/serve/controller/CategoryController.java

@@ -0,0 +1,51 @@
+package space.anyi.serve.controller;
+
+import io.swagger.v3.oas.annotations.Operation;
+import io.swagger.v3.oas.annotations.tags.Tag;
+import jakarta.annotation.Resource;
+import jakarta.validation.Valid;
+import jakarta.validation.constraints.NotNull;
+import org.springframework.security.access.prepost.PreAuthorize;
+import org.springframework.web.bind.annotation.*;
+import space.anyi.serve.entity.Response;
+import space.anyi.serve.entity.problem.CategoryDto;
+import space.anyi.serve.entity.problem.CategoryVo;
+import space.anyi.serve.service.CategoryService;
+
+import java.util.List;
+
+@Tag(name = "CategoryController", description = "题目分类相关接口")
+@RestController
+@RequestMapping("category")
+public class CategoryController {
+
+    @Resource
+    private CategoryService categoryService;
+
+    @Operation(summary = "查询分类列表")
+    @GetMapping
+    public Response<List<CategoryVo>> list() {
+        return Response.ok(categoryService.listAll());
+    }
+
+    @Operation(summary = "新增分类")
+    @PreAuthorize("hasAnyRole('ROLE_coach', 'ROLE_admin')")
+    @PostMapping
+    public Response<Boolean> add(@Valid @RequestBody CategoryDto dto) {
+        return Response.ok(categoryService.insert(dto));
+    }
+
+    @Operation(summary = "修改分类")
+    @PreAuthorize("hasAnyRole('ROLE_coach', 'ROLE_admin')")
+    @PutMapping
+    public Response<Boolean> edit(@Valid @RequestBody CategoryDto dto) {
+        return Response.ok(categoryService.update(dto));
+    }
+
+    @Operation(summary = "删除分类")
+    @PreAuthorize("hasRole('ROLE_admin')")
+    @DeleteMapping
+    public Response<Boolean> delete(@NotNull(message = "分类ID不能为空") @RequestParam Long id) {
+        return Response.ok(categoryService.deleteById(id));
+    }
+}

+ 126 - 0
src/main/java/space/anyi/serve/controller/ProblemController.java

@@ -0,0 +1,126 @@
+package space.anyi.serve.controller;
+
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import io.swagger.v3.oas.annotations.Operation;
+import io.swagger.v3.oas.annotations.tags.Tag;
+import jakarta.annotation.Resource;
+import jakarta.validation.Valid;
+import jakarta.validation.constraints.NotEmpty;
+import jakarta.validation.constraints.NotNull;
+import org.springframework.security.access.prepost.PreAuthorize;
+import org.springframework.web.bind.annotation.*;
+import space.anyi.serve.entity.PageVo;
+import space.anyi.serve.entity.Response;
+import space.anyi.serve.entity.problem.*;
+import space.anyi.serve.service.ProblemService;
+import space.anyi.serve.util.SecurityUtils;
+
+import java.util.List;
+
+@Tag(name = "ProblemController", description = "题目相关接口")
+@RestController
+@RequestMapping("problem")
+public class ProblemController {
+
+    @Resource
+    private ProblemService problemService;
+
+    @Operation(summary = "分页查询题目列表")
+    @GetMapping
+    public Response<PageVo<List<ProblemVo>>> queryByPage(
+            @RequestParam(defaultValue = "") String title,
+            @RequestParam(defaultValue = "") String difficulty,
+            @RequestParam(required = false) Long categoryId,
+            @RequestParam(required = false) Integer isPublic,
+            @NotNull(message = "页码不能为null") @RequestParam(defaultValue = "1") Integer pageNum,
+            @NotNull(message = "分页大小不能为null") @RequestParam(defaultValue = "10") Integer pageSize) {
+        boolean onlyPublic = SecurityUtils.isAnonymous();
+        Page<Problem> page = Page.of(pageNum, pageSize);
+        Page<ProblemVo> result = problemService.queryByPage(title, difficulty, categoryId, isPublic, onlyPublic, page);
+        return Response.ok(new PageVo<>(result.getTotal(), result.getRecords()));
+    }
+
+    @Operation(summary = "查询题目详情")
+    @GetMapping("{id}")
+    public Response<ProblemDetailVo> getDetail(@PathVariable Long id) {
+        ProblemDetailVo detail = problemService.getDetail(id);
+        String role = SecurityUtils.getCurrentRole();
+        boolean staff = "coach".equals(role) || "admin".equals(role);
+        if (detail.getIsPublic() != null && detail.getIsPublic() == 0 && !staff) {
+            return Response.error("无权限查看该题目");
+        }
+        return Response.ok(detail);
+    }
+
+    @Operation(summary = "新增题目")
+    @PreAuthorize("hasAnyRole('ROLE_coach', 'ROLE_admin')")
+    @PostMapping
+    public Response<Boolean> add(@Valid @RequestBody ProblemDto dto) {
+        return Response.ok(problemService.insert(dto, SecurityUtils.getCurrentUserId()));
+    }
+
+    @Operation(summary = "修改题目")
+    @PreAuthorize("hasAnyRole('ROLE_coach', 'ROLE_admin')")
+    @PutMapping
+    public Response<Boolean> edit(@Valid @RequestBody ProblemDto dto) {
+        return Response.ok(problemService.update(dto));
+    }
+
+    @Operation(summary = "批量删除题目")
+    @PreAuthorize("hasRole('ROLE_admin')")
+    @DeleteMapping
+    public Response<Boolean> deleteByIds(@NotEmpty(message = "ID列表不能为空") @RequestParam List<Long> ids) {
+        return Response.ok(problemService.deleteByIds(ids));
+    }
+
+    @Operation(summary = "新增题目样例")
+    @PreAuthorize("hasAnyRole('ROLE_coach', 'ROLE_admin')")
+    @PostMapping("{id}/case")
+    public Response<Boolean> addCase(@PathVariable Long id, @Valid @RequestBody ProblemCaseDto dto) {
+        dto.setProblemId(id);
+        return Response.ok(problemService.addCase(dto));
+    }
+
+    @Operation(summary = "修改题目样例")
+    @PreAuthorize("hasAnyRole('ROLE_coach', 'ROLE_admin')")
+    @PutMapping("case")
+    public Response<Boolean> updateCase(@Valid @RequestBody ProblemCaseDto dto) {
+        return Response.ok(problemService.updateCase(dto));
+    }
+
+    @Operation(summary = "删除题目样例")
+    @PreAuthorize("hasAnyRole('ROLE_coach', 'ROLE_admin')")
+    @DeleteMapping("case")
+    public Response<Boolean> deleteCase(@NotNull(message = "样例ID不能为空") @RequestParam Long id) {
+        return Response.ok(problemService.deleteCase(id));
+    }
+
+    @Operation(summary = "新增题目评测用例")
+    @PreAuthorize("hasAnyRole('ROLE_coach', 'ROLE_admin')")
+    @PostMapping("{id}/test")
+    public Response<Boolean> addTest(@PathVariable Long id, @Valid @RequestBody ProblemTestDto dto) {
+        dto.setProblemId(id);
+        return Response.ok(problemService.addTest(dto));
+    }
+
+    @Operation(summary = "修改题目评测用例")
+    @PreAuthorize("hasAnyRole('ROLE_coach', 'ROLE_admin')")
+    @PutMapping("test")
+    public Response<Boolean> updateTest(@Valid @RequestBody ProblemTestDto dto) {
+        return Response.ok(problemService.updateTest(dto));
+    }
+
+    @Operation(summary = "删除题目评测用例")
+    @PreAuthorize("hasAnyRole('ROLE_coach', 'ROLE_admin')")
+    @DeleteMapping("test")
+    public Response<Boolean> deleteTest(@NotNull(message = "评测用例ID不能为空") @RequestParam Long id) {
+        return Response.ok(problemService.deleteTest(id));
+    }
+
+    @Operation(summary = "查询题目评测用例列表")
+    @PreAuthorize("hasAnyRole('ROLE_coach', 'ROLE_admin')")
+    @GetMapping("{id}/test")
+    public Response<List<ProblemTestVo>> queryTests(@PathVariable Long id) {
+        return Response.ok(problemService.queryTests(id));
+    }
+}