using System; using System.Collections.Generic; using Kyoo.Abstractions.Controllers; using Microsoft.AspNetCore.Hosting; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; namespace Kyoo.SqLite { /// /// A module to add sqlite capacity to the app. /// public class SqLiteModule : IPlugin { /// public string Slug => "sqlite"; /// public string Name => "SqLite"; /// public string Description => "A database context for sqlite."; /// public Dictionary Configuration => new(); /// public bool Enabled => _configuration.GetSelectedDatabase() == "sqlite"; /// /// The configuration to use. The database connection string is pulled from it. /// private readonly IConfiguration _configuration; /// /// The host environment to check if the app is in debug mode. /// private readonly IWebHostEnvironment _environment; /// /// Create a new postgres module instance and use the given configuration and environment. /// /// The configuration to use /// The environment that will be used (if the env is in development mode, more information will be displayed on errors. public SqLiteModule(IConfiguration configuration, IWebHostEnvironment env) { _configuration = configuration; _environment = env; } /// public void Configure(IServiceCollection services) { services.AddDbContext(x => { x.UseSqlite(_configuration.GetDatabaseConnection("sqlite")); if (_environment.IsDevelopment()) x.EnableDetailedErrors().EnableSensitiveDataLogging(); }, ServiceLifetime.Transient); } /// public void Initialize(IServiceProvider provider) { DatabaseContext context = provider.GetRequiredService(); context.Database.Migrate(); } } }