Files
criminalmindsapi/server/Controllers/v1/SeasonsController.cs
T

74 lines
2.7 KiB
C#
Raw Normal View History

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;
}
/// <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]
[ProducesResponseType(typeof(List<Season>), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)]
2022-06-29 12:35:13 -05:00
public async Task<IActionResult> GetSeasonsAsync()
2022-06-19 16:42:35 -05:00
{
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
}
}
/// <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}")]
[ProducesResponseType(typeof(Season), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)]
2022-06-29 12:35:13 -05:00
public async Task<IActionResult> GetSeasonByNumberAsync(int number)
2022-06-19 16:42:35 -05:00
{
try
{
var season = await _seasonRepository.GetSeasonByNumberAsync(number);
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);
return Problem(detail: "Failed to get season", statusCode: 500);
2022-06-19 16:42:35 -05:00
}
}
}
}