From baea64229b540695db8c157c28817f0f77d40b47 Mon Sep 17 00:00:00 2001 From: john Date: Mon, 12 May 2025 09:33:07 +0200 Subject: [PATCH] wip --- Femto.Api/Controllers/Auth/AuthController.cs | 9 +++++++-- Femto.Api/Program.cs | 12 ++++++------ .../Application/AuthenticationModule.cs | 10 ++++++++++ .../Application/AuthenticationStartup.cs | 9 +++++++-- .../Commands/Login/LoginCommand.cs | 5 +++++ .../Commands/Login/LoginCommandHandler.cs | 19 +++++++++++++++++++ .../Application/IAuthenticationModule.cs | 3 +++ .../Femto.Modules.Authentication.csproj | 4 ++++ Femto.Modules.Media/Application/Startup.cs | 9 ++++++++- 9 files changed, 69 insertions(+), 11 deletions(-) create mode 100644 Femto.Modules.Authentication/Application/Commands/Login/LoginCommand.cs create mode 100644 Femto.Modules.Authentication/Application/Commands/Login/LoginCommandHandler.cs diff --git a/Femto.Api/Controllers/Auth/AuthController.cs b/Femto.Api/Controllers/Auth/AuthController.cs index 717d9c2..29c26e7 100644 --- a/Femto.Api/Controllers/Auth/AuthController.cs +++ b/Femto.Api/Controllers/Auth/AuthController.cs @@ -1,15 +1,20 @@ +using Femto.Modules.Authentication.Application; using Microsoft.AspNetCore.Mvc; namespace Femto.Api.Controllers.Auth; [ApiController] [Route("auth")] -public class AuthController : ControllerBase +public class AuthController(IAuthenticationModule authModule) : ControllerBase { [HttpPost("login")] public async Task> Login([FromBody] LoginRequest request) { - return new LoginResponse(Guid.Parse("0196960c-6296-7532-ba66-8fabb38c6ae0"), "johnbotris", "token"); + + var userId = await authModule.PostCommand(new LoginCommand(request.Username, request.Password)); + + + throw new NotImplementedException(); } [HttpPost("signup")] diff --git a/Femto.Api/Program.cs b/Femto.Api/Program.cs index 3d0af32..b85dfa3 100644 --- a/Femto.Api/Program.cs +++ b/Femto.Api/Program.cs @@ -1,24 +1,24 @@ using System.Text.Json; using System.Text.Json.Serialization; +using Femto.Modules.Authentication.Application; using Femto.Modules.Blog.Application; -using Femto.Modules.Media; using Femto.Modules.Media.Application; var builder = WebApplication.CreateBuilder(args); builder.Services.AddOpenApi(); -var databaseConnectionString = builder.Configuration.GetConnectionString("Database"); -if (databaseConnectionString is null) +var connectionString = builder.Configuration.GetConnectionString("Database"); +if (connectionString is null) throw new Exception("no database connection string found"); var blobStorageRoot = builder.Configuration.GetValue("BlobStorageRoot"); if (blobStorageRoot is null) throw new Exception("no blob storage root found"); -builder.Services.InitializeBlogModule(databaseConnectionString); -builder.Services.InitializeMediaModule(databaseConnectionString, blobStorageRoot); -// builder.Services.UseIdentityModule(databaseConnectionString); +builder.Services.InitializeBlogModule(connectionString); +builder.Services.InitializeMediaModule(connectionString, blobStorageRoot); +builder.Services.InitializeAuthenticationModule(connectionString); builder.Services.AddControllers(); diff --git a/Femto.Modules.Authentication/Application/AuthenticationModule.cs b/Femto.Modules.Authentication/Application/AuthenticationModule.cs index 6bf25e3..136c466 100644 --- a/Femto.Modules.Authentication/Application/AuthenticationModule.cs +++ b/Femto.Modules.Authentication/Application/AuthenticationModule.cs @@ -1,8 +1,18 @@ +using Femto.Common.Domain; using Femto.Modules.Authentication.Data; +using MediatR; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; namespace Femto.Modules.Authentication.Application; internal class AuthenticationModule(IHost host) : IAuthenticationModule { + public async Task PostCommand(ICommand command, CancellationToken cancellationToken = default) + { + using var scope = host.Services.CreateScope(); + var mediator = scope.ServiceProvider.GetRequiredService(); + var response = await mediator.Send(command, cancellationToken); + return response; + } } \ No newline at end of file diff --git a/Femto.Modules.Authentication/Application/AuthenticationStartup.cs b/Femto.Modules.Authentication/Application/AuthenticationStartup.cs index a4dda55..fa83d79 100644 --- a/Femto.Modules.Authentication/Application/AuthenticationStartup.cs +++ b/Femto.Modules.Authentication/Application/AuthenticationStartup.cs @@ -1,5 +1,7 @@ +using Femto.Common.Infrastructure; using Femto.Common.Infrastructure.Outbox; using Femto.Modules.Authentication.Data; +using MediatR; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; @@ -12,9 +14,7 @@ public static class AuthenticationStartup { var hostBuilder = Host.CreateDefaultBuilder(); hostBuilder.ConfigureServices(services => ConfigureServices(services, connectionString)); - var host = hostBuilder.Build(); - rootContainer.AddScoped(_ => new AuthenticationModule(host)); } @@ -40,5 +40,10 @@ public static class AuthenticationStartup { c.RegisterServicesFromAssembly(typeof(AuthenticationStartup).Assembly); }); + + services.AddTransient( + typeof(IPipelineBehavior<,>), + typeof(SaveChangesPipelineBehaviour<,>) + ); } } \ No newline at end of file diff --git a/Femto.Modules.Authentication/Application/Commands/Login/LoginCommand.cs b/Femto.Modules.Authentication/Application/Commands/Login/LoginCommand.cs new file mode 100644 index 0000000..b7e8356 --- /dev/null +++ b/Femto.Modules.Authentication/Application/Commands/Login/LoginCommand.cs @@ -0,0 +1,5 @@ +using Femto.Common.Domain; + +namespace Femto.Modules.Authentication.Application.Commands; + +public record LoginCommand(string Username, string Password) : ICommand; \ No newline at end of file diff --git a/Femto.Modules.Authentication/Application/Commands/Login/LoginCommandHandler.cs b/Femto.Modules.Authentication/Application/Commands/Login/LoginCommandHandler.cs new file mode 100644 index 0000000..5429b2f --- /dev/null +++ b/Femto.Modules.Authentication/Application/Commands/Login/LoginCommandHandler.cs @@ -0,0 +1,19 @@ +using Femto.Modules.Authentication.Data; +using Femto.Modules.Authentication.Models; +using MediatR; + +namespace Femto.Modules.Authentication.Application.Commands; + +internal class LoginCommandHandler(AuthenticationContext context) : IRequestHandler +{ + public async Task Handle(LoginCommand request, CancellationToken cancellationToken) + { + var user = new UserIdentity(request.Username); + + user.SetPassword(request.Password); + + await context.AddAsync(user, cancellationToken); + + return user.Id; + } +} \ No newline at end of file diff --git a/Femto.Modules.Authentication/Application/IAuthenticationModule.cs b/Femto.Modules.Authentication/Application/IAuthenticationModule.cs index 162e3e9..dcd7b67 100644 --- a/Femto.Modules.Authentication/Application/IAuthenticationModule.cs +++ b/Femto.Modules.Authentication/Application/IAuthenticationModule.cs @@ -1,5 +1,8 @@ +using Femto.Common.Domain; + namespace Femto.Modules.Authentication.Application; public interface IAuthenticationModule { + Task PostCommand(ICommand command, CancellationToken cancellationToken = default); } \ No newline at end of file diff --git a/Femto.Modules.Authentication/Femto.Modules.Authentication.csproj b/Femto.Modules.Authentication/Femto.Modules.Authentication.csproj index bc3de4c..bd6d1eb 100644 --- a/Femto.Modules.Authentication/Femto.Modules.Authentication.csproj +++ b/Femto.Modules.Authentication/Femto.Modules.Authentication.csproj @@ -20,4 +20,8 @@ + + + + diff --git a/Femto.Modules.Media/Application/Startup.cs b/Femto.Modules.Media/Application/Startup.cs index bb6e41d..d9c5019 100644 --- a/Femto.Modules.Media/Application/Startup.cs +++ b/Femto.Modules.Media/Application/Startup.cs @@ -1,5 +1,7 @@ -using Femto.Modules.Media.Data; +using Femto.Common.Infrastructure; +using Femto.Modules.Media.Data; using Femto.Modules.Media.Infrastructure; +using MediatR; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; @@ -21,6 +23,11 @@ public static class Startup }); services.AddTransient(s => new FilesystemStorageProvider(storageRoot)); services.AddMediatR(c => c.RegisterServicesFromAssembly(typeof(Startup).Assembly)); + + services.AddTransient( + typeof(IPipelineBehavior<,>), + typeof(SaveChangesPipelineBehaviour<,>) + ); }); var host = hostBuilder.Build();