2022-06-24 10:10:02 -05:00
|
|
|
using server.Models;
|
2022-06-24 11:35:33 -05:00
|
|
|
using MongoDB.Driver;
|
|
|
|
|
using MongoDB.Driver.Linq;
|
2022-06-24 10:10:02 -05:00
|
|
|
|
|
|
|
|
namespace server.Persistence.Repositories
|
2022-06-24 09:58:35 -05:00
|
|
|
{
|
|
|
|
|
public class CharacterRepository : ICharacterRepository
|
|
|
|
|
{
|
2022-06-24 11:35:33 -05:00
|
|
|
private readonly IDbContext _context;
|
|
|
|
|
|
|
|
|
|
public CharacterRepository(IDbContext context)
|
2022-06-24 10:10:02 -05:00
|
|
|
{
|
2022-06-24 11:35:33 -05:00
|
|
|
_context = context;
|
2022-06-24 10:10:02 -05:00
|
|
|
}
|
|
|
|
|
|
2022-06-24 11:35:33 -05:00
|
|
|
public async Task<List<Character>> GetCharactersAsync(CharacterFilter filter)
|
2022-06-24 10:10:02 -05:00
|
|
|
{
|
2022-06-24 11:35:33 -05:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var query = _context.Characters.AsQueryable();
|
|
|
|
|
|
|
|
|
|
if (filter?.Name != null)
|
|
|
|
|
{
|
|
|
|
|
query = query.Where(character => character.FullName.ToLower().Contains(filter.Name.ToLower()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (filter?.ActorName != null)
|
|
|
|
|
{
|
|
|
|
|
query = query.Where(character => character.ActorFullName.ToLower().Contains(filter.ActorName.ToLower()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (filter?.Season != null)
|
|
|
|
|
{
|
|
|
|
|
query = query.Where(character => character.Seasons.Any(season => season == filter.Season));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return await query.ToListAsync();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(e);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<Character> GetCharacterByIdAsync(string id)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return await _context.Characters.Find(character => character.Id == id).SingleOrDefaultAsync();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(e);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2022-06-24 10:10:02 -05:00
|
|
|
}
|
2022-06-24 09:58:35 -05:00
|
|
|
}
|
|
|
|
|
}
|