89 lines
3.5 KiB
C#
89 lines
3.5 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using RobotNet10.MapManager.Data;
|
|
using RobotNet10.MapManager.Services;
|
|
using RobotNet10.StorageManager;
|
|
|
|
namespace RobotNet10.MapManager.Extensions;
|
|
|
|
/// <summary>
|
|
/// Extension methods for registering MapManager services
|
|
/// </summary>
|
|
public static class ServiceCollectionExtensions
|
|
{
|
|
/// <summary>
|
|
/// Add MapManager services to the DI container
|
|
/// </summary>
|
|
/// <param name="services">Service collection</param>
|
|
/// <param name="configuration">Configuration</param>
|
|
/// <param name="dbContextOptions">action register DbContext</param>
|
|
/// <returns></returns>
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
public static IServiceCollection AddMapManager(this IServiceCollection services,
|
|
IConfiguration configuration,
|
|
Action<DbContextOptionsBuilder> dbContextOptions,
|
|
string configSectionName = "LayoutImage")
|
|
{
|
|
// Register DbContext
|
|
services.AddDbContext<MapDbContext>(dbContextOptions);
|
|
|
|
services.Configure<StorageConfig>("LayoutImages", options =>
|
|
{
|
|
configuration.GetSection(configSectionName).Bind(options);
|
|
});
|
|
services.AddSingleton<IImageStorageService, FileSystemImageStorageService>();
|
|
// Register Business Services
|
|
services.AddScoped<ILayoutService, LayoutService>();
|
|
services.AddScoped<IVehicleTypeService, VehicleTypeService>();
|
|
services.AddScoped<INodeService, NodeService>();
|
|
services.AddScoped<IEdgeService, EdgeService>();
|
|
services.AddScoped<IStationService, StationService>();
|
|
services.AddScoped<ILayoutDataService, LayoutDataService>();
|
|
services.AddScoped<IMapQueryService, MapQueryService>();
|
|
services.AddScoped<LayoutLevelNamingService>();
|
|
|
|
return services;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks MapManager database connectivity
|
|
/// Call this after app.Build() to verify database is accessible
|
|
/// Note: Does NOT apply migrations or seed data - only checks connectivity
|
|
/// </summary>
|
|
/// <param name="services">Service provider</param>
|
|
/// <returns>Task</returns>
|
|
public static async Task SeedMapManagerAsync(this IServiceProvider services)
|
|
{
|
|
using var scope = services.CreateScope();
|
|
var serviceProvider = scope.ServiceProvider;
|
|
var logger = serviceProvider.GetRequiredService<ILogger<MapDbContext>>();
|
|
|
|
try
|
|
{
|
|
var context = serviceProvider.GetRequiredService<MapDbContext>();
|
|
logger.LogInformation("Skipping automatic MapManager migration at startup. Running connectivity check only.");
|
|
|
|
// Check if database can be connected
|
|
logger.LogInformation("Checking MapManager database connection...");
|
|
var canConnect = await context.Database.CanConnectAsync();
|
|
|
|
if (canConnect)
|
|
{
|
|
logger.LogInformation("MapManager database is accessible");
|
|
}
|
|
else
|
|
{
|
|
logger.LogWarning("MapManager database is not accessible. Please ensure database is created.");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.LogError(ex, "Error occurred while checking MapManager database");
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
|