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;
}
} }
} }
} }
+43 -40
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,76 +34,78 @@ 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);
builder.Services.AddMemoryCache(); builder.Services.AddMemoryCache();
builder.Services.Configure<IpRateLimitOptions>( builder.Services.Configure<IpRateLimitOptions>(
builder.Configuration.GetSection("IpRateLimiting")); builder.Configuration.GetSection("IpRateLimiting"));
builder.Services.Configure<IpRateLimitPolicies>( builder.Services.Configure<IpRateLimitPolicies>(
builder.Configuration.GetSection("IpRateLimitPolicies")); builder.Configuration.GetSection("IpRateLimitPolicies"));
builder.Services.AddInMemoryRateLimiting(); builder.Services.AddInMemoryRateLimiting();
builder.Services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>(); builder.Services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();
builder.Services.Configure<DatabaseSettings>( builder.Services.Configure<DatabaseSettings>(
builder.Configuration.GetSection("MongoDBSettings")); builder.Configuration.GetSection("MongoDBSettings"));
builder.Services.AddSingleton<IDatabaseSettings>(sp => builder.Services.AddSingleton<IDatabaseSettings>(sp =>
sp.GetRequiredService<IOptions<DatabaseSettings>>().Value); sp.GetRequiredService<IOptions<DatabaseSettings>>().Value);
builder.Services.AddSingleton<IDbContext, DbContext>(); builder.Services.AddSingleton<IDbContext, DbContext>();
builder.Services.AddScoped<ISeasonRepository, SeasonRepository>(); builder.Services.AddScoped<ISeasonRepository, SeasonRepository>();
builder.Services.AddScoped<IEpisodeRepository, EpisodeRepository>(); builder.Services.AddScoped<IEpisodeRepository, EpisodeRepository>();
builder.Services.AddScoped<IQuoteRepository, QuoteRepository>(); builder.Services.AddScoped<IQuoteRepository, QuoteRepository>();
builder.Services.AddScoped<ICharacterRepository, CharacterRepository>(); builder.Services.AddScoped<ICharacterRepository, CharacterRepository>();
builder.Services.AddControllers(); builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options => builder.Services.AddSwaggerGen(options =>
{ {
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
options.IncludeXmlComments(xmlPath); options.IncludeXmlComments(xmlPath);
options.OperationFilter<ApiVersionOperationFilter>(); options.OperationFilter<ApiVersionOperationFilter>();
}); });
builder.Services.ConfigureOptions<ConfigureSwaggerOptions>(); builder.Services.ConfigureOptions<ConfigureSwaggerOptions>();
builder.Services.AddApiVersioning(config => builder.Services.AddApiVersioning(config =>
{ {
config.DefaultApiVersion = new ApiVersion(1, 0); config.DefaultApiVersion = new ApiVersion(1, 0);
config.AssumeDefaultVersionWhenUnspecified = true; config.AssumeDefaultVersionWhenUnspecified = true;
config.ReportApiVersions = true; config.ReportApiVersions = true;
config.ApiVersionReader = new HeaderApiVersionReader("x-api-version"); config.ApiVersionReader = new HeaderApiVersionReader("x-api-version");
}); });
builder.Services.AddVersionedApiExplorer(config => builder.Services.AddVersionedApiExplorer(config =>
{ {
config.GroupNameFormat = "'v'VVV"; config.GroupNameFormat = "'v'VVV";
}); });
var app = builder.Build(); var app = builder.Build();
app.UseStaticFiles(); app.UseStaticFiles();
app.UseSwagger(options => app.UseSwagger(options =>
{ {
options.RouteTemplate = "/{documentName}/docs.json"; options.RouteTemplate = "/{documentName}/docs.json";
}); });
app.UseSwaggerUI(options => app.UseSwaggerUI(options =>
{ {
var provider = app.Services.GetRequiredService<IApiVersionDescriptionProvider>(); var provider = app.Services.GetRequiredService<IApiVersionDescriptionProvider>();
foreach (var description in provider.ApiVersionDescriptions) foreach (var description in provider.ApiVersionDescriptions)
@@ -117,16 +119,17 @@ app.UseSwaggerUI(options =>
options.DisplayRequestDuration(); options.DisplayRequestDuration();
options.DocumentTitle = "criminalmindsapi"; options.DocumentTitle = "criminalmindsapi";
} }
}); });
app.UseHttpsRedirection(); app.UseHttpsRedirection();
app.UseAuthorization(); app.UseAuthorization();
app.MapControllers(); 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
{ {