Skip to content

Commit

Permalink
feat: existing NoteIds will be added to connected Node when seedi…
Browse files Browse the repository at this point in the history
…ng database
  • Loading branch information
HunorTotBagi committed Feb 12, 2025
1 parent 0776266 commit a3726ea
Showing 1 changed file with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace Notes.Infrastructure.Data.Extensions;
using BuildingBlocks.Application.Data;
using Notes.Application.Data;

namespace Notes.Infrastructure.Data.Extensions;

public static class DatabaseExtensions
{
Expand All @@ -7,21 +10,25 @@ public static async Task MigrateAndSeedNotesDatabaseAsync(this IServiceProvider
using var scope = services.CreateScope();
var scopedProvider = scope.ServiceProvider;

var context = scopedProvider.GetRequiredService<NotesDbContext>();
var notesDbContext = scopedProvider.GetRequiredService<NotesDbContext>();
var nodesService = scopedProvider.GetRequiredService<INodesService>();

// Apply migrations
await context.Database.MigrateAsync();
await notesDbContext.Database.MigrateAsync();

// Seed initial data if necessary
await SeedAsync(context);
await SeedAsync(notesDbContext, nodesService);
}

private static async Task SeedAsync(NotesDbContext context)
private static async Task SeedAsync(NotesDbContext context, INodesService nodesService)
{
if (await context.Notes.AnyAsync())
return;

await context.AddRangeAsync(InitialData.Notes);
await context.SaveChangesAsync();

foreach (var initialNote in InitialData.Notes)
await nodesService.AddNote(initialNote.NodeId, initialNote.Id, CancellationToken.None);
}
}

0 comments on commit a3726ea

Please sign in to comment.