This commit is contained in:
john 2025-05-11 23:43:38 +02:00
parent 1ecaf64dea
commit cb9d5e332e
11 changed files with 72 additions and 69 deletions

View file

@ -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<ActionResult<UploadMediaResponse>> 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;

View file

@ -0,0 +1,8 @@
using Femto.Modules.Authentication.Data;
using Microsoft.Extensions.Hosting;
namespace Femto.Modules.Authentication.Application;
internal class AuthenticationModule(IHost host) : IAuthenticationModule
{
}

View file

@ -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<IAuthenticationModule>(_ => new AuthenticationModule(host));
}
private static void ConfigureServices(IServiceCollection services, string connectionString)
{
services.AddDbContext<AuthenticationContext>(
builder =>
{
builder.UseNpgsql(connectionString);
builder.UseSnakeCaseNamingConvention();
});
services.AddMediatR(c => c.RegisterServicesFromAssembly(typeof(AuthenticationStartup).Assembly));
services.AddDbContext<AuthenticationContext>(builder =>
{
builder.UseNpgsql();
builder.UseSnakeCaseNamingConvention();
builder.EnableSensitiveDataLogging();
});
services.AddMediatR(c =>
{
c.RegisterServicesFromAssembly(typeof(AuthenticationStartup).Assembly);
});
}
}

View file

@ -0,0 +1,5 @@
namespace Femto.Modules.Authentication.Application;
public interface IAuthenticationModule
{
}

View file

@ -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<AuthenticationContext>(
builder =>
{
builder.UseNpgsql(connectionString);
builder.UseSnakeCaseNamingConvention();
});
services.AddMediatR(c => c.RegisterServicesFromAssembly(typeof(Module).Assembly));
services.AddDbContext<AuthenticationContext>(builder =>
{
builder.UseNpgsql(
connectionString,
o =>
{
o.MapEnum<OutboxEntryStatus>("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<AuthenticationContext>();
services.AddMediatR(c =>
{
c.RegisterServicesFromAssembly(typeof(Module).Assembly);
});
services.AddTransient<Outbox<AuthenticationContext>, Outbox<AuthenticationContext>>();
}
}

View file

@ -4,6 +4,6 @@ namespace Femto.Modules.Media.Application;
public interface IMediaModule
{
Task<TResult> PostCommand<TResult>(ICommand<TResult> command);
Task<TResult> PostQuery<TResult>(IQuery<TResult> query);
Task<TResult> PostCommand<TResult>(ICommand<TResult> command, CancellationToken cancellationToken = default);
Task<TResult> PostQuery<TResult>(IQuery<TResult> query, CancellationToken cancellationToken = default);
}

View file

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

View file

@ -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<LoadFileQueryResult>;
public record LoadFileQuery(Guid FileId) : IQuery<LoadFileQueryResult>;

View file

@ -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<Guid>;
public record SaveFileCommand(Stream Data, string ContentType, long? ContentLength) : ICommand<Guid>;