mirror of
https://github.com/meysamhadeli/booking-microservices.git
synced 2026-04-15 22:04:05 +08:00
refactor: refactor persist message processor
This commit is contained in:
parent
eab6386f3d
commit
fe8c0f444d
@ -5,10 +5,8 @@ using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace BuildingBlocks.PersistMessageProcessor;
|
||||
|
||||
using EFCore;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
public static class Extensions
|
||||
{
|
||||
@ -39,38 +37,17 @@ public static class Extensions
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IApplicationBuilder UseMigration<TContext>(this IApplicationBuilder app, IWebHostEnvironment env)
|
||||
public static IApplicationBuilder UseMigrationPersistMessage<TContext>(this IApplicationBuilder app, IWebHostEnvironment env)
|
||||
where TContext : DbContext, IPersistMessageDbContext
|
||||
{
|
||||
MigrateDatabaseAsync<TContext>(app.ApplicationServices).GetAwaiter().GetResult();
|
||||
using var scope = app.ApplicationServices.CreateScope();
|
||||
|
||||
if (!env.IsEnvironment("test"))
|
||||
{
|
||||
SeedDataAsync(app.ApplicationServices).GetAwaiter().GetResult();
|
||||
}
|
||||
var persistMessageContext = scope.ServiceProvider.GetRequiredService<PersistMessageDbContext>();
|
||||
persistMessageContext.Database.Migrate();
|
||||
|
||||
var context = scope.ServiceProvider.GetRequiredService<TContext>();
|
||||
context.Database.Migrate();
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
private static async Task MigrateDatabaseAsync<TContext>(IServiceProvider serviceProvider)
|
||||
where TContext : DbContext, IPersistMessageDbContext
|
||||
{
|
||||
using var scope = serviceProvider.CreateScope();
|
||||
|
||||
var persistMessageContext = scope.ServiceProvider.GetRequiredService<PersistMessageDbContext>();
|
||||
await persistMessageContext.Database.MigrateAsync();
|
||||
|
||||
var context = scope.ServiceProvider.GetRequiredService<TContext>();
|
||||
await context.Database.MigrateAsync();
|
||||
}
|
||||
|
||||
private static async Task SeedDataAsync(IServiceProvider serviceProvider)
|
||||
{
|
||||
using var scope = serviceProvider.CreateScope();
|
||||
var seeders = scope.ServiceProvider.GetServices<IDataSeeder>();
|
||||
foreach (var seeder in seeders)
|
||||
{
|
||||
await seeder.SeedAllAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -100,7 +100,7 @@ public static class InfrastructureExtensions
|
||||
});
|
||||
app.UseCorrelationId();
|
||||
app.UseHttpMetrics();
|
||||
app.UseMigration<PersistMessageDbContext>(env);
|
||||
app.UseMigrationPersistMessage<PersistMessageDbContext>(env);
|
||||
app.UseCustomHealthCheck();
|
||||
app.MapMetrics();
|
||||
app.MapGet("/", x => x.Response.WriteAsync(appOptions.Name));
|
||||
|
||||
@ -30,6 +30,7 @@ using Serilog;
|
||||
|
||||
namespace Flight.Extensions.Infrastructure;
|
||||
|
||||
using BuildingBlocks.PersistMessageProcessor.Data;
|
||||
using Microsoft.AspNetCore.HttpOverrides;
|
||||
|
||||
public static class InfrastructureExtensions
|
||||
@ -105,6 +106,7 @@ public static class InfrastructureExtensions
|
||||
});
|
||||
app.UseCorrelationId();
|
||||
app.UseHttpMetrics();
|
||||
app.UseMigrationPersistMessage<PersistMessageDbContext>(env);
|
||||
app.UseMigration<FlightDbContext>(env);
|
||||
app.MapMetrics();
|
||||
app.UseCustomHealthCheck();
|
||||
|
||||
@ -26,6 +26,7 @@ using Serilog;
|
||||
|
||||
namespace Identity.Extensions.Infrastructure;
|
||||
|
||||
using BuildingBlocks.PersistMessageProcessor.Data;
|
||||
using Configurations;
|
||||
using Microsoft.AspNetCore.HttpOverrides;
|
||||
|
||||
@ -101,6 +102,7 @@ public static class InfrastructureExtensions
|
||||
{
|
||||
options.EnrichDiagnosticContext = LogEnrichHelper.EnrichFromRequest;
|
||||
});
|
||||
app.UseMigrationPersistMessage<PersistMessageDbContext>(env);
|
||||
app.UseMigration<IdentityContext>(env);
|
||||
app.UseCorrelationId();
|
||||
app.UseHttpMetrics();
|
||||
|
||||
@ -28,6 +28,8 @@ using Serilog;
|
||||
|
||||
namespace Passenger.Extensions.Infrastructure;
|
||||
|
||||
using BuildingBlocks.PersistMessageProcessor.Data;
|
||||
|
||||
public static class InfrastructureExtensions
|
||||
{
|
||||
public static WebApplicationBuilder AddInfrastructure(this WebApplicationBuilder builder)
|
||||
@ -95,6 +97,7 @@ public static class InfrastructureExtensions
|
||||
{
|
||||
options.EnrichDiagnosticContext = LogEnrichHelper.EnrichFromRequest;
|
||||
});
|
||||
app.UseMigrationPersistMessage<PersistMessageDbContext>(env);
|
||||
app.UseMigration<PassengerDbContext>(env);
|
||||
app.UseCorrelationId();
|
||||
app.UseHttpMetrics();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user