- Updated Lesson entity to use LevelId (int) instead of Level (int) - Added navigation property Level (Level entity) - Added Order, Topic, and IsActive properties to Lesson - Updated Lesson.Create() factory method signature to accept levelId, title, order, topic, description - Split Update method into individual property update methods - Updated LessonDto to use LevelId, LevelName, LevelCode instead of Level int - Added CreateLessonDto and UpdateLessonDto with all required fields - Added UpdateFromDto extension method for Lesson - Updated CreateLessonCommand to validate LevelId instead of Level - Updated SeedDataExtension to seed Level entities first, then use new Lesson.Create() signature - Made SeedDatabaseAsync async and updated Program.cs to await it - Updated LessonsEndpoints to use GetByLevelAsync for beginner/advanced queries - Fixed missing using directive for Domain.Entities Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
72 lines
2.7 KiB
C#
72 lines
2.7 KiB
C#
using GermanApp.Domain.Entities;
|
|
using GermanApp.Infrastructure.Data.DbContext;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace GermanApp.Infrastructure.Data.SeedData;
|
|
|
|
/// <summary>
|
|
/// Extension methods for seeding the database.
|
|
/// </summary>
|
|
public static class SeedDataExtension
|
|
{
|
|
/// <summary>
|
|
/// Seeds the database with initial data.
|
|
/// </summary>
|
|
/// <param name="app">The web application</param>
|
|
public static async Task SeedDatabaseAsync(this WebApplication app)
|
|
{
|
|
using var scope = app.Services.CreateScope();
|
|
var dbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
|
|
|
// Apply pending migrations
|
|
dbContext.Database.Migrate();
|
|
|
|
// Only seed if there are no users
|
|
if (!dbContext.Users.Any())
|
|
{
|
|
// Seed CEFR levels first
|
|
var levels = new[]
|
|
{
|
|
Level.Create("Beginner A1", "A1", 1),
|
|
Level.Create("Elementary A2", "A2", 2),
|
|
Level.Create("Intermediate B1", "B1", 3),
|
|
Level.Create("Upper Intermediate B2", "B2", 4),
|
|
Level.Create("Advanced C1", "C1", 5)
|
|
};
|
|
dbContext.Levels.AddRange(levels);
|
|
await dbContext.SaveChangesAsync();
|
|
|
|
// 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(1, "Greetings", 1, "Greetings", "Basic German greetings and introductions"),
|
|
Lesson.Create(1, "Numbers", 2, "Numbers", "German numbers 1-100"),
|
|
Lesson.Create(2, "Grammar Basics", 1, "Grammar", "Basic German grammar rules"),
|
|
Lesson.Create(2, "Everyday Phrases", 2, "Phrases", "Common phrases for daily conversations"),
|
|
Lesson.Create(5, "Advanced Grammar", 1, "Grammar", "Complex German grammar")
|
|
};
|
|
dbContext.Lessons.AddRange(lessons);
|
|
|
|
await dbContext.SaveChangesAsync();
|
|
|
|
Console.WriteLine("Database seeded with levels, admin user, test user, and sample lessons.");
|
|
}
|
|
}
|
|
}
|