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
@@ -7,7 +7,7 @@ namespace server.Controllers.v1
{ {
[ApiController] [ApiController]
[ApiVersion("1.0")] [ApiVersion("1.0")]
[Route("/api/episodes")] [Route("/api/characters")]
[Produces("application/json")] [Produces("application/json")]
public class CharactersController : ControllerBase public class CharactersController : ControllerBase
{ {
+12
View File
@@ -12,6 +12,12 @@ namespace server.Models
[BsonRepresentation(BsonType.ObjectId)] [BsonRepresentation(BsonType.ObjectId)]
public string? Id { get; set; } public string? Id { get; set; }
/// <summary>
/// The character's full name.
/// </summary>
[BsonElement("fullName")]
public string FullName => $"{FirstName} {LastName}";
/// <summary> /// <summary>
/// The character's first name. /// The character's first name.
/// </summary> /// </summary>
@@ -24,6 +30,12 @@ namespace server.Models
[BsonElement("lastName")] [BsonElement("lastName")]
public string? LastName { get; set; } public string? LastName { get; set; }
/// <summary>
/// The full name of the actor who potrayed the character.
/// </summary>
[BsonElement("actorFullName")]
public string ActorFullName => $"{ActorFirstName} {ActorLastName}";
/// <summary> /// <summary>
/// The first name of the actor who potrayed the character. /// The first name of the actor who potrayed the character.
/// </summary> /// </summary>
+14 -1
View File
@@ -8,6 +8,19 @@ namespace server.Models
{ {
public class CharacterFilter public class CharacterFilter
{ {
// TODO: Build character filter model /// <summary>
/// Used to filter characters by their name.
/// </summary>
public string? Name { get; set; } = null;
/// <summary>
/// Used to filter characters by the actor's name.
/// </summary>
public string? ActorName { get; set; } = null;
/// <summary>
/// Used to filter characters by a season.
/// </summary>
public int? Season { get; set; } = null;
} }
} }
@@ -1,19 +1,59 @@
using server.Models; using server.Models;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
namespace server.Persistence.Repositories namespace server.Persistence.Repositories
{ {
public class CharacterRepository : ICharacterRepository public class CharacterRepository : ICharacterRepository
{ {
// TODO: Implement getcharactersasync private readonly IDbContext _context;
public Task<List<Character>> GetCharactersAsync(CharacterFilter filter)
public CharacterRepository(IDbContext context)
{ {
throw new NotImplementedException(); _context = context;
} }
// TODO: Implement getcharacterbyidasync public async Task<List<Character>> GetCharactersAsync(CharacterFilter filter)
public Task<Character> GetCharacterByIdAsync(string id)
{ {
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;
}
} }
} }
} }
+5 -2
View File
@@ -2,13 +2,13 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApiExplorer; using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.Mvc.Versioning; using Microsoft.AspNetCore.Mvc.Versioning;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using server.Options; using server.SwaggerOptions;
using server.Persistence; using server.Persistence;
using server.Persistence.Repositories; using server.Persistence.Repositories;
using server.Persistence.Seed; using server.Persistence.Seed;
using System.Reflection; using System.Reflection;
using AspNetCoreRateLimit; using AspNetCoreRateLimit;
using server.Options.Filters; using server.SwaggerOptions.Filters;
if (args.Length == 2 && args[0].ToLower() == "seed") if (args.Length == 2 && args[0].ToLower() == "seed")
{ {
@@ -34,6 +34,8 @@ if (args.Length == 2 && args[0].ToLower() == "seed")
await seeder.SeedCharactersAsync(); await seeder.SeedCharactersAsync();
} }
} }
else
{
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
@@ -128,5 +130,6 @@ app.MapControllers();
app.UseIpRateLimiting(); app.UseIpRateLimiting();
app.Run(); app.Run();
}
@@ -3,7 +3,7 @@ using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen; using Swashbuckle.AspNetCore.SwaggerGen;
namespace server.Options namespace server.SwaggerOptions
{ {
public class ConfigureSwaggerOptions : IConfigureOptions<SwaggerGenOptions> public class ConfigureSwaggerOptions : IConfigureOptions<SwaggerGenOptions>
{ {
@@ -2,7 +2,7 @@
using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen; using Swashbuckle.AspNetCore.SwaggerGen;
namespace server.Options.Filters namespace server.SwaggerOptions.Filters
{ {
public class ApiVersionOperationFilter : IOperationFilter public class ApiVersionOperationFilter : IOperationFilter
{ {