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,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<ActionResult<LoginResponse>> 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")]

View file

@ -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<string>("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();

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>

View file

@ -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<IStorageProvider>(s => new FilesystemStorageProvider(storageRoot));
services.AddMediatR(c => c.RegisterServicesFromAssembly(typeof(Startup).Assembly));
services.AddTransient(
typeof(IPipelineBehavior<,>),
typeof(SaveChangesPipelineBehaviour<,>)
);
});
var host = hostBuilder.Build();