using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using RobotNet10.ScriptEngine.Data;
using RobotNet10.ScriptEngine.HubContexts;
using RobotNet10.ScriptEngine.Hubs;
using RobotNet10.ScriptEngine.Shared;
namespace RobotNet10.ScriptEngine;
public static class ScriptEngineExtensions
{
///
/// Adds ScriptEngine services to the service collection.
///
/// The type implementing IScriptEngineResource.
/// The service collection.
/// The action to configure DbContext options.
extension(IServiceCollection services)
{
public void AddScriptEngine(Action optionsAction)
where TScriptEngineResource : class, IScriptEngineResource
{
// Note: SignalR must be configured before calling this method
// services.AddSignalR() should be called in Program.cs before this method
services.AddSingleton()
.AddSingleton()
.AddSingleton()
.AddSingleton()
.AddSingleton()
.AddSingleton()
.AddSingleton()
.AddDbContext(optionsAction);
}
}
///
/// Maps ScriptEngine SignalR hubs to endpoints.
/// This extension method should be called in Program.cs after app.Build().
/// Example: app.MapScriptEngineHubs();
///
/// The web application.
extension(WebApplication app)
{
public void MapScriptEngineHubs()
{
app.MapHub(HubEndpoints.ScriptConsoleHubPath);
app.MapHub(HubEndpoints.ScriptManagerHubPath);
app.MapHub(HubEndpoints.ScriptFileManagerHubPath);
app.MapHub(HubEndpoints.InstanceMissionHubPath);
}
}
///
/// Seeds the ScriptEngine database by applying migrations.
///
/// The service provider.
extension(IServiceProvider serviceProvider)
{
public async Task SeedScriptEngineDbAsync()
{
using var scope = serviceProvider.CreateScope();
using var appDb = scope.ServiceProvider.GetRequiredService();
await appDb.Database.MigrateAsync();
await appDb.Database.EnsureCreatedAsync();
await appDb.SaveChangesAsync();
}
}
}