finished implementing characters endpoints

This commit is contained in:
StevanFreeborn
2022-06-24 11:35:33 -05:00
parent 9bc24949b7
commit 15eea06073
7 changed files with 157 additions and 89 deletions
@@ -1,19 +1,59 @@
using server.Models;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
namespace server.Persistence.Repositories
{
public class CharacterRepository : ICharacterRepository
{
// TODO: Implement getcharactersasync
public Task<List<Character>> GetCharactersAsync(CharacterFilter filter)
private readonly IDbContext _context;
public CharacterRepository(IDbContext context)
{
throw new NotImplementedException();
_context = context;
}
// TODO: Implement getcharacterbyidasync
public Task<Character> GetCharacterByIdAsync(string id)
public async Task<List<Character>> GetCharactersAsync(CharacterFilter filter)
{
throw new NotImplementedException();
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;
}
}
}
}