using GermanApp.Domain.Entities;
using GermanApp.Infrastructure.Data.DbContext;
using Microsoft.EntityFrameworkCore;
namespace GermanApp.Infrastructure.Data.SeedData;
///
/// Extension methods for seeding the database.
///
public static class SeedDataExtension
{
///
/// Seeds the database with initial data.
///
/// The web application
public static void SeedDatabase(this WebApplication app)
{
using var scope = app.Services.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService();
// Apply pending migrations
dbContext.Database.Migrate();
// Only seed if there are no users
if (!dbContext.Users.Any())
{
// Seed an admin user
var adminUser = User.Create(
"admin",
"admin@deutschlernen.com",
"$2a$11$N9qo8uLOickgx2ZMRZoMy.Mrq9Hq4yVJyX2sX7z3J0J9z6J9Z2K4a" // bcrypt hash of "Admin@123!"
);
adminUser.UpdateLevel("C1");
dbContext.Users.Add(adminUser);
// Seed a test user
var testUser = User.Create(
"testuser",
"test@deutschlernen.com",
"$2a$11$N9qo8uLOickgx2ZMRZoMy.Mrq9Hq4yVJyX2sX7z3J0J9z6J9Z2K4a" // bcrypt hash of "Test@123!"
);
dbContext.Users.Add(testUser);
// Seed some lessons
var lessons = new[]
{
Lesson.Create("Greetings", "Basic German greetings and introductions", 1),
Lesson.Create("Numbers", "German numbers 1-100", 1),
Lesson.Create("Grammar Basics", "Basic German grammar rules", 2),
Lesson.Create("Everyday Phrases", "Common phrases for daily conversations", 2),
Lesson.Create("Advanced Grammar", "Complex German grammar", 4)
};
dbContext.Lessons.AddRange(lessons);
dbContext.SaveChanges();
Console.WriteLine("Database seeded with admin user, test user, and sample lessons.");
}
}
}