femto-backend/Femto.Modules.Blog/Module.cs
2025-05-04 21:46:24 +02:00

92 lines
2.8 KiB
C#

using System.Collections.Immutable;
using Femto.Modules.Blog.Data;
using Femto.Modules.Blog.Infrastructure;
using Femto.Modules.Blog.Infrastructure.DbConnection;
using Femto.Modules.Blog.Infrastructure.Integration;
using Femto.Modules.Blog.Infrastructure.Integration.Outbox;
using Femto.Modules.Blog.Infrastructure.PipelineBehaviours;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Quartz;
namespace Femto.Modules.Blog;
public static class Module
{
public static void UseBlogModule(this IServiceCollection services, string connectionString)
{
OutboxMessageTypeRegistry.RegisterOutboxMessages(
Contracts.Module.GetIntegrationEventTypes().ToImmutableDictionary()
);
services.AddDbContext<BlogContext>(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.AddMediatR(c =>
{
c.RegisterServicesFromAssembly(typeof(Module).Assembly);
});
services.AddQuartz(q =>
{
q.AddMailmanJob();
});
services.AddQuartzHostedService(options =>
{
options.WaitForJobsToComplete = true;
});
services.SetupMediatrPipeline();
services.AddTransient<Outbox, Outbox>();
services.AddTransient<IDbConnectionFactory>(_ => new DbConnectionFactory(connectionString));
}
private static void SetupMediatrPipeline(this IServiceCollection services)
{
services.AddTransient(
typeof(IPipelineBehavior<,>),
typeof(SaveChangesPipelineBehaviour<,>)
);
}
private static void AddMailmanJob(this IServiceCollectionQuartzConfigurator q)
{
var jobKey = JobKey.Create(nameof(MailmanJob));
q.AddJob<MailmanJob>(jobKey)
.AddTrigger(trigger =>
trigger
.ForJob(jobKey)
.WithSimpleSchedule(schedule =>
schedule.WithIntervalInSeconds(1).RepeatForever()
)
);
}
}