123 lines
4.4 KiB
Java
123 lines
4.4 KiB
Java
package com.designhub.controller;
|
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
import com.designhub.common.Result;
|
|
import com.designhub.entity.Contest;
|
|
import com.designhub.entity.ContestParticipant;
|
|
import com.designhub.service.ContestService;
|
|
import com.designhub.util.JwtUtil;
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
import lombok.RequiredArgsConstructor;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import java.util.List;
|
|
|
|
/**
|
|
* 竞赛活动Controller
|
|
*/
|
|
@Tag(name = "竞赛活动", description = "竞赛活动管理相关API")
|
|
@RestController
|
|
@RequestMapping("/api/contests")
|
|
@RequiredArgsConstructor
|
|
public class ContestController {
|
|
|
|
private final ContestService contestService;
|
|
private final JwtUtil jwtUtil;
|
|
|
|
@Operation(summary = "创建竞赛")
|
|
@PostMapping
|
|
public Result<Contest> createContest(
|
|
@RequestBody Contest contest,
|
|
@RequestHeader("Authorization") String authHeader) {
|
|
String token = jwtUtil.extractTokenFromHeader(authHeader);
|
|
Long userId = jwtUtil.getUserIdFromToken(token);
|
|
contest.setCreatorId(userId);
|
|
return Result.success(contestService.createContest(contest));
|
|
}
|
|
|
|
@Operation(summary = "更新竞赛")
|
|
@PutMapping("/{id}")
|
|
public Result<Contest> updateContest(@PathVariable Long id, @RequestBody Contest contest) {
|
|
return Result.success(contestService.updateContest(id, contest));
|
|
}
|
|
|
|
@Operation(summary = "发布竞赛")
|
|
@PostMapping("/{id}/publish")
|
|
public Result<Contest> publishContest(@PathVariable Long id) {
|
|
return Result.success(contestService.publishContest(id));
|
|
}
|
|
|
|
@Operation(summary = "结束竞赛")
|
|
@PostMapping("/{id}/end")
|
|
public Result<Void> endContest(@PathVariable Long id) {
|
|
contestService.endContest(id);
|
|
return Result.success();
|
|
}
|
|
|
|
@Operation(summary = "取消竞赛")
|
|
@PostMapping("/{id}/cancel")
|
|
public Result<Void> cancelContest(@PathVariable Long id) {
|
|
contestService.cancelContest(id);
|
|
return Result.success();
|
|
}
|
|
|
|
@Operation(summary = "删除竞赛")
|
|
@DeleteMapping("/{id}")
|
|
public Result<Void> deleteContest(@PathVariable Long id) {
|
|
contestService.deleteContest(id);
|
|
return Result.success();
|
|
}
|
|
|
|
@Operation(summary = "获取竞赛列表")
|
|
@GetMapping
|
|
public Result<IPage<Contest>> getContestList(
|
|
@RequestParam(defaultValue = "1") Integer page,
|
|
@RequestParam(defaultValue = "10") Integer pageSize,
|
|
@RequestParam(required = false) String status) {
|
|
return Result.success(contestService.getContestList(page, pageSize, status));
|
|
}
|
|
|
|
@Operation(summary = "获取竞赛详情")
|
|
@GetMapping("/{id}")
|
|
public Result<Contest> getContestById(@PathVariable Long id) {
|
|
return Result.success(contestService.getContestById(id));
|
|
}
|
|
|
|
@Operation(summary = "获取竞赛排行榜")
|
|
@GetMapping("/{id}/leaderboard")
|
|
public Result<List<ContestParticipant>> getLeaderboard(
|
|
@PathVariable Long id,
|
|
@RequestParam(required = false, defaultValue = "50") Integer limit) {
|
|
return Result.success(contestService.getLeaderboard(id, limit));
|
|
}
|
|
|
|
@Operation(summary = "获取用户在竞赛中的数据")
|
|
@GetMapping("/{contestId}/user/{userId}")
|
|
public Result<ContestParticipant> getUserContestData(
|
|
@PathVariable Long contestId,
|
|
@PathVariable Long userId) {
|
|
return Result.success(contestService.getUserContestData(contestId, userId));
|
|
}
|
|
|
|
@Operation(summary = "获取我的竞赛数据")
|
|
@GetMapping("/{contestId}/my")
|
|
public Result<ContestParticipant> getMyContestData(
|
|
@PathVariable Long contestId,
|
|
@RequestHeader("Authorization") String authHeader) {
|
|
String token = jwtUtil.extractTokenFromHeader(authHeader);
|
|
Long userId = jwtUtil.getUserIdFromToken(token);
|
|
return Result.success(contestService.getUserContestData(contestId, userId));
|
|
}
|
|
|
|
@Operation(summary = "领取竞赛奖励")
|
|
@PostMapping("/{contestId}/claim-reward")
|
|
public Result<Boolean> claimReward(
|
|
@PathVariable Long contestId,
|
|
@RequestHeader("Authorization") String authHeader) {
|
|
String token = jwtUtil.extractTokenFromHeader(authHeader);
|
|
Long userId = jwtUtil.getUserIdFromToken(token);
|
|
return Result.success(contestService.claimReward(contestId, userId));
|
|
}
|
|
}
|