| | 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.AssignClassToClassroom; |
| | 30 | | using Syki.Back.Features.Academic.CreateWebhookSubscription; |
| | 31 | |
|
| | 32 | | namespace Syki.Back.Database; |
| | 33 | |
|
| 15046 | 34 | | public class SykiDbContext(DbContextOptions<SykiDbContext> options, NpgsqlDataSource npgsqlDataSource) : IdentityDbConte |
| | 35 | | { |
| 16198 | 36 | | public DbSet<Institution> Institutions { get; set; } |
| 15048 | 37 | | public DbSet<InstitutionConfigs> Configs { get; set; } |
| | 38 | |
|
| 16430 | 39 | | public DbSet<Campus> Campi { get; set; } |
| 16172 | 40 | | public DbSet<Course> Courses { get; set; } |
| 15878 | 41 | | public DbSet<Discipline> Disciplines { get; set; } |
| 16216 | 42 | | public DbSet<CourseCurriculum> CourseCurriculums { get; set; } |
| 15594 | 43 | | public DbSet<CourseDiscipline> CoursesDisciplines { get; set; } |
| 15060 | 44 | | public DbSet<CourseCurriculumDiscipline> CourseCurriculumDisciplines { get; set; } |
| 15936 | 45 | | public DbSet<CourseOffering> CourseOfferings { get; set; } |
| 16972 | 46 | | public DbSet<AcademicPeriod> AcademicPeriods { get; set; } |
| 15642 | 47 | | public DbSet<EnrollmentPeriod> EnrollmentPeriods { get; set; } |
| | 48 | |
|
| 15504 | 49 | | public DbSet<Notification> Notifications { get; set; } |
| 15546 | 50 | | public DbSet<UserNotification> UserNotifications { get; set; } |
| 17794 | 51 | | public DbSet<UserRegister> UserRegisters { get; set; } |
| 16466 | 52 | | public DbSet<ResetPasswordToken> ResetPasswordTokens { get; set; } |
| | 53 | |
|
| 15368 | 54 | | public DbSet<WebhookSubscription> Webhooks { get; set; } |
| 15046 | 55 | | public DbSet<WebhookCall> WebhookCalls { get; set; } |
| | 56 | |
|
| 17282 | 57 | | public DbSet<Class> Classes { get; set; } |
| 15060 | 58 | | public DbSet<Classroom> Classrooms { get; set; } |
| 15058 | 59 | | public DbSet<ClassroomClass> ClassroomsClasses { get; set; } |
| 15054 | 60 | | public DbSet<Schedule> Schedules { get; set; } |
| 16086 | 61 | | public DbSet<SykiTeacher> Teachers { get; set; } |
| 15652 | 62 | | public DbSet<TeacherDiscipline> TeachersDisciplines { get; set; } |
| 15664 | 63 | | public DbSet<TeacherCampus> TeachersCampi { get; set; } |
| 15872 | 64 | | public DbSet<SykiStudent> Students { get; set; } |
| 15560 | 65 | | public DbSet<ClassStudent> ClassesStudents { get; set; } |
| 15302 | 66 | | public DbSet<ClassLesson> Lessons { get; set; } |
| 15142 | 67 | | public DbSet<ClassLessonAttendance> Attendances { get; set; } |
| 15334 | 68 | | public DbSet<ClassActivity> ClassActivities { get; set; } |
| 15116 | 69 | | public DbSet<ClassActivityWork> ClassActivityWorks { get; set; } |
| | 70 | |
|
| 15050 | 71 | | public DbSet<AuditLog> AuditLogs { get; set; } |
| 15050 | 72 | | public DbSet<FeatureFlags> FeatureFlags { get; set; } |
| 20254 | 73 | | public DbSet<DomainEvent> DomainEvents { get; set; } |
| 20000 | 74 | | public DbSet<Command> Commands { get; set; } |
| | 75 | |
|
| 5604 | 76 | | public Guid UserId { get; set; } |
| 15516 | 77 | | public Guid InstitutionId { get; set; } |
| | 78 | |
|
| | 79 | | protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) |
| | 80 | | { |
| 14904 | 81 | | optionsBuilder.UseSnakeCaseNamingConvention(); |
| 14904 | 82 | | optionsBuilder.UseNpgsql( |
| 14904 | 83 | | npgsqlDataSource, |
| 14904 | 84 | | x => x.MigrationsHistoryTable("migrations", "syki") |
| 14904 | 85 | | ); |
| | 86 | |
|
| 29808 | 87 | | optionsBuilder.ConfigureWarnings(w => w.Ignore(RelationalEventId.PendingModelChangesWarning)); |
| | 88 | |
|
| 14904 | 89 | | optionsBuilder.AddInterceptors(new SetBatchSizeInterceptor()); |
| 14904 | 90 | | optionsBuilder.AddInterceptors(new AuditSaveChangesInterceptor()); |
| 14904 | 91 | | optionsBuilder.AddInterceptors(new SaveDomainEventsInterceptor()); |
| 14904 | 92 | | optionsBuilder.AddInterceptors(new CommandsDelayInterceptor()); |
| 14904 | 93 | | } |
| | 94 | |
|
| | 95 | | protected override void OnModelCreating(ModelBuilder builder) |
| | 96 | | { |
| 2 | 97 | | base.OnModelCreating(builder); |
| | 98 | |
|
| 2 | 99 | | builder.HasDefaultSchema("syki"); |
| | 100 | |
|
| 2 | 101 | | builder.ApplyConfigurationsFromAssembly(GetType().Assembly); |
| | 102 | |
|
| 200 | 103 | | foreach (var entity in builder.Model.GetEntityTypes()) |
| | 104 | | { |
| 98 | 105 | | if (entity.GetTableName().IsEmpty()) continue; |
| | 106 | |
|
| 98 | 107 | | entity.SetTableName(entity.GetTableName().ToSnakeCase().Replace("asp_net_", "")); |
| | 108 | |
|
| 468 | 109 | | foreach (var fk in entity.GetForeignKeys()) |
| | 110 | | { |
| 136 | 111 | | fk.SetConstraintName(fk.GetConstraintName().Replace("1", "")); |
| | 112 | | } |
| | 113 | |
|
| 428 | 114 | | foreach (var index in entity.GetIndexes()) |
| | 115 | | { |
| 116 | 116 | | index.SetDatabaseName(index.GetDatabaseName()?.ToSnakeCase()); |
| | 117 | | } |
| | 118 | | } |
| 2 | 119 | | } |
| | 120 | |
|
| | 121 | | protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder) |
| | 122 | | { |
| 2 | 123 | | configurationBuilder.Properties<Enum>().HaveConversion<string>(); |
| 2 | 124 | | configurationBuilder.Properties<CommandId>().HaveConversion<CommandId.CommandIdEfCoreValueConverter>(); |
| 2 | 125 | | configurationBuilder.Properties<DomainEventId>().HaveConversion<DomainEventId.DomainEventIdEfCoreValueConverter> |
| 2 | 126 | | configurationBuilder.Properties<CommandBatchId>().HaveConversion<CommandBatchId.CommandBatchIdEfCoreValueConvert |
| 2 | 127 | | } |
| | 128 | | } |