implement getquotebyidasync
This commit is contained in:
@@ -40,7 +40,7 @@ namespace server.Controllers.v1
|
|||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Console.WriteLine(e);
|
Console.WriteLine(e);
|
||||||
return Problem(detail: "Failed to get episode", statusCode: 500);
|
return Problem(detail: "Failed to get episodes", statusCode: 500);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||||
|
using MongoDB.Bson;
|
||||||
using server.Models;
|
using server.Models;
|
||||||
using server.Persistence.Repositories;
|
using server.Persistence.Repositories;
|
||||||
|
|
||||||
@@ -40,16 +42,39 @@ namespace server.Controllers.v1
|
|||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Console.WriteLine(e);
|
Console.WriteLine(e);
|
||||||
return Problem(detail: "Failed to get quote", statusCode: 500);
|
return Problem(detail: "Failed to get quotes", statusCode: 500);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Implement getquotesbyid
|
// TODO: Add comments
|
||||||
[MapToApiVersion("1.0")]
|
[MapToApiVersion("1.0")]
|
||||||
[HttpGet("{id:int}")]
|
[HttpGet("{id}")]
|
||||||
public Task<ActionResult<Quote>> GetQuoteByIdAsync(int id)
|
[ProducesResponseType(typeof(Quote), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
|
||||||
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)]
|
||||||
|
public async Task<ActionResult<Quote>> GetQuoteByIdAsync(string id)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
|
||||||
|
if (!ObjectId.TryParse(id, out _))
|
||||||
|
{
|
||||||
|
ModelState.AddModelError(nameof(id), $"{id} is not a valid id");
|
||||||
|
return ValidationProblem();
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var quote = await _quoteRepository.GetQuoteByIdAsync(id);
|
||||||
|
|
||||||
|
return quote == null ?
|
||||||
|
Problem(detail: $"Could not find quote {id}", statusCode: 404) :
|
||||||
|
Ok(quote);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Console.WriteLine(e);
|
||||||
|
return Problem(detail: "Failed to get quote", statusCode: 500);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ namespace server.Controllers.v1
|
|||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Console.WriteLine(e);
|
Console.WriteLine(e);
|
||||||
return Problem(detail: "Failed to get season", statusCode: 500);
|
return Problem(detail: "Failed to get seasons", statusCode: 500);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,9 +2,31 @@
|
|||||||
{
|
{
|
||||||
public class QuoteFilter
|
public class QuoteFilter
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Use to filter quotes by season.
|
||||||
|
/// </summary>
|
||||||
|
public int? Season { get; set; } = null;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Use to filter quotes by episode.
|
||||||
|
/// </summary>
|
||||||
|
public int? Episode { get; set; } = null;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Use to filter quotes by a key word.
|
||||||
|
/// </summary>
|
||||||
|
public string? TextKeyword { get; set; } = null;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Use to filter quotes by source.
|
||||||
|
/// </summary>
|
||||||
|
public string? Source { get; set; } = null;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Use to filter quote by narrator.
|
/// Use to filter quote by narrator.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string? Narrator { get; set; } = null;
|
public string? Narrator { get; set; } = null;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,6 @@ namespace server.Persistence.Repositories
|
|||||||
public interface IQuoteRepository
|
public interface IQuoteRepository
|
||||||
{
|
{
|
||||||
Task<List<Quote>> GetQuotesAsync(QuoteFilter filter);
|
Task<List<Quote>> GetQuotesAsync(QuoteFilter filter);
|
||||||
Task<Quote> GetQuoteByIdAsync(int id);
|
Task<Quote> GetQuoteByIdAsync(string id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,13 +13,32 @@ namespace server.Persistence.Repositories
|
|||||||
_context = context;
|
_context = context;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Implement optional, but possible filters for query.
|
|
||||||
public async Task<List<Quote>> GetQuotesAsync(QuoteFilter filter)
|
public async Task<List<Quote>> GetQuotesAsync(QuoteFilter filter)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var query = _context.Quotes.AsQueryable();
|
var query = _context.Quotes.AsQueryable();
|
||||||
|
|
||||||
|
if (filter?.Season != null)
|
||||||
|
{
|
||||||
|
query = query.Where(quote => quote.Season == filter.Season);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (filter?.Episode != null)
|
||||||
|
{
|
||||||
|
query = query.Where(quote => quote.Episode == filter.Episode);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (filter?.TextKeyword != null)
|
||||||
|
{
|
||||||
|
query = query.Where(quote => quote.Text!.ToLower().Contains(filter.TextKeyword.ToLower()));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (filter?.Source != null)
|
||||||
|
{
|
||||||
|
query = query.Where(quote => quote.Source!.ToLower().Contains(filter.Source.ToLower()));
|
||||||
|
}
|
||||||
|
|
||||||
if (filter?.Narrator != null)
|
if (filter?.Narrator != null)
|
||||||
{
|
{
|
||||||
query = query.Where(quote => quote.Narrator!.ToLower().Contains(filter.Narrator.ToLower()));
|
query = query.Where(quote => quote.Narrator!.ToLower().Contains(filter.Narrator.ToLower()));
|
||||||
@@ -34,10 +53,17 @@ namespace server.Persistence.Repositories
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Implement GetQuoteByIdAsync() method.
|
public async Task<Quote> GetQuoteByIdAsync(string id)
|
||||||
public Task<Quote> GetQuoteByIdAsync(int id)
|
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
try
|
||||||
|
{
|
||||||
|
return await _context.Quotes.Find(quote => quote.Id == id).SingleOrDefaultAsync();
|
||||||
|
}
|
||||||
|
catch(Exception e)
|
||||||
|
{
|
||||||
|
Console.WriteLine(e);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user