| | 1 | | using Npgsql; |
| | 2 | | using Syki.Back.Audit; |
| | 3 | | using Audit.EntityFramework; |
| | 4 | | using Syki.Back.Database.Interceptors; |
| | 5 | | using Syki.Back.Features.Cross.CreateUser; |
| | 6 | | using Syki.Back.Features.Academic.CreateClass; |
| | 7 | | using Syki.Back.Features.Academic.CreateCourse; |
| | 8 | | using Syki.Back.Features.Academic.CallWebhooks; |
| | 9 | | using Syki.Back.Features.Academic.CreateCampus; |
| | 10 | | using Syki.Back.Features.Academic.CreateStudent; |
| | 11 | | using Syki.Back.Features.Academic.CreateTeacher; |
| | 12 | | using Microsoft.EntityFrameworkCore.Diagnostics; |
| | 13 | | using Syki.Back.Features.Cross.CreateInstitution; |
| | 14 | | using Syki.Back.Features.Cross.SetupFeatureFlags; |
| | 15 | | using Syki.Back.Features.Academic.CreateClassroom; |
| | 16 | | using Syki.Back.Features.Academic.CreateDiscipline; |
| | 17 | | using Syki.Back.Features.Academic.CreateNotification; |
| | 18 | | using Syki.Back.Features.Teacher.CreateClassActivity; |
| | 19 | | using Syki.Back.Features.Cross.SendResetPasswordToken; |
| | 20 | | using Syki.Back.Features.Academic.CreateCourseOffering; |
| | 21 | | using Syki.Back.Features.Academic.CreateAcademicPeriod; |
| | 22 | | using Microsoft.AspNetCore.Identity.EntityFrameworkCore; |
| | 23 | | using Syki.Back.Features.Teacher.CreateLessonAttendance; |
| | 24 | | using Syki.Back.Features.Cross.CreatePendingUserRegister; |
| | 25 | | using Syki.Back.Features.Academic.CreateCourseCurriculum; |
| | 26 | | using Syki.Back.Features.Academic.CreateEnrollmentPeriod; |
| | 27 | | using Syki.Back.Features.Student.CreateStudentEnrollment; |
| | 28 | | using Syki.Back.Features.Student.CreateClassActivityWork; |
| | 29 | | using Syki.Back.Features.Academic.CreateWebhookSubscription; |
| | 30 | |
|
| | 31 | | namespace Syki.Back.Database; |
| | 32 | |
|
| 13868 | 33 | | public class SykiDbContext(DbContextOptions<SykiDbContext> options, NpgsqlDataSource npgsqlDataSource) : IdentityDbConte |
| | 34 | | { |
| 14942 | 35 | | public DbSet<Institution> Institutions { get; set; } |
| 13870 | 36 | | public DbSet<InstitutionConfigs> Configs { get; set; } |
| | 37 | |
|
| 14404 | 38 | | public DbSet<Campus> Campi { get; set; } |
| 14970 | 39 | | public DbSet<Course> Courses { get; set; } |
| 14484 | 40 | | public DbSet<Discipline> Disciplines { get; set; } |
| 15018 | 41 | | public DbSet<CourseCurriculum> CourseCurriculums { get; set; } |
| 14404 | 42 | | public DbSet<CourseDiscipline> CoursesDisciplines { get; set; } |
| 13882 | 43 | | public DbSet<CourseCurriculumDiscipline> CourseCurriculumDisciplines { get; set; } |
| 15246 | 44 | | public DbSet<CourseOffering> CourseOfferings { get; set; } |
| 15774 | 45 | | public DbSet<AcademicPeriod> AcademicPeriods { get; set; } |
| 14464 | 46 | | public DbSet<EnrollmentPeriod> EnrollmentPeriods { get; set; } |
| | 47 | |
|
| 14312 | 48 | | public DbSet<Notification> Notifications { get; set; } |
| 14354 | 49 | | public DbSet<UserNotification> UserNotifications { get; set; } |
| 16360 | 50 | | public DbSet<UserRegister> UserRegisters { get; set; } |
| 15288 | 51 | | public DbSet<ResetPasswordToken> ResetPasswordTokens { get; set; } |
| | 52 | |
|
| 14188 | 53 | | public DbSet<WebhookSubscription> Webhooks { get; set; } |
| 13868 | 54 | | public DbSet<WebhookCall> WebhookCalls { get; set; } |
| | 55 | |
|
| 16096 | 56 | | public DbSet<Class> Classes { get; set; } |
| 13868 | 57 | | public DbSet<Classroom> Classrooms { get; set; } |
| 14488 | 58 | | public DbSet<SykiTeacher> Teachers { get; set; } |
| 13870 | 59 | | public DbSet<TeacherDiscipline> TeachersDisciplines { get; set; } |
| 14680 | 60 | | public DbSet<SykiStudent> Students { get; set; } |
| 14382 | 61 | | public DbSet<ClassStudent> ClassesStudents { get; set; } |
| 14124 | 62 | | public DbSet<ClassLesson> Lessons { get; set; } |
| 13964 | 63 | | public DbSet<ClassLessonAttendance> Attendances { get; set; } |
| 14156 | 64 | | public DbSet<ClassActivity> ClassActivities { get; set; } |
| 13938 | 65 | | public DbSet<ClassActivityWork> ClassActivityWorks { get; set; } |
| | 66 | |
|
| 13872 | 67 | | public DbSet<AuditLog> AuditLogs { get; set; } |
| 13872 | 68 | | public DbSet<FeatureFlags> FeatureFlags { get; set; } |
| 18786 | 69 | | public DbSet<DomainEvent> DomainEvents { get; set; } |
| 18576 | 70 | | public DbSet<Command> Commands { get; set; } |
| | 71 | |
|
| | 72 | | protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) |
| | 73 | | { |
| 13776 | 74 | | optionsBuilder.UseSnakeCaseNamingConvention(); |
| 13776 | 75 | | optionsBuilder.UseNpgsql( |
| 13776 | 76 | | npgsqlDataSource, |
| 13776 | 77 | | x => x.MigrationsHistoryTable("migrations", "syki") |
| 13776 | 78 | | ); |
| | 79 | |
|
| 27552 | 80 | | optionsBuilder.ConfigureWarnings(w => w.Ignore(RelationalEventId.PendingModelChangesWarning)); |
| | 81 | |
|
| 13776 | 82 | | optionsBuilder.AddInterceptors(new SetBatchSizeInterceptor()); |
| 13776 | 83 | | optionsBuilder.AddInterceptors(new AuditSaveChangesInterceptor()); |
| 13776 | 84 | | optionsBuilder.AddInterceptors(new SaveDomainEventsInterceptor()); |
| 13776 | 85 | | optionsBuilder.AddInterceptors(new CommandsDelayInterceptor()); |
| 13776 | 86 | | } |
| | 87 | |
|
| | 88 | | protected override void OnModelCreating(ModelBuilder builder) |
| | 89 | | { |
| 2 | 90 | | base.OnModelCreating(builder); |
| | 91 | |
|
| 2 | 92 | | builder.HasDefaultSchema("syki"); |
| | 93 | |
|
| 2 | 94 | | builder.ApplyConfigurationsFromAssembly(GetType().Assembly); |
| | 95 | |
|
| 200 | 96 | | foreach (var entity in builder.Model.GetEntityTypes()) |
| | 97 | | { |
| 98 | 98 | | if (entity.GetTableName().IsEmpty()) continue; |
| | 99 | |
|
| 98 | 100 | | entity.SetTableName(entity.GetTableName().ToSnakeCase().Replace("asp_net_", "")); |
| | 101 | |
|
| 464 | 102 | | foreach (var fk in entity.GetForeignKeys()) |
| | 103 | | { |
| 134 | 104 | | fk.SetConstraintName(fk.GetConstraintName().Replace("1", "")); |
| | 105 | | } |
| | 106 | |
|
| 424 | 107 | | foreach (var index in entity.GetIndexes()) |
| | 108 | | { |
| 114 | 109 | | index.SetDatabaseName(index.GetDatabaseName()?.ToSnakeCase()); |
| | 110 | | } |
| | 111 | | } |
| 2 | 112 | | } |
| | 113 | |
|
| | 114 | | protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder) |
| | 115 | | { |
| 2 | 116 | | configurationBuilder.Properties<Enum>().HaveConversion<string>(); |
| 2 | 117 | | configurationBuilder.Properties<CommandId>().HaveConversion<CommandId.CommandIdEfCoreValueConverter>(); |
| 2 | 118 | | configurationBuilder.Properties<DomainEventId>().HaveConversion<DomainEventId.DomainEventIdEfCoreValueConverter> |
| 2 | 119 | | configurationBuilder.Properties<CommandBatchId>().HaveConversion<CommandBatchId.CommandBatchIdEfCoreValueConvert |
| 2 | 120 | | } |
| | 121 | | } |