Add Application configuration #11
@ -1,54 +1,26 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Mirea.Api.DataAccess.Application.Interfaces.DbContexts.Schedule;
|
||||
using Mirea.Api.DataAccess.Persistence.Contexts.Schedule;
|
||||
using Mirea.Api.DataAccess.Persistence.Properties;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Mirea.Api.DataAccess.Persistence;
|
||||
|
||||
public static class DependencyInjection
|
||||
{
|
||||
public static IServiceCollection AddPersistence(this IServiceCollection services, IConfiguration configuration)
|
||||
public static IServiceCollection AddPersistence(this IServiceCollection services, string connection)
|
||||
{
|
||||
var settings = configuration.GetSection(nameof(DbSettings)).Get<DbSettings>();
|
||||
var connection = settings?.ConnectionStringSql;
|
||||
services.AddDbContext<CampusDbContext>(DbConfig);
|
||||
services.AddDbContext<DisciplineDbContext>(DbConfig);
|
||||
services.AddDbContext<FacultyDbContext>(DbConfig);
|
||||
services.AddDbContext<GroupDbContext>(DbConfig);
|
||||
services.AddDbContext<LectureHallDbContext>(DbConfig);
|
||||
services.AddDbContext<LessonAssociationDbContext>(DbConfig);
|
||||
services.AddDbContext<ProfessorDbContext>(DbConfig);
|
||||
services.AddDbContext<LessonDbContext>(DbConfig);
|
||||
services.AddDbContext<TypeOfOccupationDbContext>(DbConfig);
|
||||
services.AddDbContext<SpecificWeekDbContext>(DbConfig);
|
||||
|
||||
Dictionary<DatabaseEnum, Action<DbContextOptionsBuilder>> dbConfigurations = new()
|
||||
{
|
||||
{
|
||||
DatabaseEnum.Mysql,
|
||||
options => options.UseMySql(connection, ServerVersion.AutoDetect(connection))
|
||||
},
|
||||
{
|
||||
DatabaseEnum.Sqlite,
|
||||
options => options.UseSqlite(connection)
|
||||
},
|
||||
{
|
||||
DatabaseEnum.PostgresSql,
|
||||
options => options.UseNpgsql(connection)
|
||||
}
|
||||
};
|
||||
|
||||
if (dbConfigurations.TryGetValue((DatabaseEnum)settings?.TypeDatabase!, out var dbConfig))
|
||||
{
|
||||
services.AddDbContext<CampusDbContext>(dbConfig);
|
||||
services.AddDbContext<DisciplineDbContext>(dbConfig);
|
||||
services.AddDbContext<FacultyDbContext>(dbConfig);
|
||||
services.AddDbContext<GroupDbContext>(dbConfig);
|
||||
services.AddDbContext<LectureHallDbContext>(dbConfig);
|
||||
services.AddDbContext<LessonAssociationDbContext>(dbConfig);
|
||||
services.AddDbContext<ProfessorDbContext>(dbConfig);
|
||||
services.AddDbContext<LessonDbContext>(dbConfig);
|
||||
services.AddDbContext<TypeOfOccupationDbContext>(dbConfig);
|
||||
services.AddDbContext<SpecificWeekDbContext>(dbConfig);
|
||||
|
||||
services.AddDbContext<UberDbContext>(dbConfig);
|
||||
}
|
||||
else
|
||||
throw new NotSupportedException("Unsupported database type");
|
||||
services.AddDbContext<UberDbContext>(DbConfig);
|
||||
|
||||
services.AddScoped<ICampusDbContext>(provider => provider.GetService<CampusDbContext>()!);
|
||||
services.AddScoped<IDisciplineDbContext>(provider => provider.GetService<DisciplineDbContext>()!);
|
||||
@ -62,5 +34,7 @@ public static class DependencyInjection
|
||||
services.AddScoped<ISpecificWeekDbContext>(provider => provider.GetService<SpecificWeekDbContext>()!);
|
||||
|
||||
return services;
|
||||
|
||||
void DbConfig(DbContextOptionsBuilder options) => options.UseSqlite(connection);
|
||||
}
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
namespace Mirea.Api.DataAccess.Persistence.Properties;
|
||||
|
||||
public enum DatabaseEnum
|
||||
{
|
||||
Mysql,
|
||||
Sqlite,
|
||||
PostgresSql
|
||||
}
|
||||
public class DbSettings
|
||||
{
|
||||
public bool IsDoneConfiguration { get; set; }
|
||||
public DatabaseEnum TypeDatabase { get; set; }
|
||||
public required string ConnectionStringSql { get; set; }
|
||||
public DatabaseEnum? MigrateTo { get; set; }
|
||||
}
|
Loading…
Reference in New Issue
Block a user