-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
173197 - Persons API new endpoints (#601)
* Added cache service, new entities for the new endpoints * Added Mediator pattern E2E test is now via PersonsAPI.Client * - Added GetAllPersonsAssociatedWithAcademyAsync endpoint - Fixed the repository pattern - Refactored project references and moved interfaces to adhere with CA/DDD - Implemented Mediatr pattern - Added E2E API Client tests * Added value objects * Implemented Token based authentication * Added cache service, new entities for the new endpoints * Added Mediator pattern E2E test is now via PersonsAPI.Client * - Added GetAllPersonsAssociatedWithAcademyAsync endpoint - Fixed the repository pattern - Refactored project references and moved interfaces to adhere with CA/DDD - Implemented Mediatr pattern - Added E2E API Client tests * Implemented Token based authentication * Added API Key authentication support as a temporary workaround if JWT Token is not provided * Implemented batch endpoint to retrieve MPs by a collection of Constituencies * Added validation to the requests, refactored the pattern and palcements of the interfaces * Excluded EducationEstablishmentGovernance and GovernanceRoleType from migration * General refactoring, added auto fixture attributes
- Loading branch information
1 parent
2521439
commit 949b246
Showing
141 changed files
with
3,311 additions
and
881 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Dfe.Academies.Infrastructure.Caching | ||
{ | ||
public class CacheSettings | ||
{ | ||
public int DefaultDurationInSeconds { get; set; } = 5; | ||
public Dictionary<string, int> Durations { get; set; } = new(); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
Dfe.Academies.Api.Infrastructure/Caching/MemoryCacheService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using Microsoft.Extensions.Caching.Memory; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
using System.Diagnostics.CodeAnalysis; | ||
using Dfe.Academies.Domain.Interfaces.Caching; | ||
|
||
namespace Dfe.Academies.Infrastructure.Caching | ||
{ | ||
[ExcludeFromCodeCoverage] | ||
public class MemoryCacheService( | ||
IMemoryCache memoryCache, | ||
ILogger<MemoryCacheService> logger, | ||
IOptions<CacheSettings> cacheSettings) | ||
: ICacheService | ||
{ | ||
private readonly CacheSettings _cacheSettings = cacheSettings.Value; | ||
|
||
public async Task<T> GetOrAddAsync<T>(string cacheKey, Func<Task<T>> fetchFunction, string methodName) | ||
{ | ||
if (memoryCache.TryGetValue(cacheKey, out T? cachedValue)) | ||
{ | ||
logger.LogInformation("Cache hit for key: {CacheKey}", cacheKey); | ||
return cachedValue!; | ||
} | ||
|
||
logger.LogInformation("Cache miss for key: {CacheKey}. Fetching from source...", cacheKey); | ||
var result = await fetchFunction(); | ||
|
||
if (Equals(result, default(T))) return result; | ||
var cacheDuration = GetCacheDurationForMethod(methodName); | ||
memoryCache.Set(cacheKey, result, cacheDuration); | ||
logger.LogInformation("Cached result for key: {CacheKey} for duration: {CacheDuration}", cacheKey, cacheDuration); | ||
|
||
return result; | ||
} | ||
|
||
public void Remove(string cacheKey) | ||
{ | ||
memoryCache.Remove(cacheKey); | ||
logger.LogInformation("Cache removed for key: {CacheKey}", cacheKey); | ||
} | ||
|
||
private TimeSpan GetCacheDurationForMethod(string methodName) | ||
{ | ||
if (_cacheSettings.Durations.TryGetValue(methodName, out int durationInSeconds)) | ||
{ | ||
return TimeSpan.FromSeconds(durationInSeconds); | ||
} | ||
return TimeSpan.FromSeconds(_cacheSettings.DefaultDurationInSeconds); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
Dfe.Academies.Api.Infrastructure/InfrastructureServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using Dfe.Academies.Application.Common.Interfaces; | ||
using Dfe.Academies.Infrastructure; | ||
using Dfe.Academies.Infrastructure.Caching; | ||
using Dfe.Academies.Infrastructure.Repositories; | ||
using Dfe.Academies.Infrastructure.Security.Authorization; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.Configuration; | ||
using Dfe.Academies.Domain.Interfaces.Repositories; | ||
using Dfe.Academies.Domain.Interfaces.Caching; | ||
using Dfe.Academies.Infrastructure.QueryServices; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection | ||
{ | ||
public static class InfrastructureServiceCollectionExtensions | ||
{ | ||
public static IServiceCollection AddInfrastructureDependencyGroup( | ||
this IServiceCollection services, IConfiguration config) | ||
{ | ||
//Repos | ||
services.AddScoped<ITrustRepository, TrustRepository>(); | ||
services.AddScoped<IEstablishmentRepository, EstablishmentRepository>(); | ||
services.AddSingleton<ICensusDataRepository, CensusDataRepository>(); | ||
services.AddScoped<IEducationalPerformanceRepository, EducationalPerformanceRepository>(); | ||
|
||
//Db | ||
var connectionString = config.GetConnectionString("DefaultConnection"); | ||
|
||
services.AddDbContext<MstrContext>(options => | ||
options.UseSqlServer(connectionString)); | ||
|
||
services.AddDbContext<EdperfContext>(options => | ||
options.UseSqlServer(connectionString)); | ||
|
||
return services; | ||
} | ||
|
||
public static IServiceCollection AddPersonsApiInfrastructureDependencyGroup( | ||
this IServiceCollection services, IConfiguration config) | ||
{ | ||
//Repos | ||
services.AddScoped<ITrustRepository, TrustRepository>(); | ||
services.AddScoped<IEstablishmentRepository, EstablishmentRepository>(); | ||
services.AddScoped<IConstituencyRepository, ConstituencyRepository>(); | ||
services.AddScoped(typeof(IMstrRepository<>), typeof(MstrRepository<>)); | ||
services.AddScoped(typeof(IMopRepository<>), typeof(MopRepository<>)); | ||
|
||
// Query Services | ||
services.AddScoped<IEstablishmentQueryService, EstablishmentQueryService>(); | ||
|
||
//Cache service | ||
services.Configure<CacheSettings>(config.GetSection("CacheSettings")); | ||
services.AddSingleton<ICacheService, MemoryCacheService>(); | ||
|
||
//Db | ||
var connectionString = config.GetConnectionString("DefaultConnection"); | ||
|
||
services.AddDbContext<MstrContext>(options => | ||
options.UseSqlServer(connectionString)); | ||
|
||
services.AddDbContext<MopContext>(options => | ||
options.UseSqlServer(connectionString)); | ||
|
||
// Authentication | ||
services.AddCustomAuthorization(config); | ||
|
||
return services; | ||
} | ||
} | ||
} |
1 change: 0 additions & 1 deletion
1
Dfe.Academies.Api.Infrastructure/Migrations/Mstr/20240115130158_Initial.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,7 @@ | ||
using Dfe.Academies.Academisation.Data; | ||
using Dfe.Academies.Domain.Interfaces.Repositories; | ||
using Dfe.Academies.Infrastructure.Repositories; | ||
|
||
namespace Dfe.Academies.Infrastructure | ||
{ | ||
public class MopRepository<TEntity> : Repository<TEntity, MopContext> where TEntity : class, new() | ||
{ | ||
public MopRepository(MopContext dbContext) : base(dbContext) | ||
{ | ||
} | ||
} | ||
public class MopRepository<TEntity>(MopContext dbContext) : Repository<TEntity, MopContext>(dbContext), IMopRepository<TEntity> where TEntity : class, new(); | ||
} |
Oops, something went wrong.