// Kyoo - A portable and vast media library solution.
// Copyright (c) Kyoo.
//
// See AUTHORS.md and LICENSE file in the project root for full license information.
//
// Kyoo is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// Kyoo is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Kyoo. If not, see .
using System;
using System.Collections.Generic;
using Kyoo.Abstractions.Controllers;
using Kyoo.Database;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Npgsql;
namespace Kyoo.Postgresql
{
///
/// A module to add postgresql capacity to the app.
///
public class PostgresModule : IPlugin
{
///
public string Slug => "postgresql";
///
public string Name => "Postgresql";
///
public string Description => "A database context for postgresql.";
///
public Dictionary Configuration => new();
///
public bool Enabled => _configuration.GetSelectedDatabase() == "postgres";
///
/// 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 PostgresModule(IConfiguration configuration, IWebHostEnvironment env)
{
_configuration = configuration;
_environment = env;
}
///
public void Configure(IServiceCollection services)
{
services.AddDbContext(x =>
{
x.UseNpgsql(_configuration.GetDatabaseConnection("postgres"));
if (_environment.IsDevelopment())
x.EnableDetailedErrors().EnableSensitiveDataLogging();
}, ServiceLifetime.Transient);
}
///
public void Initialize(IServiceProvider provider)
{
DatabaseContext context = provider.GetRequiredService();
context.Database.Migrate();
using NpgsqlConnection conn = (NpgsqlConnection)context.Database.GetDbConnection();
conn.Open();
conn.ReloadTypes();
}
}
}