begin implementing quotes repository and controller

This commit is contained in:
StevanFreeborn
2022-06-22 11:28:44 -05:00
parent 8533869b31
commit 2cbab773d0
5 changed files with 109 additions and 0 deletions
+55
View File
@@ -0,0 +1,55 @@
using Microsoft.AspNetCore.Mvc;
using server.Models;
using server.Persistence.Repositories;
namespace server.Controllers.v1
{
[ApiController]
[ApiVersion("1.0")]
[Route("/api/quotes")]
[Produces("application/json")]
public class QuotesController : ControllerBase
{
private readonly IQuoteRepository _quoteRepository;
public QuotesController(IQuoteRepository quoteRepository)
{
_quoteRepository = quoteRepository;
}
/// <summary>
/// Gets a collection of quotes.
/// </summary>
/// <param name="filter">Filter parameters passed from query string.</param>
/// <response code="200">Returns the collection of quotes requested.</response>
/// <response code="400">Not a valid request.</response>
/// <response code="500">Failed to get quotes.</response>
/// <returns>Returns a collection of quotes.</returns>
[MapToApiVersion("1.0")]
[HttpGet]
[ProducesResponseType(typeof(List<Quote>), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<List<Quote>>> GetQuotesAsync([FromQuery] QuoteFilter? filter)
{
try
{
var quotes = await _quoteRepository.GetQuotesAsync(filter);
return Ok(quotes);
}
catch (Exception e)
{
Console.WriteLine(e);
return Problem(detail: "Failed to get quote", statusCode: 500);
}
}
// TODO
[MapToApiVersion("1.0")]
[HttpGet]
public async Task<ActionResult<Quote>> GetQuoteByIdAsync(int id)
{
throw new NotImplementedException();
}
}
}
+10
View File
@@ -0,0 +1,10 @@
namespace server.Models
{
public class QuoteFilter
{
/// <summary>
/// Use to filter quote by narrator.
/// </summary>
public string? Narrator { get; set; } = null;
}
}
@@ -0,0 +1,10 @@
using server.Models;
namespace server.Persistence.Repositories
{
public interface IQuoteRepository
{
Task<List<Quote>> GetQuotesAsync(QuoteFilter filter);
Task<Quote> GetQuoteByIdAsync(int id);
}
}
@@ -0,0 +1,32 @@
using server.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace server.Persistence.Repositories
{
public class QuoteRepository : IQuoteRepository
{
private readonly IDbContext _context;
public QuoteRepository(IDbContext context)
{
_context = context;
}
// TODO: Implement GetQuotesAsync() method.
public Task<List<Quote>> GetQuotesAsync(QuoteFilter filter)
{
throw new NotImplementedException();
}
// TODO: Implement GetQuoteByIdAsync() method.
public Task<Quote> GetQuoteByIdAsync(int id)
{
throw new NotImplementedException();
}
}
}
+2
View File
@@ -56,6 +56,8 @@ builder.Services.AddScoped<ISeasonRepository, SeasonRepository>();
builder.Services.AddScoped<IEpisodeRepository, EpisodeRepository>();
builder.Services.AddScoped<IQuoteRepository, QuoteRepository>();
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();