using Microsoft.AspNetCore.Mvc;
using server.Models;
using server.Persistence.Repositories;
namespace server.Controllers.v1
{
[ApiController]
[ApiVersion("1.0")]
[Route("/api/seasons")]
[Produces("application/json")]
public class SeasonsController : ControllerBase
{
private readonly ISeasonRepository _seasonRepository;
public SeasonsController(ISeasonRepository seasonRepository)
{
_seasonRepository = seasonRepository;
}
///
/// Gets a collection of seasons.
///
/// Returns the collection of seasons requested.
/// Failed to get seasons.
/// Returns a collection of seasons.
[MapToApiVersion("1.0")]
[HttpGet]
[ProducesResponseType(typeof(List), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)]
public async Task GetSeasonsAsync()
{
try
{
var seasons = await _seasonRepository.GetSeasonsAsync();
return Ok(seasons);
}
catch (Exception e)
{
Console.WriteLine(e);
return Problem(detail: "Failed to get seasons", statusCode: 500);
}
}
///
/// Gets a season by its number.
///
/// Returns the season requested.
/// Could not find a seaon with number provided.
/// Failed to get season.
/// Returns the season requested
[MapToApiVersion("1.0")]
[HttpGet("{number:int}")]
[ProducesResponseType(typeof(Season), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)]
public async Task GetSeasonByNumberAsync(int number)
{
try
{
var season = await _seasonRepository.GetSeasonByNumberAsync(number);
return season == null ?
Problem(detail: $"Could not find season {number}", statusCode: 404) :
Ok(season);
}
catch (Exception e)
{
Console.WriteLine(e);
return Problem(detail: "Failed to get season", statusCode: 500);
}
}
}
}