37 lines
1.3 KiB
C#
37 lines
1.3 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using QuestionsApi.Services;
|
|
using System.Security.Claims;
|
|
|
|
namespace QuestionsApi.Controllers;
|
|
|
|
[ApiController]
|
|
[Route("api/daily-missions")]
|
|
[Authorize(Roles = "USER,SUPER")]
|
|
public sealed class DailyMissionsController(
|
|
DailyMissionStore missions, DashboardStore dashboard, QuestionStore questions) : ControllerBase
|
|
{
|
|
[HttpGet]
|
|
public async Task<IActionResult> Get()
|
|
{
|
|
var now = DateTime.UtcNow;
|
|
return Ok(await missions.GetOrCreateAsync(UserId(), await dashboard.GetAsync(UserId(), now),
|
|
await questions.GetPublishedAsync()));
|
|
}
|
|
|
|
[HttpPost("{missionId}/replace")]
|
|
public async Task<IActionResult> Replace(string missionId)
|
|
{
|
|
try
|
|
{
|
|
var result = await missions.ReplaceAsync(UserId(), missionId);
|
|
return result is null ? NotFound() : Ok(result);
|
|
}
|
|
catch (ArgumentException exception) { return NotFound(exception.Message); }
|
|
catch (InvalidOperationException exception) { return Conflict(exception.Message); }
|
|
}
|
|
|
|
private string UserId() => User.FindFirstValue(ClaimTypes.NameIdentifier)
|
|
?? User.FindFirstValue(ClaimTypes.Name) ?? User.Identity?.Name ?? throw new UnauthorizedAccessException();
|
|
}
|