- Create User entity with gamification fields (Level, Streak, Points) - Add User DbSet to AppDbContext with proper configuration - Create initial database migration with Users and Lessons tables - Add SeedData extension for initial admin user and sample lessons - Update Program.cs to use seed data method Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
60 lines
2.1 KiB
C#
60 lines
2.1 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 void SeedDatabase(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 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.");
|
|
}
|
|
}
|
|
}
|