| | 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 | |
|
| | 7 | | namespace Syki.Back.Database; |
| | 8 | |
|
| | 9 | | public static class DbContextExtensions |
| | 10 | | { |
| | 11 | | public static void ResetDevDb(this SykiDbContext ctx) |
| | 12 | | { |
| 0 | 13 | | if (!Env.IsDevelopment()) return; |
| | 14 | |
|
| 0 | 15 | | ctx.Database.EnsureDeleted(); |
| 0 | 16 | | ctx.Database.Migrate(); |
| 0 | 17 | | } |
| | 18 | |
|
| | 19 | | public static async Task ResetTestDbAsync(this SykiDbContext ctx) |
| | 20 | | { |
| 2 | 21 | | if (!Env.IsTesting()) return; |
| | 22 | |
|
| 2 | 23 | | await ctx.Database.EnsureDeletedAsync(); |
| 2 | 24 | | await ctx.Database.MigrateAsync(); |
| 2 | 25 | | } |
| | 26 | |
|
| | 27 | | public static bool HasMissingMigration(this SykiDbContext context) |
| | 28 | | { |
| 2 | 29 | | var modelDiffer = context.GetService<IMigrationsModelDiffer>(); |
| | 30 | |
|
| 2 | 31 | | var migrationsAssembly = context.GetService<IMigrationsAssembly>(); |
| 2 | 32 | | var modelInitializer = context.GetService<IModelRuntimeInitializer>(); |
| | 33 | |
|
| 2 | 34 | | var snapshotModel = migrationsAssembly.ModelSnapshot?.Model; |
| 2 | 35 | | if (snapshotModel is IMutableModel mutableModel) |
| 2 | 36 | | snapshotModel = mutableModel.FinalizeModel(); |
| 2 | 37 | | if (snapshotModel is not null) |
| 2 | 38 | | snapshotModel = modelInitializer.Initialize(snapshotModel); |
| | 39 | |
|
| 2 | 40 | | var designTimeModel = context.GetService<IDesignTimeModel>(); |
| | 41 | |
|
| 2 | 42 | | return modelDiffer.HasDifferences( |
| 2 | 43 | | snapshotModel?.GetRelationalModel(), |
| 2 | 44 | | designTimeModel.Model.GetRelationalModel()); |
| | 45 | | } |
| | 46 | |
|
| | 47 | | public static async Task<bool> AcademicPeriodExists(this SykiDbContext ctx, Guid institutionId, string id) |
| | 48 | | { |
| 1352 | 49 | | return await ctx.AcademicPeriods.AnyAsync(p => p.InstitutionId == institutionId && p.Id == id); |
| 1352 | 50 | | } |
| | 51 | |
|
| | 52 | | public static async Task<List<Course>> GetCourses(this SykiDbContext ctx, Guid institutionId) |
| | 53 | | { |
| 4 | 54 | | return await ctx.Courses.AsNoTracking() |
| 4 | 55 | | .Where(c => c.InstitutionId == institutionId) |
| 4 | 56 | | .OrderBy(c => c.Name) |
| 4 | 57 | | .ToListAsync(); |
| 4 | 58 | | } |
| | 59 | |
|
| | 60 | | public static Command AddCommand( |
| | 61 | | this DbContext ctx, |
| | 62 | | Guid institutionId, |
| | 63 | | ICommand command, |
| | 64 | | DomainEventId? eventId = null, |
| | 65 | | CommandId? parentId = null, |
| | 66 | | CommandId? originalId = null, |
| | 67 | | CommandBatchId? batchId = null, |
| | 68 | | int? delaySeconds = null |
| | 69 | | ) { |
| 2688 | 70 | | var activityId = Activity.Current?.Id; |
| | 71 | |
|
| 2688 | 72 | | return ctx.Add( |
| 2688 | 73 | | new Command( |
| 2688 | 74 | | institutionId, |
| 2688 | 75 | | command, |
| 2688 | 76 | | eventId: eventId, |
| 2688 | 77 | | parentId: parentId, |
| 2688 | 78 | | originalId: originalId, |
| 2688 | 79 | | batchId: batchId, |
| 2688 | 80 | | delaySeconds: delaySeconds, |
| 2688 | 81 | | activityId: activityId |
| 2688 | 82 | | ) |
| 2688 | 83 | | ).Entity; |
| | 84 | | } |
| | 85 | |
|
| | 86 | | public static async Task<int> SaveChangesAsync<TEntity>(this SykiDbContext ctx, TEntity entity) |
| | 87 | | { |
| 888 | 88 | | ctx.Add(entity); |
| 888 | 89 | | return await ctx.SaveChangesAsync(); |
| 888 | 90 | | } |
| | 91 | | } |