switched to use the problemdetails class when responding instead of custom errorresponse class
This commit is contained in:
@@ -20,9 +20,10 @@ namespace server.Controllers.v1
|
|||||||
[MapToApiVersion("1.0")]
|
[MapToApiVersion("1.0")]
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
[ProducesResponseType(typeof(List<Episode>), 200)]
|
[ProducesResponseType(typeof(List<Episode>), 200)]
|
||||||
[ProducesResponseType(500)]
|
[ProducesResponseType(typeof(ValidationProblemDetails), 400)]
|
||||||
|
[ProducesResponseType(typeof(ProblemDetails), 500)]
|
||||||
public async Task<ActionResult<List<Episode>>> GetEpisodesAsync([FromQuery] EpisodeFilter? filter)
|
public async Task<ActionResult<List<Episode>>> GetEpisodesAsync([FromQuery] EpisodeFilter? filter)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var seasons = await _episodeRepository.GetEpisodesAsync(filter);
|
var seasons = await _episodeRepository.GetEpisodesAsync(filter);
|
||||||
@@ -31,29 +32,30 @@ namespace server.Controllers.v1
|
|||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Console.WriteLine(e);
|
Console.WriteLine(e);
|
||||||
return StatusCode(StatusCodes.Status500InternalServerError, "Failed to get episodes");
|
return Problem(detail: "Failed to get episode", statusCode: 500);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[MapToApiVersion("1.0")]
|
[MapToApiVersion("1.0")]
|
||||||
[HttpGet("{number:int}")]
|
[HttpGet("{number:int}")]
|
||||||
[ProducesResponseType(typeof(Episode), 200)]
|
[ProducesResponseType(typeof(Episode), 200)]
|
||||||
[ProducesResponseType(500)]
|
[ProducesResponseType(typeof(ValidationProblemDetails), 400)]
|
||||||
|
[ProducesResponseType(typeof(ProblemDetails), 404)]
|
||||||
|
[ProducesResponseType(typeof(ProblemDetails), 500)]
|
||||||
public async Task<ActionResult<Episode>> GetEpisodeByNumberAsync(int number)
|
public async Task<ActionResult<Episode>> GetEpisodeByNumberAsync(int number)
|
||||||
{
|
{
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var episode = await _episodeRepository.GetEpisodeByNumberAsync(number);
|
var episode = await _episodeRepository.GetEpisodeByNumberAsync(number);
|
||||||
|
|
||||||
if (episode == null) return NotFound(new ErrorResponse($"Could not find episode number {number}"));
|
if (episode == null) return Problem(detail: $"Could not find episode {number}", statusCode: 404);
|
||||||
|
|
||||||
return Ok(episode);
|
return Ok(episode);
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Console.WriteLine(e);
|
Console.WriteLine(e);
|
||||||
return StatusCode(StatusCodes.Status500InternalServerError, "Failed to get season");
|
return Problem(detail: "Failed to get episode", statusCode: 500);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ namespace server.Controllers.v1
|
|||||||
[MapToApiVersion("1.0")]
|
[MapToApiVersion("1.0")]
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
[ProducesResponseType(typeof(List<Season>), 200)]
|
[ProducesResponseType(typeof(List<Season>), 200)]
|
||||||
[ProducesResponseType(500)]
|
[ProducesResponseType(typeof(ProblemDetails), 500)]
|
||||||
public async Task<ActionResult<List<Season>>> GetSeasonsAsync()
|
public async Task<ActionResult<List<Season>>> GetSeasonsAsync()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -31,29 +31,30 @@ namespace server.Controllers.v1
|
|||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Console.WriteLine(e);
|
Console.WriteLine(e);
|
||||||
return StatusCode(StatusCodes.Status500InternalServerError, "Failed to get seasons");
|
return Problem(detail: "Failed to get season", statusCode: 500);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[MapToApiVersion("1.0")]
|
[MapToApiVersion("1.0")]
|
||||||
[HttpGet("{number:int}")]
|
[HttpGet("{number:int}")]
|
||||||
[ProducesResponseType(typeof(Season), 200)]
|
[ProducesResponseType(typeof(Season), 200)]
|
||||||
[ProducesResponseType(typeof(ErrorResponse), 404)]
|
[ProducesResponseType(typeof(ValidationProblemDetails), 400)]
|
||||||
[ProducesResponseType(500)]
|
[ProducesResponseType(typeof(ProblemDetails), 404)]
|
||||||
|
[ProducesResponseType(typeof(ProblemDetails),500)]
|
||||||
public async Task<ActionResult<Season>> GetSeasonByNumberAsync(int number)
|
public async Task<ActionResult<Season>> GetSeasonByNumberAsync(int number)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var season = await _seasonRepository.GetSeasonByNumberAsync(number);
|
var season = await _seasonRepository.GetSeasonByNumberAsync(number);
|
||||||
|
|
||||||
if (season == null) return NotFound(new ErrorResponse($"Could not find season number {number}"));
|
if (season == null) return Problem(detail: $"Could not find season {number}", statusCode: 404);
|
||||||
|
|
||||||
return Ok(season);
|
return Ok(season);
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Console.WriteLine(e);
|
Console.WriteLine(e);
|
||||||
return StatusCode(StatusCodes.Status500InternalServerError, "Failed to get season");
|
return Problem(detail: "Failed to get season", statusCode: 500);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,5 +9,7 @@ namespace server.Models
|
|||||||
public class EpisodeFilter
|
public class EpisodeFilter
|
||||||
{
|
{
|
||||||
public int? Season { get; set; } = null;
|
public int? Season { get; set; } = null;
|
||||||
|
public DateTime? StartDate { get; set; } = null;
|
||||||
|
public DateTime? EndDate { get; set; } = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +0,0 @@
|
|||||||
namespace server.Models
|
|
||||||
{
|
|
||||||
public class ErrorResponse
|
|
||||||
{
|
|
||||||
public string Error { get; set; }
|
|
||||||
|
|
||||||
public ErrorResponse(string error)
|
|
||||||
{
|
|
||||||
Error = error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -24,6 +24,16 @@ namespace server.Persistence.Repositories
|
|||||||
query = query.Where(episode => episode.Season == filter.Season);
|
query = query.Where(episode => episode.Season == filter.Season);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (filter?.StartDate != null)
|
||||||
|
{
|
||||||
|
query = query.Where(episode => episode.AirDate >= filter.StartDate);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (filter?.EndDate != null)
|
||||||
|
{
|
||||||
|
query = query.Where(episode => episode.AirDate <= filter.EndDate);
|
||||||
|
}
|
||||||
|
|
||||||
return await query.ToListAsync();
|
return await query.ToListAsync();
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
|
|||||||
Reference in New Issue
Block a user