Add Application configuration #11

Merged
Wesser merged 128 commits from feat/add-setup into release/v1.0.0 2024-06-01 07:35:30 +03:00
2 changed files with 14 additions and 55 deletions
Showing only changes of commit 202098b723 - Show all commits

View File

@ -1,54 +1,26 @@
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Mirea.Api.DataAccess.Application.Interfaces.DbContexts.Schedule; using Mirea.Api.DataAccess.Application.Interfaces.DbContexts.Schedule;
using Mirea.Api.DataAccess.Persistence.Contexts.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; namespace Mirea.Api.DataAccess.Persistence;
public static class DependencyInjection 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>(); services.AddDbContext<CampusDbContext>(DbConfig);
var connection = settings?.ConnectionStringSql; 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() services.AddDbContext<UberDbContext>(DbConfig);
{
{
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.AddScoped<ICampusDbContext>(provider => provider.GetService<CampusDbContext>()!); services.AddScoped<ICampusDbContext>(provider => provider.GetService<CampusDbContext>()!);
services.AddScoped<IDisciplineDbContext>(provider => provider.GetService<DisciplineDbContext>()!); services.AddScoped<IDisciplineDbContext>(provider => provider.GetService<DisciplineDbContext>()!);
@ -62,5 +34,7 @@ public static class DependencyInjection
services.AddScoped<ISpecificWeekDbContext>(provider => provider.GetService<SpecificWeekDbContext>()!); services.AddScoped<ISpecificWeekDbContext>(provider => provider.GetService<SpecificWeekDbContext>()!);
return services; return services;
void DbConfig(DbContextOptionsBuilder options) => options.UseSqlite(connection);
} }
} }

View File

@ -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; }
}