150 lines
4.3 KiB
C#
150 lines
4.3 KiB
C#
|
|
using Microsoft.AspNetCore.Mvc;
|
||
|
|
using YD_TrainingCompetition_Api.Competition;
|
||
|
|
|
||
|
|
namespace YD_TrainingCompetition_Api.Controllers;
|
||
|
|
|
||
|
|
[ApiController]
|
||
|
|
[Route("api/[controller]")]
|
||
|
|
public sealed class CompetitionController : ControllerBase
|
||
|
|
{
|
||
|
|
private readonly ICompetitionSessionService _competition;
|
||
|
|
|
||
|
|
public CompetitionController(ICompetitionSessionService competition)
|
||
|
|
{
|
||
|
|
_competition = competition;
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpGet("session")]
|
||
|
|
public IActionResult GetSession() => Ok(_competition.GetState());
|
||
|
|
|
||
|
|
[HttpPost("session")]
|
||
|
|
public async Task<IActionResult> CreateSession([FromBody] CreateCompetitionSessionRequest request, CancellationToken cancellationToken)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
return Ok(await _competition.CreateAsync(request, cancellationToken));
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
return BadRequest(new { message = ex.Message });
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpPost("rounds/{roundIndex:int}/select")]
|
||
|
|
public async Task<IActionResult> SelectRound(int roundIndex, CancellationToken cancellationToken)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
return Ok(await _competition.SelectRoundAsync(roundIndex, cancellationToken));
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
return BadRequest(new { message = ex.Message });
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpPost("rounds/next")]
|
||
|
|
public async Task<IActionResult> NextRound(CancellationToken cancellationToken)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
return Ok(await _competition.SelectNextRoundAsync(cancellationToken));
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
return BadRequest(new { message = ex.Message });
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpPost("rounds/previous")]
|
||
|
|
public async Task<IActionResult> PreviousRound(CancellationToken cancellationToken)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
return Ok(await _competition.SelectPreviousRoundAsync(cancellationToken));
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
return BadRequest(new { message = ex.Message });
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpPost("rounds/current/download")]
|
||
|
|
public async Task<IActionResult> DownloadCurrentRound(CancellationToken cancellationToken)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
return Ok(await _competition.DownloadCurrentRoundAsync(cancellationToken));
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
return BadRequest(new { message = ex.Message });
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpPost("test/start")]
|
||
|
|
public async Task<IActionResult> Start(CancellationToken cancellationToken)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
return Ok(await _competition.StartAsync(cancellationToken));
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
return BadRequest(new { message = ex.Message });
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpPost("test/stop")]
|
||
|
|
public async Task<IActionResult> Stop(CancellationToken cancellationToken)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
return Ok(await _competition.StopAsync(cancellationToken));
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
return BadRequest(new { message = ex.Message });
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpPost("test/reset")]
|
||
|
|
public async Task<IActionResult> Reset(CancellationToken cancellationToken)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
return Ok(await _competition.ResetCurrentRoundAsync(cancellationToken));
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
return BadRequest(new { message = ex.Message });
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpPost("participants/{participantId:int}/count")]
|
||
|
|
public async Task<IActionResult> SetCount(int participantId, [FromBody] CompetitionParticipantCountRequest request, CancellationToken cancellationToken)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
return Ok(await _competition.SetParticipantCountAsync(participantId, request.Count, cancellationToken));
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
return BadRequest(new { message = ex.Message });
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpPost("participants/{participantId:int}/delta")]
|
||
|
|
public async Task<IActionResult> ChangeCount(int participantId, [FromBody] CompetitionParticipantDeltaRequest request, CancellationToken cancellationToken)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
return Ok(await _competition.ChangeParticipantCountAsync(participantId, request.Delta, cancellationToken));
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
return BadRequest(new { message = ex.Message });
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|