| | | 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 | | |
| | 15052 | 34 | | public class SykiDbContext(DbContextOptions<SykiDbContext> options, NpgsqlDataSource npgsqlDataSource) : IdentityDbConte |
| | | 35 | | { |
| | 16204 | 36 | | public DbSet<Institution> Institutions { get; set; } |
| | 15054 | 37 | | public DbSet<InstitutionConfigs> Configs { get; set; } |
| | | 38 | | |
| | 16436 | 39 | | public DbSet<Campus> Campi { get; set; } |
| | 16178 | 40 | | public DbSet<Course> Courses { get; set; } |
| | 15884 | 41 | | public DbSet<Discipline> Disciplines { get; set; } |
| | 16222 | 42 | | public DbSet<CourseCurriculum> CourseCurriculums { get; set; } |
| | 15600 | 43 | | public DbSet<CourseDiscipline> CoursesDisciplines { get; set; } |
| | 15066 | 44 | | public DbSet<CourseCurriculumDiscipline> CourseCurriculumDisciplines { get; set; } |
| | 15942 | 45 | | public DbSet<CourseOffering> CourseOfferings { get; set; } |
| | 16978 | 46 | | public DbSet<AcademicPeriod> AcademicPeriods { get; set; } |
| | 15648 | 47 | | public DbSet<EnrollmentPeriod> EnrollmentPeriods { get; set; } |
| | | 48 | | |
| | 15510 | 49 | | public DbSet<Notification> Notifications { get; set; } |
| | 15552 | 50 | | public DbSet<UserNotification> UserNotifications { get; set; } |
| | 17800 | 51 | | public DbSet<UserRegister> UserRegisters { get; set; } |
| | 16472 | 52 | | public DbSet<ResetPasswordToken> ResetPasswordTokens { get; set; } |
| | | 53 | | |
| | 15374 | 54 | | public DbSet<WebhookSubscription> Webhooks { get; set; } |
| | 15052 | 55 | | public DbSet<WebhookCall> WebhookCalls { get; set; } |
| | | 56 | | |
| | 17288 | 57 | | public DbSet<Class> Classes { get; set; } |
| | 15066 | 58 | | public DbSet<Classroom> Classrooms { get; set; } |
| | 15064 | 59 | | public DbSet<ClassroomClass> ClassroomsClasses { get; set; } |
| | 15060 | 60 | | public DbSet<Schedule> Schedules { get; set; } |
| | 16092 | 61 | | public DbSet<SykiTeacher> Teachers { get; set; } |
| | 15658 | 62 | | public DbSet<TeacherDiscipline> TeachersDisciplines { get; set; } |
| | 15670 | 63 | | public DbSet<TeacherCampus> TeachersCampi { get; set; } |
| | 15878 | 64 | | public DbSet<SykiStudent> Students { get; set; } |
| | 15566 | 65 | | public DbSet<ClassStudent> ClassesStudents { get; set; } |
| | 15308 | 66 | | public DbSet<ClassLesson> Lessons { get; set; } |
| | 15148 | 67 | | public DbSet<ClassLessonAttendance> Attendances { get; set; } |
| | 15340 | 68 | | public DbSet<ClassActivity> ClassActivities { get; set; } |
| | 15122 | 69 | | public DbSet<ClassActivityWork> ClassActivityWorks { get; set; } |
| | | 70 | | |
| | 15056 | 71 | | public DbSet<AuditLog> AuditLogs { get; set; } |
| | 15056 | 72 | | public DbSet<FeatureFlags> FeatureFlags { get; set; } |
| | 20266 | 73 | | public DbSet<DomainEvent> DomainEvents { get; set; } |
| | 20040 | 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 | | { |
| | 14910 | 81 | | optionsBuilder.UseSnakeCaseNamingConvention(); |
| | 14910 | 82 | | optionsBuilder.UseNpgsql( |
| | 14910 | 83 | | npgsqlDataSource, |
| | 14910 | 84 | | x => x.MigrationsHistoryTable("migrations", "syki") |
| | 14910 | 85 | | ); |
| | | 86 | | |
| | 29820 | 87 | | optionsBuilder.ConfigureWarnings(w => w.Ignore(RelationalEventId.PendingModelChangesWarning)); |
| | | 88 | | |
| | 14910 | 89 | | optionsBuilder.AddInterceptors(new SetBatchSizeInterceptor()); |
| | 14910 | 90 | | optionsBuilder.AddInterceptors(new AuditSaveChangesInterceptor()); |
| | 14910 | 91 | | optionsBuilder.AddInterceptors(new SaveDomainEventsInterceptor()); |
| | 14910 | 92 | | optionsBuilder.AddInterceptors(new CommandsDelayInterceptor()); |
| | 14910 | 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 | | } |