From cb9d5e332e320afe8a0bfa8258603a0b07441ad0 Mon Sep 17 00:00:00 2001 From: john Date: Sun, 11 May 2025 23:43:38 +0200 Subject: [PATCH] wip --- .../Controllers/Media/MediaController.cs | 7 ++- .../Application/AuthenticationModule.cs | 8 +++ .../Application/AuthenticationStartup.cs | 44 ++++++++++++++ .../Application/IAuthenticationModule.cs | 5 ++ Femto.Modules.Authentication/Module.cs | 57 ------------------- .../Application/IMediaModule.cs | 4 +- .../{Data => Application}/MediaContext.cs | 0 .../Application/MediaModule.cs | 10 ++-- .../{Data => Application}/SavedBlob.cs | 0 .../Contracts/LoadFile/LoadFileQuery.cs | 3 +- .../Contracts/SaveFile/SaveFileCommand.cs | 3 +- 11 files changed, 72 insertions(+), 69 deletions(-) create mode 100644 Femto.Modules.Authentication/Application/AuthenticationModule.cs create mode 100644 Femto.Modules.Authentication/Application/AuthenticationStartup.cs create mode 100644 Femto.Modules.Authentication/Application/IAuthenticationModule.cs delete mode 100644 Femto.Modules.Authentication/Module.cs rename Femto.Modules.Media/{Data => Application}/MediaContext.cs (100%) rename Femto.Modules.Media/{Data => Application}/SavedBlob.cs (100%) diff --git a/Femto.Api/Controllers/Media/MediaController.cs b/Femto.Api/Controllers/Media/MediaController.cs index 44e23b6..2ad6515 100644 --- a/Femto.Api/Controllers/Media/MediaController.cs +++ b/Femto.Api/Controllers/Media/MediaController.cs @@ -1,4 +1,5 @@ using Femto.Api.Controllers.Media.Dto; +using Femto.Modules.Media.Application; using Femto.Modules.Media.Contracts; using Femto.Modules.Media.Contracts.LoadFile; using Femto.Modules.Media.Contracts.SaveFile; @@ -9,7 +10,7 @@ namespace Femto.Api.Controllers.Media; [ApiController] [Route("media")] -public class MediaController(IMediator mediator) : ControllerBase +public class MediaController(IMediaModule mediaModule) : ControllerBase { [HttpPost] public async Task> UploadMedia( @@ -18,7 +19,7 @@ public class MediaController(IMediator mediator) : ControllerBase ) { await using var data = file.OpenReadStream(); - var id = await mediator.Send( + var id = await mediaModule.PostCommand( new SaveFileCommand(data, file.ContentType, file.Length), cancellationToken ); @@ -30,7 +31,7 @@ public class MediaController(IMediator mediator) : ControllerBase [HttpGet("{id}")] public async Task GetMedia(Guid id, CancellationToken cancellationToken) { - var res = await mediator.Send(new LoadFileQuery(id), cancellationToken); + var res = await mediaModule.PostQuery(new LoadFileQuery(id), cancellationToken); HttpContext.Response.ContentType = res.Type; HttpContext.Response.ContentLength = res.Size; diff --git a/Femto.Modules.Authentication/Application/AuthenticationModule.cs b/Femto.Modules.Authentication/Application/AuthenticationModule.cs new file mode 100644 index 0000000..6bf25e3 --- /dev/null +++ b/Femto.Modules.Authentication/Application/AuthenticationModule.cs @@ -0,0 +1,8 @@ +using Femto.Modules.Authentication.Data; +using Microsoft.Extensions.Hosting; + +namespace Femto.Modules.Authentication.Application; + +internal class AuthenticationModule(IHost host) : IAuthenticationModule +{ +} \ No newline at end of file diff --git a/Femto.Modules.Authentication/Application/AuthenticationStartup.cs b/Femto.Modules.Authentication/Application/AuthenticationStartup.cs new file mode 100644 index 0000000..a4dda55 --- /dev/null +++ b/Femto.Modules.Authentication/Application/AuthenticationStartup.cs @@ -0,0 +1,44 @@ +using Femto.Common.Infrastructure.Outbox; +using Femto.Modules.Authentication.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; + +namespace Femto.Modules.Authentication.Application; + +public static class AuthenticationStartup +{ + public static void InitializeAuthenticationModule(this IServiceCollection rootContainer, string connectionString) + { + var hostBuilder = Host.CreateDefaultBuilder(); + hostBuilder.ConfigureServices(services => ConfigureServices(services, connectionString)); + + var host = hostBuilder.Build(); + + rootContainer.AddScoped(_ => new AuthenticationModule(host)); + } + + private static void ConfigureServices(IServiceCollection services, string connectionString) + { + services.AddDbContext( + builder => + { + builder.UseNpgsql(connectionString); + builder.UseSnakeCaseNamingConvention(); + }); + + services.AddMediatR(c => c.RegisterServicesFromAssembly(typeof(AuthenticationStartup).Assembly)); + + services.AddDbContext(builder => + { + builder.UseNpgsql(); + builder.UseSnakeCaseNamingConvention(); + builder.EnableSensitiveDataLogging(); + }); + + services.AddMediatR(c => + { + c.RegisterServicesFromAssembly(typeof(AuthenticationStartup).Assembly); + }); + } +} \ No newline at end of file diff --git a/Femto.Modules.Authentication/Application/IAuthenticationModule.cs b/Femto.Modules.Authentication/Application/IAuthenticationModule.cs new file mode 100644 index 0000000..162e3e9 --- /dev/null +++ b/Femto.Modules.Authentication/Application/IAuthenticationModule.cs @@ -0,0 +1,5 @@ +namespace Femto.Modules.Authentication.Application; + +public interface IAuthenticationModule +{ +} \ No newline at end of file diff --git a/Femto.Modules.Authentication/Module.cs b/Femto.Modules.Authentication/Module.cs deleted file mode 100644 index 9f46401..0000000 --- a/Femto.Modules.Authentication/Module.cs +++ /dev/null @@ -1,57 +0,0 @@ -using Femto.Common.Infrastructure.Outbox; -using Femto.Modules.Authentication.Data; -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; - -namespace Femto.Modules.Authentication; - -public static class Module -{ - public static void UseIdentityModule(this IServiceCollection services, string connectionString) - { - services.AddDbContext( - builder => - { - builder.UseNpgsql(connectionString); - builder.UseSnakeCaseNamingConvention(); - }); - - services.AddMediatR(c => c.RegisterServicesFromAssembly(typeof(Module).Assembly)); - - services.AddDbContext(builder => - { - builder.UseNpgsql( - connectionString, - o => - { - o.MapEnum("outbox_status"); - } - ); - - builder.UseSnakeCaseNamingConvention(); - - var loggerFactory = LoggerFactory.Create(b => - { - // b.AddConsole(); - // .AddFilter( - // (category, level) => - // category == DbLoggerCategory.Database.Command.Name - // && level == LogLevel.Debug - // ); - }); - - builder.UseLoggerFactory(loggerFactory); - builder.EnableSensitiveDataLogging(); - }); - - // services.AddOutbox(); - - services.AddMediatR(c => - { - c.RegisterServicesFromAssembly(typeof(Module).Assembly); - }); - - services.AddTransient, Outbox>(); - } -} \ No newline at end of file diff --git a/Femto.Modules.Media/Application/IMediaModule.cs b/Femto.Modules.Media/Application/IMediaModule.cs index 6d6d14b..8ccca67 100644 --- a/Femto.Modules.Media/Application/IMediaModule.cs +++ b/Femto.Modules.Media/Application/IMediaModule.cs @@ -4,6 +4,6 @@ namespace Femto.Modules.Media.Application; public interface IMediaModule { - Task PostCommand(ICommand command); - Task PostQuery(IQuery query); + Task PostCommand(ICommand command, CancellationToken cancellationToken = default); + Task PostQuery(IQuery query, CancellationToken cancellationToken = default); } \ No newline at end of file diff --git a/Femto.Modules.Media/Data/MediaContext.cs b/Femto.Modules.Media/Application/MediaContext.cs similarity index 100% rename from Femto.Modules.Media/Data/MediaContext.cs rename to Femto.Modules.Media/Application/MediaContext.cs diff --git a/Femto.Modules.Media/Application/MediaModule.cs b/Femto.Modules.Media/Application/MediaModule.cs index b22a1d8..2b66e36 100644 --- a/Femto.Modules.Media/Application/MediaModule.cs +++ b/Femto.Modules.Media/Application/MediaModule.cs @@ -5,21 +5,21 @@ using Microsoft.Extensions.Hosting; namespace Femto.Modules.Media.Application; -public class MediaModule(IHost host) : IMediaModule +internal class MediaModule(IHost host) : IMediaModule { - public async Task PostCommand(ICommand command) + public async Task PostCommand(ICommand command, CancellationToken cancellationToken) { using var scope = host.Services.CreateScope(); var mediator = scope.ServiceProvider.GetRequiredService(); - var response = await mediator.Send(command); + var response = await mediator.Send(command, cancellationToken); return response; } - public async Task PostQuery(IQuery query) + public async Task PostQuery(IQuery query, CancellationToken cancellationToken) { using var scope = host.Services.CreateScope(); var mediator = scope.ServiceProvider.GetRequiredService(); - var response = await mediator.Send(query); + var response = await mediator.Send(query, cancellationToken); return response; } } \ No newline at end of file diff --git a/Femto.Modules.Media/Data/SavedBlob.cs b/Femto.Modules.Media/Application/SavedBlob.cs similarity index 100% rename from Femto.Modules.Media/Data/SavedBlob.cs rename to Femto.Modules.Media/Application/SavedBlob.cs diff --git a/Femto.Modules.Media/Contracts/LoadFile/LoadFileQuery.cs b/Femto.Modules.Media/Contracts/LoadFile/LoadFileQuery.cs index 12938c0..9c00707 100644 --- a/Femto.Modules.Media/Contracts/LoadFile/LoadFileQuery.cs +++ b/Femto.Modules.Media/Contracts/LoadFile/LoadFileQuery.cs @@ -1,6 +1,7 @@ +using Femto.Common.Domain; using Femto.Modules.Media.Contracts.LoadFile.Dto; using MediatR; namespace Femto.Modules.Media.Contracts.LoadFile; -public record LoadFileQuery(Guid FileId) : IRequest; \ No newline at end of file +public record LoadFileQuery(Guid FileId) : IQuery; \ No newline at end of file diff --git a/Femto.Modules.Media/Contracts/SaveFile/SaveFileCommand.cs b/Femto.Modules.Media/Contracts/SaveFile/SaveFileCommand.cs index e3c2df4..e30e66e 100644 --- a/Femto.Modules.Media/Contracts/SaveFile/SaveFileCommand.cs +++ b/Femto.Modules.Media/Contracts/SaveFile/SaveFileCommand.cs @@ -1,5 +1,6 @@ +using Femto.Common.Domain; using MediatR; namespace Femto.Modules.Media.Contracts.SaveFile; -public record SaveFileCommand(Stream Data, string ContentType, long? ContentLength) : IRequest; \ No newline at end of file +public record SaveFileCommand(Stream Data, string ContentType, long? ContentLength) : ICommand; \ No newline at end of file