Fix some issues in the endpoints and use local mongodb

This commit is contained in:
Oscar Morales
2025-06-04 11:39:29 -06:00
parent ffc1afa8c9
commit f5b5f7d0f0
17 changed files with 410 additions and 316 deletions

View File

@@ -1,4 +1,6 @@
using Core.Thalos.Infraestructure.Caching.Contracts;
using Core.Blueprint.Mongo;
using Core.Thalos.Adapters;
using Core.Thalos.Infraestructure.Caching.Contracts;
using Core.Thalos.Infraestructure.Contexts.Mongo;
using Core.Thalos.Provider.Contracts;
using Core.Thalos.Provider.Providers;
@@ -14,60 +16,21 @@ namespace Core.Thalos.Provider
{
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddDALLayer(this IServiceCollection services, IConfiguration configuration)
public static IServiceCollection AddDALLayerServices(this IServiceCollection services, IConfiguration configuration)
{
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? string.Empty;
//Mongo
services.AddScoped<IModuleProvider, ModuleProvider>();
services.AddScoped<CollectionRepository<ModuleAdapter>>();
var connectionString = configuration.GetSection("ConnectionStrings:MongoDB").Value ?? string.Empty;
var databaseName = configuration.GetSection("MongoDB:DatabaseName").Value ?? string.Empty;
var audience = (environment == "Local")
? configuration.GetSection("MongoDB:LocalAudience").Value
: configuration.GetSection("MongoDB:Audience").Value;
services.AddScoped<IPermissionProvider, PermissionProvider>();
services.AddScoped<CollectionRepository<PermissionAdapter>>();
if (string.IsNullOrEmpty(connectionString) || string.IsNullOrEmpty(databaseName) || string.IsNullOrEmpty(audience))
{
throw new InvalidOperationException("Mongo connection is not configured correctly.");
}
services.Configure<MongoConnSettings>(options =>
{
options.ConnectionString = connectionString;
options.Databasename = databaseName;
options.Audience = audience ?? string.Empty;
});
services.AddSingleton<IMongoClient>(serviceProvider =>
{
var settings = serviceProvider.GetRequiredService<IOptions<MongoConnSettings>>().Value;
var mongoClientSettings = MongoClientSettings.FromConnectionString(settings.ConnectionString);
mongoClientSettings.Credential = MongoCredential.CreateOidcCredential(new HeathOidcCallback(settings.Audience));
return new MongoClient(mongoClientSettings);
});
services.AddSingleton<IMongoDatabase>(serviceProvider =>
{
var settings = serviceProvider.GetRequiredService<IOptions<MongoConnSettings>>().Value;
var client = serviceProvider.GetRequiredService<IMongoClient>();
return client.GetDatabase(settings.Databasename);
});
services.AddDALConfigurationLayer();
services.AddLogs();
services.AddRedisCacheService(configuration);
return services;
}
private static IServiceCollection AddDALConfigurationLayer(this IServiceCollection services)
{
services.AddHttpContextAccessor();
services.AddScoped<IRoleProvider, RoleProvider>();
services.AddScoped<CollectionRepository<RoleAdapter>>();
services.AddScoped<IUserProvider, UserProvider>();
services.AddScoped<IRoleProvider, RoleProvider>();
services.AddScoped<IPermissionProvider, PermissionProvider>();
services.AddScoped<IPermissionProvider, PermissionProvider>();
services.AddScoped<IModuleProvider, ModuleProvider>();
services.AddScoped<CollectionRepository<UserAdapter>>();
return services;
}