2022-06-23 13:26:21 -05:00
|
|
|
using MongoDB.Driver;
|
2022-06-23 15:43:39 -05:00
|
|
|
using MongoDB.Driver.Linq;
|
2022-06-23 13:26:21 -05:00
|
|
|
using server.Models;
|
2022-06-22 11:28:44 -05:00
|
|
|
|
|
|
|
|
namespace server.Persistence.Repositories
|
|
|
|
|
{
|
|
|
|
|
public class QuoteRepository : IQuoteRepository
|
|
|
|
|
{
|
|
|
|
|
private readonly IDbContext _context;
|
|
|
|
|
|
|
|
|
|
public QuoteRepository(IDbContext context)
|
|
|
|
|
{
|
|
|
|
|
_context = context;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-23 13:26:21 -05:00
|
|
|
public async Task<List<Quote>> GetQuotesAsync(QuoteFilter filter)
|
2022-06-22 11:28:44 -05:00
|
|
|
{
|
2022-06-23 13:26:21 -05:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var query = _context.Quotes.AsQueryable();
|
2022-06-23 15:43:39 -05:00
|
|
|
|
2022-06-23 16:43:17 -05:00
|
|
|
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()));
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-23 15:43:39 -05:00
|
|
|
if (filter?.Narrator != null)
|
|
|
|
|
{
|
|
|
|
|
query = query.Where(quote => quote.Narrator!.ToLower().Contains(filter.Narrator.ToLower()));
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-23 13:26:21 -05:00
|
|
|
return await query.ToListAsync();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(e);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2022-06-22 11:28:44 -05:00
|
|
|
}
|
|
|
|
|
|
2022-06-23 16:43:17 -05:00
|
|
|
public async Task<Quote> GetQuoteByIdAsync(string id)
|
2022-06-22 11:28:44 -05:00
|
|
|
{
|
2022-06-23 16:43:17 -05:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return await _context.Quotes.Find(quote => quote.Id == id).SingleOrDefaultAsync();
|
|
|
|
|
}
|
|
|
|
|
catch(Exception e)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(e);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2022-06-22 11:28:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|