2024-01-26 19:39:59 +03:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Mirea.Api.DataAccess.Application.Interfaces.DbContexts.Schedule;
|
2024-05-30 20:17:36 +03:00
|
|
|
|
using Mirea.Api.DataAccess.Domain.Schedule;
|
|
|
|
|
using Mirea.Api.DataAccess.Persistence.Common;
|
2024-01-26 19:39:59 +03:00
|
|
|
|
using Mirea.Api.DataAccess.Persistence.Contexts.Schedule;
|
2024-05-30 20:17:36 +03:00
|
|
|
|
using Mirea.Api.DataAccess.Persistence.EntityTypeConfigurations;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
2024-01-26 19:39:59 +03:00
|
|
|
|
|
|
|
|
|
namespace Mirea.Api.DataAccess.Persistence;
|
|
|
|
|
|
|
|
|
|
public static class DependencyInjection
|
|
|
|
|
{
|
2024-05-30 20:17:36 +03:00
|
|
|
|
public static IServiceCollection AddPersistence(this IServiceCollection services, DatabaseProvider dbProvider, string connection)
|
2024-01-26 19:39:59 +03:00
|
|
|
|
{
|
2024-05-30 20:17:36 +03:00
|
|
|
|
services.AddDbContext<CampusDbContext>(options =>
|
|
|
|
|
UseDatabase(options).CreateDbContext<CampusDbContext, Campus>(dbProvider));
|
|
|
|
|
services.AddDbContext<DisciplineDbContext>(options =>
|
|
|
|
|
UseDatabase(options).CreateDbContext<DisciplineDbContext, Discipline>(dbProvider));
|
|
|
|
|
services.AddDbContext<FacultyDbContext>(options =>
|
|
|
|
|
UseDatabase(options).CreateDbContext<FacultyDbContext, Faculty>(dbProvider));
|
|
|
|
|
services.AddDbContext<GroupDbContext>(options =>
|
|
|
|
|
UseDatabase(options).CreateDbContext<GroupDbContext, Group>(dbProvider));
|
|
|
|
|
services.AddDbContext<LectureHallDbContext>(options =>
|
|
|
|
|
UseDatabase(options).CreateDbContext<LectureHallDbContext, LectureHall>(dbProvider));
|
|
|
|
|
services.AddDbContext<LessonAssociationDbContext>(options =>
|
|
|
|
|
UseDatabase(options).CreateDbContext<LessonAssociationDbContext, LessonAssociation>(dbProvider));
|
|
|
|
|
services.AddDbContext<ProfessorDbContext>(options =>
|
|
|
|
|
UseDatabase(options).CreateDbContext<ProfessorDbContext, Professor>(dbProvider));
|
|
|
|
|
services.AddDbContext<LessonDbContext>(options =>
|
|
|
|
|
UseDatabase(options).CreateDbContext<LessonDbContext, Lesson>(dbProvider));
|
|
|
|
|
services.AddDbContext<TypeOfOccupationDbContext>(options =>
|
|
|
|
|
UseDatabase(options).CreateDbContext<TypeOfOccupationDbContext, TypeOfOccupation>(dbProvider));
|
|
|
|
|
services.AddDbContext<SpecificWeekDbContext>(options =>
|
|
|
|
|
UseDatabase(options).CreateDbContext<SpecificWeekDbContext, SpecificWeek>(dbProvider));
|
|
|
|
|
|
2024-05-30 20:20:20 +03:00
|
|
|
|
services.AddDbContext<UberDbContext>(options =>
|
|
|
|
|
{
|
|
|
|
|
var providerNamespace = typeof(Mark).Namespace + "." + Enum.GetName(dbProvider);
|
2024-01-26 19:39:59 +03:00
|
|
|
|
|
2024-05-30 20:20:20 +03:00
|
|
|
|
var assembly = Assembly.GetExecutingAssembly();
|
|
|
|
|
var configurationTypes = assembly.GetTypes()
|
|
|
|
|
.Where(t =>
|
|
|
|
|
t is { IsNested: false, IsAbstract: false, Namespace: not null } &&
|
|
|
|
|
t.Namespace.StartsWith(providerNamespace) &&
|
|
|
|
|
t.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IEntityTypeConfiguration<>)));
|
|
|
|
|
|
|
|
|
|
var modelBuilder = new ModelBuilder();
|
|
|
|
|
|
|
|
|
|
foreach (var configurationType in configurationTypes)
|
|
|
|
|
{
|
|
|
|
|
var configurationInstance = Activator.CreateInstance(configurationType)!;
|
|
|
|
|
modelBuilder.ApplyConfiguration(configurationInstance);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var dbContext = (UberDbContext)Activator.CreateInstance(typeof(UberDbContext), (DbContextOptions<UberDbContext>)UseDatabase(options).Options)!;
|
|
|
|
|
dbContext.ApplyConfigurations(modelBuilder);
|
|
|
|
|
});
|
2024-01-26 19:39:59 +03:00
|
|
|
|
|
2024-05-30 20:07:20 +03:00
|
|
|
|
services.AddScoped<ICampusDbContext>(provider => provider.GetRequiredService<CampusDbContext>());
|
|
|
|
|
services.AddScoped<IDisciplineDbContext>(provider => provider.GetRequiredService<DisciplineDbContext>());
|
|
|
|
|
services.AddScoped<IFacultyDbContext>(provider => provider.GetRequiredService<FacultyDbContext>());
|
|
|
|
|
services.AddScoped<IGroupDbContext>(provider => provider.GetRequiredService<GroupDbContext>());
|
|
|
|
|
services.AddScoped<ILectureHallDbContext>(provider => provider.GetRequiredService<LectureHallDbContext>());
|
|
|
|
|
services.AddScoped<ILessonAssociationDbContext>(provider => provider.GetRequiredService<LessonAssociationDbContext>());
|
|
|
|
|
services.AddScoped<IProfessorDbContext>(provider => provider.GetRequiredService<ProfessorDbContext>());
|
|
|
|
|
services.AddScoped<ILessonDbContext>(provider => provider.GetRequiredService<LessonDbContext>());
|
|
|
|
|
services.AddScoped<ITypeOfOccupationDbContext>(provider => provider.GetRequiredService<TypeOfOccupationDbContext>());
|
|
|
|
|
services.AddScoped<ISpecificWeekDbContext>(provider => provider.GetRequiredService<SpecificWeekDbContext>());
|
2024-01-26 19:39:59 +03:00
|
|
|
|
|
|
|
|
|
return services;
|
2024-05-28 06:43:24 +03:00
|
|
|
|
|
2024-05-30 20:17:36 +03:00
|
|
|
|
DbContextOptionsBuilder UseDatabase(DbContextOptionsBuilder options)
|
|
|
|
|
{
|
|
|
|
|
return dbProvider switch
|
|
|
|
|
{
|
2024-06-01 05:40:48 +03:00
|
|
|
|
DatabaseProvider.Mysql => options.UseMySql(connection, ServerVersion.AutoDetect(connection),
|
|
|
|
|
x => x.MigrationsAssembly("MysqlMigrations")),
|
|
|
|
|
DatabaseProvider.Sqlite => options.UseSqlite(connection,
|
|
|
|
|
x => x.MigrationsAssembly("SqliteMigrations")),
|
|
|
|
|
DatabaseProvider.Postgresql => options.UseNpgsql(connection,
|
|
|
|
|
x => x.MigrationsAssembly("PsqlMigrations")),
|
2024-05-30 20:17:36 +03:00
|
|
|
|
_ => throw new ArgumentException("Unsupported database provider", Enum.GetName(dbProvider))
|
|
|
|
|
};
|
|
|
|
|
}
|
2024-01-26 19:39:59 +03:00
|
|
|
|
}
|
2024-10-25 02:35:36 +03:00
|
|
|
|
|
|
|
|
|
public static IHealthChecksBuilder AddDatabaseHealthCheck(this IHealthChecksBuilder healthChecksBuilder, DatabaseProvider dbProvider, string connection)
|
|
|
|
|
{
|
|
|
|
|
return dbProvider switch
|
|
|
|
|
{
|
|
|
|
|
DatabaseProvider.Mysql => healthChecksBuilder.AddMySql(connection, name: "MySql"),
|
|
|
|
|
DatabaseProvider.Sqlite => healthChecksBuilder.AddSqlite(connection, name: "Sqlite"),
|
|
|
|
|
DatabaseProvider.Postgresql => healthChecksBuilder.AddNpgSql(connection, name: "PostgreSQL"),
|
|
|
|
|
_ => throw new ArgumentException("Unsupported database provider", Enum.GetName(dbProvider))
|
|
|
|
|
};
|
|
|
|
|
}
|
2024-01-26 19:39:59 +03:00
|
|
|
|
}
|