2022-06-19 16:42:35 -05:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using server.Models;
|
|
|
|
|
using server.Persistence.Repositories;
|
|
|
|
|
|
2022-06-20 23:09:47 -05:00
|
|
|
namespace server.Controllers.v1
|
2022-06-19 16:42:35 -05:00
|
|
|
{
|
|
|
|
|
[ApiController]
|
2022-06-20 23:09:47 -05:00
|
|
|
[ApiVersion("1.0")]
|
2022-06-19 23:07:59 -05:00
|
|
|
[Route("/api/seasons")]
|
2022-06-19 16:42:35 -05:00
|
|
|
[Produces("application/json")]
|
|
|
|
|
public class SeasonsController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private readonly ISeasonRepository _seasonRepository;
|
|
|
|
|
|
|
|
|
|
public SeasonsController(ISeasonRepository seasonRepository)
|
|
|
|
|
{
|
|
|
|
|
_seasonRepository = seasonRepository;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-21 23:24:56 -05:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a collection of seasons.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <response code="200">Returns the collection of seasons requested.</response>
|
|
|
|
|
/// <response code="500">Failed to get seasons.</response>
|
|
|
|
|
/// <returns>Returns a collection of seasons.</returns>
|
2022-06-20 23:09:47 -05:00
|
|
|
[MapToApiVersion("1.0")]
|
2022-06-19 16:42:35 -05:00
|
|
|
[HttpGet]
|
2022-06-21 17:17:32 -05:00
|
|
|
[ProducesResponseType(typeof(List<Season>), StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)]
|
2022-06-19 16:42:35 -05:00
|
|
|
public async Task<ActionResult<List<Season>>> GetSeasonsAsync()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var seasons = await _seasonRepository.GetSeasonsAsync();
|
|
|
|
|
return Ok(seasons);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(e);
|
2022-06-23 16:43:17 -05:00
|
|
|
return Problem(detail: "Failed to get seasons", statusCode: 500);
|
2022-06-19 16:42:35 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-21 23:24:56 -05:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a season by its number.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <response code="200">Returns the season requested.</response>
|
|
|
|
|
/// <response code="404">Could not find a seaon with number provided.</response>
|
|
|
|
|
/// <response code="500">Failed to get season.</response>
|
|
|
|
|
/// <returns>Returns the season requested</returns>
|
2022-06-20 23:09:47 -05:00
|
|
|
[MapToApiVersion("1.0")]
|
2022-06-19 16:42:35 -05:00
|
|
|
[HttpGet("{number:int}")]
|
2022-06-21 17:17:32 -05:00
|
|
|
[ProducesResponseType(typeof(Season), StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
|
|
|
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)]
|
2022-06-19 16:42:35 -05:00
|
|
|
public async Task<ActionResult<Season>> GetSeasonByNumberAsync(int number)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var season = await _seasonRepository.GetSeasonByNumberAsync(number);
|
|
|
|
|
|
2022-06-21 23:24:56 -05:00
|
|
|
return season == null ?
|
|
|
|
|
Problem(detail: $"Could not find season {number}", statusCode: 404) :
|
|
|
|
|
Ok(season);
|
2022-06-19 16:42:35 -05:00
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(e);
|
2022-06-21 11:50:54 -05:00
|
|
|
return Problem(detail: "Failed to get season", statusCode: 500);
|
2022-06-19 16:42:35 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|