-
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using Apollinare.API.Hubs;
|
||||
using Apollinare.API.Services;
|
||||
using Apollinare.API.Services.Reports;
|
||||
using Apollinare.API.Modules.Warehouse.Services;
|
||||
using Apollinare.Infrastructure.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Text.Json.Serialization;
|
||||
@@ -20,6 +21,9 @@ builder.Services.AddScoped<ReportGeneratorService>();
|
||||
builder.Services.AddScoped<ModuleService>();
|
||||
builder.Services.AddSingleton<DataNotificationService>();
|
||||
|
||||
// Warehouse Module Services
|
||||
builder.Services.AddScoped<IWarehouseService, WarehouseService>();
|
||||
|
||||
// Memory cache for module state
|
||||
builder.Services.AddMemoryCache();
|
||||
|
||||
@@ -58,18 +62,48 @@ builder.Services.AddOpenApi();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Initialize database
|
||||
if (app.Environment.IsDevelopment())
|
||||
// Apply pending migrations automatically on startup
|
||||
using (var scope = app.Services.CreateScope())
|
||||
{
|
||||
using var scope = app.Services.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<AppollinareDbContext>();
|
||||
db.Database.EnsureCreated();
|
||||
var logger = scope.ServiceProvider.GetRequiredService<ILogger<Program>>();
|
||||
|
||||
try
|
||||
{
|
||||
var pendingMigrations = await db.Database.GetPendingMigrationsAsync();
|
||||
if (pendingMigrations.Any())
|
||||
{
|
||||
logger.LogInformation("Applying {Count} pending migrations: {Migrations}",
|
||||
pendingMigrations.Count(),
|
||||
string.Join(", ", pendingMigrations));
|
||||
await db.Database.MigrateAsync();
|
||||
logger.LogInformation("Migrations applied successfully");
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.LogInformation("Database is up to date, no migrations to apply");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Error applying migrations");
|
||||
throw;
|
||||
}
|
||||
|
||||
// Seed data (only in development or if database is empty)
|
||||
DbSeeder.Seed(db);
|
||||
|
||||
// Seed default modules
|
||||
var moduleService = scope.ServiceProvider.GetRequiredService<ModuleService>();
|
||||
await moduleService.SeedDefaultModulesAsync();
|
||||
|
||||
// Seed warehouse default data
|
||||
var warehouseService = scope.ServiceProvider.GetRequiredService<IWarehouseService>();
|
||||
await warehouseService.SeedDefaultDataAsync();
|
||||
}
|
||||
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.MapOpenApi();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user