|
|
@@ -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));
|
|
|
+ }
|
|
|
+}
|