This commit is contained in:
john 2025-05-12 09:33:07 +02:00
parent cb9d5e332e
commit baea64229b
9 changed files with 69 additions and 11 deletions

View file

@ -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<TResponse> PostCommand<TResponse>(ICommand<TResponse> command, CancellationToken cancellationToken = default)
{
using var scope = host.Services.CreateScope();
var mediator = scope.ServiceProvider.GetRequiredService<IMediator>();
var response = await mediator.Send(command, cancellationToken);
return response;
}
}

View file

@ -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<IAuthenticationModule>(_ => new AuthenticationModule(host));
}
@ -40,5 +40,10 @@ public static class AuthenticationStartup
{
c.RegisterServicesFromAssembly(typeof(AuthenticationStartup).Assembly);
});
services.AddTransient(
typeof(IPipelineBehavior<,>),
typeof(SaveChangesPipelineBehaviour<,>)
);
}
}

View file

@ -0,0 +1,5 @@
using Femto.Common.Domain;
namespace Femto.Modules.Authentication.Application.Commands;
public record LoginCommand(string Username, string Password) : ICommand<Guid>;

View file

@ -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<LoginCommand, Guid>
{
public async Task<Guid> Handle(LoginCommand request, CancellationToken cancellationToken)
{
var user = new UserIdentity(request.Username);
user.SetPassword(request.Password);
await context.AddAsync(user, cancellationToken);
return user.Id;
}
}

View file

@ -1,5 +1,8 @@
using Femto.Common.Domain;
namespace Femto.Modules.Authentication.Application;
public interface IAuthenticationModule
{
Task<TResponse> PostCommand<TResponse>(ICommand<TResponse> command, CancellationToken cancellationToken = default);
}

View file

@ -20,4 +20,8 @@
<ProjectReference Include="..\Femto.Common\Femto.Common.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Models\DomainEventHandlers\" />
</ItemGroup>
</Project>