implement getquotebyidasync

This commit is contained in:
StevanFreeborn
2022-06-23 16:43:17 -05:00
parent 911c199ba2
commit e459d20f54
6 changed files with 85 additions and 12 deletions
@@ -5,6 +5,6 @@ namespace server.Persistence.Repositories
public interface IQuoteRepository
{
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;
}
// TODO: Implement optional, but possible filters for query.
public async Task<List<Quote>> GetQuotesAsync(QuoteFilter filter)
{
try
{
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)
{
query = query.Where(quote => quote.Narrator!.ToLower().Contains(filter.Narrator.ToLower()));
@@ -34,10 +53,17 @@ namespace server.Persistence.Repositories
}
}
// TODO: Implement GetQuoteByIdAsync() method.
public Task<Quote> GetQuoteByIdAsync(int id)
public async Task<Quote> GetQuoteByIdAsync(string id)
{
throw new NotImplementedException();
try
{
return await _context.Quotes.Find(quote => quote.Id == id).SingleOrDefaultAsync();
}
catch(Exception e)
{
Console.WriteLine(e);
throw;
}
}
}