|
|
@@ -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.contest.*;
|
|
|
+import space.anyi.serve.service.ContestService;
|
|
|
+import space.anyi.serve.util.SecurityUtils;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+@Tag(name = "ContestController", description = "比赛相关接口")
|
|
|
+@RestController
|
|
|
+@RequestMapping("contest")
|
|
|
+public class ContestController {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private ContestService contestService;
|
|
|
+
|
|
|
+ @Operation(summary = "分页查询比赛列表")
|
|
|
+ @GetMapping
|
|
|
+ public Response<PageVo<List<ContestVo>>> queryByPage(
|
|
|
+ @RequestParam(defaultValue = "") String title,
|
|
|
+ @RequestParam(defaultValue = "") String status,
|
|
|
+ @NotNull(message = "页码不能为null") @RequestParam(defaultValue = "1") Integer pageNum,
|
|
|
+ @NotNull(message = "分页大小不能为null") @RequestParam(defaultValue = "10") Integer pageSize) {
|
|
|
+ boolean onlyPublic = SecurityUtils.isAnonymous();
|
|
|
+ Page<Contest> page = Page.of(pageNum, pageSize);
|
|
|
+ Page<ContestVo> result = contestService.queryByPage(title, status, onlyPublic, page);
|
|
|
+ return Response.ok(new PageVo<>(result.getTotal(), result.getRecords()));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Operation(summary = "查询比赛详情")
|
|
|
+ @GetMapping("{id}")
|
|
|
+ public Response<ContestVo> getDetail(@PathVariable Long id,
|
|
|
+ @RequestParam(required = false) String password) {
|
|
|
+ Long userId = SecurityUtils.getCurrentUserId();
|
|
|
+ ContestVo base = contestService.getBaseInfo(id);
|
|
|
+ if ("password".equals(base.getType())) {
|
|
|
+ if (!contestService.checkPassword(id, password)) {
|
|
|
+ return Response.ok(base);
|
|
|
+ }
|
|
|
+ } else if ("private".equals(base.getType())) {
|
|
|
+ String role = SecurityUtils.getCurrentRole();
|
|
|
+ boolean staff = "coach".equals(role) || "admin".equals(role);
|
|
|
+ if (!contestService.isRegistered(id, userId)
|
|
|
+ && !contestService.isCreator(id, userId) && !staff) {
|
|
|
+ return Response.error("无权限查看该比赛");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return Response.ok(contestService.getDetail(id, userId));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Operation(summary = "查询比赛排行榜")
|
|
|
+ @GetMapping("{id}/ranking")
|
|
|
+ public Response<List<RankVo>> getRanking(@PathVariable Long id) {
|
|
|
+ return Response.ok(contestService.getRanking(id));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Operation(summary = "新增比赛")
|
|
|
+ @PreAuthorize("hasAnyRole('ROLE_coach', 'ROLE_admin')")
|
|
|
+ @PostMapping
|
|
|
+ public Response<Boolean> add(@Valid @RequestBody ContestDto dto) {
|
|
|
+ return Response.ok(contestService.insert(dto, SecurityUtils.getCurrentUserId()));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Operation(summary = "修改比赛")
|
|
|
+ @PreAuthorize("hasAnyRole('ROLE_coach', 'ROLE_admin')")
|
|
|
+ @PutMapping
|
|
|
+ public Response<Boolean> edit(@Valid @RequestBody ContestDto dto) {
|
|
|
+ return Response.ok(contestService.update(dto));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Operation(summary = "批量删除比赛")
|
|
|
+ @PreAuthorize("hasRole('ROLE_admin')")
|
|
|
+ @DeleteMapping
|
|
|
+ public Response<Boolean> deleteByIds(@NotEmpty(message = "ID列表不能为空") @RequestParam List<Long> ids) {
|
|
|
+ return Response.ok(contestService.deleteByIds(ids));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Operation(summary = "比赛添加题目")
|
|
|
+ @PreAuthorize("hasAnyRole('ROLE_coach', 'ROLE_admin')")
|
|
|
+ @PostMapping("{id}/problem")
|
|
|
+ public Response<Boolean> addProblem(@PathVariable Long id, @Valid @RequestBody ContestProblemDto dto) {
|
|
|
+ return Response.ok(contestService.addProblem(id, dto));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Operation(summary = "修改比赛题目")
|
|
|
+ @PreAuthorize("hasAnyRole('ROLE_coach', 'ROLE_admin')")
|
|
|
+ @PutMapping("{id}/problem")
|
|
|
+ public Response<Boolean> updateProblem(@PathVariable Long id, @Valid @RequestBody ContestProblemDto dto) {
|
|
|
+ return Response.ok(contestService.updateProblem(id, dto));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Operation(summary = "比赛移除题目")
|
|
|
+ @PreAuthorize("hasAnyRole('ROLE_coach', 'ROLE_admin')")
|
|
|
+ @DeleteMapping("{id}/problem")
|
|
|
+ public Response<Boolean> removeProblem(@PathVariable Long id,
|
|
|
+ @NotNull(message = "题目ID不能为空") @RequestParam Long problemId) {
|
|
|
+ return Response.ok(contestService.removeProblem(id, problemId));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Operation(summary = "报名比赛")
|
|
|
+ @PostMapping("{id}/register")
|
|
|
+ public Response<Boolean> register(@PathVariable Long id, @Valid @RequestBody ContestRegisterDto dto) {
|
|
|
+ if (!contestService.checkPassword(id, dto.getPassword())) {
|
|
|
+ return Response.error("比赛密码错误");
|
|
|
+ }
|
|
|
+ return Response.ok(contestService.register(id, SecurityUtils.getCurrentUserId()));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Operation(summary = "查询比赛报名名单")
|
|
|
+ @PreAuthorize("hasAnyRole('ROLE_coach', 'ROLE_admin')")
|
|
|
+ @GetMapping("{id}/registrations")
|
|
|
+ public Response<List<ContestRegistrationVo>> queryRegistrations(@PathVariable Long id) {
|
|
|
+ return Response.ok(contestService.queryRegistrations(id));
|
|
|
+ }
|
|
|
+}
|