| | 1 | | using System.Diagnostics; |
| | 2 | | using Microsoft.EntityFrameworkCore.Metadata; |
| | 3 | | using Syki.Back.Features.Academic.CreateCourse; |
| | 4 | | using Microsoft.EntityFrameworkCore.Migrations; |
| | 5 | | using Microsoft.EntityFrameworkCore.Infrastructure; |
| | 6 | | using Syki.Back.Features.Academic.CreateAcademicPeriod; |
| | 7 | |
|
| | 8 | | namespace Syki.Back.Database; |
| | 9 | |
|
| | 10 | | public static class DbContextExtensions |
| | 11 | | { |
| | 12 | | public static async Task<bool> CampusNotFound(this SykiDbContext ctx, Guid id) |
| | 13 | | { |
| 524 | 14 | | return !await ctx.Campi.AnyAsync(p => p.InstitutionId == ctx.InstitutionId && p.Id == id); |
| 524 | 15 | | } |
| | 16 | |
|
| | 17 | | public static async Task<bool> CourseNotFound(this SykiDbContext ctx, Guid id) |
| | 18 | | { |
| 520 | 19 | | return !await ctx.Courses.AnyAsync(p => p.InstitutionId == ctx.InstitutionId && p.Id == id); |
| 520 | 20 | | } |
| | 21 | |
|
| | 22 | | public static async Task<bool> CourseCurriculumNotFound(this SykiDbContext ctx, Guid id, Guid courseId) |
| | 23 | | { |
| 516 | 24 | | return !await ctx.CourseCurriculums.AnyAsync(g => g.InstitutionId == ctx.InstitutionId && g.Id == id && g.Course |
| 516 | 25 | | } |
| | 26 | |
|
| | 27 | | public static async Task<bool> AcademicPeriodNotFound(this SykiDbContext ctx, string id) |
| | 28 | | { |
| 1358 | 29 | | return !await ctx.AcademicPeriods.AnyAsync(p => p.InstitutionId == ctx.InstitutionId && p.Id == id); |
| 1358 | 30 | | } |
| | 31 | |
|
| | 32 | |
|
| | 33 | |
|
| | 34 | |
|
| | 35 | |
|
| | 36 | |
|
| | 37 | | public static async Task<AcademicPeriod?> GetCurrentAcademicPeriod(this SykiDbContext ctx, Guid institutionId) |
| | 38 | | { |
| 0 | 39 | | var today = DateOnly.FromDateTime(DateTime.UtcNow); |
| 0 | 40 | | return await ctx.AcademicPeriods |
| 0 | 41 | | .Where(p => p.InstitutionId == institutionId && p.StartAt <= today && p.EndAt >= today) |
| 0 | 42 | | .FirstOrDefaultAsync(); |
| 0 | 43 | | } |
| | 44 | |
|
| | 45 | | public static async Task<List<Course>> GetCourses(this SykiDbContext ctx, Guid institutionId) |
| | 46 | | { |
| 4 | 47 | | return await ctx.Courses.AsNoTracking() |
| 4 | 48 | | .Where(c => c.InstitutionId == institutionId) |
| 4 | 49 | | .OrderBy(c => c.Name) |
| 4 | 50 | | .ToListAsync(); |
| 4 | 51 | | } |
| | 52 | |
|
| | 53 | | public static Command AddCommand( |
| | 54 | | this DbContext ctx, |
| | 55 | | Guid institutionId, |
| | 56 | | ICommand command, |
| | 57 | | DomainEventId? eventId = null, |
| | 58 | | CommandId? parentId = null, |
| | 59 | | CommandId? originalId = null, |
| | 60 | | CommandBatchId? batchId = null, |
| | 61 | | int? delaySeconds = null |
| | 62 | | ) |
| | 63 | | { |
| 2830 | 64 | | var activityId = Activity.Current?.Id; |
| | 65 | |
|
| 2830 | 66 | | return ctx.Add( |
| 2830 | 67 | | new Command( |
| 2830 | 68 | | institutionId, |
| 2830 | 69 | | command, |
| 2830 | 70 | | eventId: eventId, |
| 2830 | 71 | | parentId: parentId, |
| 2830 | 72 | | originalId: originalId, |
| 2830 | 73 | | batchId: batchId, |
| 2830 | 74 | | delaySeconds: delaySeconds, |
| 2830 | 75 | | activityId: activityId |
| 2830 | 76 | | ) |
| 2830 | 77 | | ).Entity; |
| | 78 | | } |
| | 79 | |
|
| | 80 | | public static async Task<int> SaveChangesAsync<TEntity>(this SykiDbContext ctx, TEntity entity) |
| | 81 | | { |
| 1486 | 82 | | ctx.Add(entity); |
| 1486 | 83 | | return await ctx.SaveChangesAsync(); |
| 1486 | 84 | | } |
| | 85 | |
|
| | 86 | | public static void ResetDevDb(this SykiDbContext ctx) |
| | 87 | | { |
| 0 | 88 | | if (!Env.IsDevelopment()) return; |
| | 89 | |
|
| 0 | 90 | | ctx.Database.EnsureDeleted(); |
| 0 | 91 | | ctx.Database.Migrate(); |
| 0 | 92 | | } |
| | 93 | |
|
| | 94 | | public static async Task ResetTestDbAsync(this SykiDbContext ctx) |
| | 95 | | { |
| 2 | 96 | | if (!Env.IsTesting()) return; |
| | 97 | |
|
| 2 | 98 | | await ctx.Database.EnsureDeletedAsync(); |
| 2 | 99 | | await ctx.Database.MigrateAsync(); |
| 2 | 100 | | } |
| | 101 | |
|
| | 102 | | public static bool HasMissingMigration(this SykiDbContext context) |
| | 103 | | { |
| 2 | 104 | | var modelDiffer = context.GetService<IMigrationsModelDiffer>(); |
| | 105 | |
|
| 2 | 106 | | var migrationsAssembly = context.GetService<IMigrationsAssembly>(); |
| 2 | 107 | | var modelInitializer = context.GetService<IModelRuntimeInitializer>(); |
| | 108 | |
|
| 2 | 109 | | var snapshotModel = migrationsAssembly.ModelSnapshot?.Model; |
| 2 | 110 | | if (snapshotModel is IMutableModel mutableModel) |
| 2 | 111 | | snapshotModel = mutableModel.FinalizeModel(); |
| 2 | 112 | | if (snapshotModel is not null) |
| 2 | 113 | | snapshotModel = modelInitializer.Initialize(snapshotModel); |
| | 114 | |
|
| 2 | 115 | | var designTimeModel = context.GetService<IDesignTimeModel>(); |
| | 116 | |
|
| 2 | 117 | | return modelDiffer.HasDifferences( |
| 2 | 118 | | snapshotModel?.GetRelationalModel(), |
| 2 | 119 | | designTimeModel.Model.GetRelationalModel()); |
| | 120 | | } |
| | 121 | | } |