| | 1 | | using Syki.Back.Audit; |
| | 2 | | using Audit.EntityFramework; |
| | 3 | | using Syki.Back.Features.Cross.CreateUser; |
| | 4 | | using Syki.Back.Features.Academic.CreateClass; |
| | 5 | | using Syki.Back.Features.Academic.CreateCourse; |
| | 6 | | using Syki.Back.Features.Academic.CreateCampus; |
| | 7 | | using Syki.Back.Features.Academic.CreateStudent; |
| | 8 | | using Syki.Back.Features.Academic.CreateTeacher; |
| | 9 | | using Syki.Back.Features.Cross.CreateInstitution; |
| | 10 | | using Syki.Back.Features.Cross.SetupFeatureFlags; |
| | 11 | | using Syki.Back.Features.Academic.CreateDiscipline; |
| | 12 | | using Syki.Back.Features.Academic.CreateNotification; |
| | 13 | | using Syki.Back.Features.Cross.SendResetPasswordToken; |
| | 14 | | using Syki.Back.Features.Academic.CreateCourseOffering; |
| | 15 | | using Syki.Back.Features.Academic.CreateAcademicPeriod; |
| | 16 | | using Microsoft.AspNetCore.Identity.EntityFrameworkCore; |
| | 17 | | using Syki.Back.Features.Teacher.CreateLessonAttendance; |
| | 18 | | using Syki.Back.Features.Cross.CreatePendingUserRegister; |
| | 19 | | using Syki.Back.Features.Academic.CreateCourseCurriculum; |
| | 20 | | using Syki.Back.Features.Academic.CreateEnrollmentPeriod; |
| | 21 | | using Syki.Back.Features.Student.CreateStudentEnrollment; |
| | 22 | |
|
| | 23 | | namespace Syki.Back.Database; |
| | 24 | |
|
| 6199 | 25 | | public class SykiDbContext(DbContextOptions<SykiDbContext> options, DatabaseSettings settings) : IdentityDbContext<SykiU |
| | 26 | | { |
| 6423 | 27 | | public DbSet<Campus> Campi { get; set; } |
| 7134 | 28 | | public DbSet<Class> Classes { get; set; } |
| 6275 | 29 | | public DbSet<Lesson> Lessons { get; set; } |
| 8440 | 30 | | public DbSet<Course> Courses { get; set; } |
| 6204 | 31 | | public DbSet<SykiTask> Tasks { get; set; } |
| 6201 | 32 | | public DbSet<AuditLog> AuditLogs { get; set; } |
| 6400 | 33 | | public DbSet<SykiTeacher> Teachers { get; set; } |
| 6459 | 34 | | public DbSet<SykiStudent> Students { get; set; } |
| 6249 | 35 | | public DbSet<ExamGrade> ExamGrades { get; set; } |
| 6403 | 36 | | public DbSet<Discipline> Disciplines { get; set; } |
| 6713 | 37 | | public DbSet<Institution> Institutions { get; set; } |
| 6200 | 38 | | public DbSet<InstitutionConfigs> Configs { get; set; } |
| 6199 | 39 | | public DbSet<FeatureFlags> FeatureFlags { get; set; } |
| 7417 | 40 | | public DbSet<UserRegister> UserRegisters { get; set; } |
| 6381 | 41 | | public DbSet<Notification> Notifications { get; set; } |
| 6236 | 42 | | public DbSet<LessonAttendance> Attendances { get; set; } |
| 6266 | 43 | | public DbSet<ClassStudent> ClassesStudents { get; set; } |
| 6951 | 44 | | public DbSet<AcademicPeriod> AcademicPeriods { get; set; } |
| 6793 | 45 | | public DbSet<CourseOffering> CourseOfferings { get; set; } |
| 6676 | 46 | | public DbSet<CourseCurriculum> CourseCurriculums { get; set; } |
| 6453 | 47 | | public DbSet<EnrollmentPeriod> EnrollmentPeriods { get; set; } |
| 6403 | 48 | | public DbSet<UserNotification> UserNotifications { get; set; } |
| 6426 | 49 | | public DbSet<CourseDiscipline> CoursesDisciplines { get; set; } |
| 6663 | 50 | | public DbSet<ResetPasswordToken> ResetPasswordTokens { get; set; } |
| 6206 | 51 | | public DbSet<CourseCurriculumDiscipline> CourseCurriculumDisciplines { get; set; } |
| | 52 | |
|
| | 53 | | protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) |
| | 54 | | { |
| 5926 | 55 | | optionsBuilder.UseNpgsql(settings.ConnectionString); |
| 5926 | 56 | | optionsBuilder.UseSnakeCaseNamingConvention(); |
| 5926 | 57 | | optionsBuilder.AddInterceptors(new AuditSaveChangesInterceptor()); |
| 5926 | 58 | | } |
| | 59 | |
|
| | 60 | | protected override void OnModelCreating(ModelBuilder builder) |
| | 61 | | { |
| 2 | 62 | | base.OnModelCreating(builder); |
| | 63 | |
|
| 2 | 64 | | builder.HasDefaultSchema("syki"); |
| | 65 | |
|
| 2 | 66 | | builder.ApplyConfigurationsFromAssembly(GetType().Assembly); |
| | 67 | |
|
| 144 | 68 | | foreach (var entity in builder.Model.GetEntityTypes()) |
| | 69 | | { |
| 70 | 70 | | entity.SetTableName(entity.GetTableName()!.ToSnakeCase().Replace("asp_net_", "")); |
| | 71 | |
|
| 328 | 72 | | foreach (var fk in entity.GetForeignKeys()) |
| | 73 | | { |
| 94 | 74 | | fk.SetConstraintName(fk.GetConstraintName().Replace("1", "")); |
| | 75 | | } |
| | 76 | |
|
| 304 | 77 | | foreach (var index in entity.GetIndexes()) |
| | 78 | | { |
| 82 | 79 | | index.SetDatabaseName(index.GetDatabaseName()?.ToSnakeCase()); |
| | 80 | | } |
| | 81 | | } |
| 2 | 82 | | } |
| | 83 | |
|
| | 84 | | protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder) |
| | 85 | | { |
| 2 | 86 | | configurationBuilder.Properties<Enum>().HaveConversion<string>(); |
| 2 | 87 | | } |
| | 88 | |
|
| | 89 | | public async Task ResetDbAsync() |
| | 90 | | { |
| 1 | 91 | | if (Env.IsTesting()) |
| | 92 | | { |
| 1 | 93 | | await Database.EnsureDeletedAsync(); |
| 1 | 94 | | await Database.EnsureCreatedAsync(); |
| | 95 | | } |
| 1 | 96 | | } |
| | 97 | |
|
| | 98 | | public void ResetDb() |
| | 99 | | { |
| 0 | 100 | | if (Env.IsDevelopment()) |
| | 101 | | { |
| 0 | 102 | | Database.EnsureDeleted(); |
| 0 | 103 | | Database.EnsureCreated(); |
| | 104 | | } |
| 0 | 105 | | } |
| | 106 | |
|
| | 107 | | public void MigrateDb() |
| | 108 | | { |
| 0 | 109 | | if (!Env.IsTesting()) |
| | 110 | | { |
| 0 | 111 | | Database.Migrate(); |
| | 112 | | } |
| 0 | 113 | | } |
| | 114 | |
|
| | 115 | |
|
| | 116 | | public async Task<bool> AcademicPeriodExists(Guid institutionId, string id) |
| | 117 | | { |
| 515 | 118 | | return await AcademicPeriods.AnyAsync(p => p.InstitutionId == institutionId && p.Id == id); |
| 515 | 119 | | } |
| | 120 | | } |