using System.Security.Claims; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using UoocProgress.Api.Models; using UoocProgress.Api.Services; namespace UoocProgress.Api.Controllers; [ApiController] [Authorize(Policy = "UserOrAdmin")] [Route("api/platform-challenges")] public sealed class PlatformChallengesController(ChallengeSessionService challengeSessionService) : ControllerBase { [HttpGet("{challengeSessionId}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] public ActionResult GetChallenge(string challengeSessionId) { var claimValue = User.FindFirstValue(ClaimTypes.NameIdentifier); var userId = long.TryParse(claimValue, out var parsed) ? parsed : throw new InvalidOperationException("当前登录态无效。"); var challenge = challengeSessionService.Get(challengeSessionId, userId); if (challenge is null) { return NotFound(new ProblemDetails { Title = "挑战会话不存在", Detail = "未找到对应的挑战会话。", Status = StatusCodes.Status404NotFound }); } return Ok(challenge); } }