2022-06-24 09:58:35 -05:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2022-06-24 10:10:02 -05:00
|
|
|
using MongoDB.Bson;
|
|
|
|
|
using server.Models;
|
|
|
|
|
using server.Persistence.Repositories;
|
2022-06-24 09:58:35 -05:00
|
|
|
|
|
|
|
|
namespace server.Controllers.v1
|
|
|
|
|
{
|
|
|
|
|
[ApiController]
|
|
|
|
|
[ApiVersion("1.0")]
|
2022-06-24 11:35:33 -05:00
|
|
|
[Route("/api/characters")]
|
2022-06-24 09:58:35 -05:00
|
|
|
[Produces("application/json")]
|
|
|
|
|
public class CharactersController : ControllerBase
|
|
|
|
|
{
|
2022-06-24 10:10:02 -05:00
|
|
|
private readonly ICharacterRepository _characterRepository;
|
|
|
|
|
|
|
|
|
|
public CharactersController(ICharacterRepository characterRepository)
|
|
|
|
|
{
|
|
|
|
|
_characterRepository = characterRepository;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a collection of characters.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="filter">Filter parameters passed from query string.</param>
|
|
|
|
|
/// <response code="200">Returns the collection of characters requested.</response>
|
|
|
|
|
/// <response code="400">Not a valid request.</response>
|
|
|
|
|
/// <response code="500">Failed to get characters.</response>
|
|
|
|
|
/// <returns>Returns a collection of characters.</returns>
|
|
|
|
|
[MapToApiVersion("1.0")]
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[ProducesResponseType(typeof(List<Character>), StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
|
|
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)]
|
2022-06-29 12:35:13 -05:00
|
|
|
public async Task<IActionResult> GetCharactersAsync([FromQuery] CharacterFilter? filter)
|
2022-06-24 10:10:02 -05:00
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var character = await _characterRepository.GetCharactersAsync(filter);
|
|
|
|
|
return Ok(character);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(e);
|
|
|
|
|
return Problem(detail: "Failed to get characters", statusCode: 500);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a character by its id.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id">The id of the character being requested.</param>
|
|
|
|
|
/// <response code="200">Returns the character requested.</response>
|
|
|
|
|
/// <response code="400">Not a valid request.</response>
|
|
|
|
|
/// <response code="404">Character with the given id not found.</response>
|
|
|
|
|
/// <response code="500">Failed to get character.</response>
|
|
|
|
|
/// <returns>Returns a character.</returns>
|
|
|
|
|
[MapToApiVersion("1.0")]
|
|
|
|
|
[HttpGet("{id}")]
|
|
|
|
|
[ProducesResponseType(typeof(Character), StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
|
|
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
|
|
|
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)]
|
2022-06-29 12:35:13 -05:00
|
|
|
public async Task<IActionResult> GetCharacterByIdAsync(string id)
|
2022-06-24 10:10:02 -05:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if (!ObjectId.TryParse(id, out _))
|
|
|
|
|
{
|
|
|
|
|
ModelState.AddModelError(nameof(id), $"{id} is not a valid id");
|
2022-07-06 11:01:46 -05:00
|
|
|
return ValidationProblem(detail: "Invalid request");
|
2022-06-24 10:10:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var quote = await _characterRepository.GetCharacterByIdAsync(id);
|
|
|
|
|
|
|
|
|
|
return quote == null ?
|
|
|
|
|
Problem(detail: $"Could not find character {id}", statusCode: 404) :
|
|
|
|
|
Ok(quote);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(e);
|
|
|
|
|
return Problem(detail: "Failed to get character", statusCode: 500);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-24 09:58:35 -05:00
|
|
|
}
|
|
|
|
|
}
|