| | 1 | | using Microsoft.EntityFrameworkCore; |
| | 2 | | using Backend.Model; |
| | 3 | |
|
| | 4 | | namespace Backend |
| | 5 | | { |
| | 6 | | public class PlannerContext : DbContext |
| | 7 | | { |
| | 8 | | public PlannerContext(DbContextOptions<PlannerContext> options, IConfiguration configuration, ILogger<PlannerCon |
| 45 | 9 | | : base(options) |
| | 10 | | { |
| 45 | 11 | | _configuration = configuration; |
| 45 | 12 | | _logger = logger; |
| 45 | 13 | | _logger.LogInformation("PlannerContext created with options: {Options}", options); |
| 45 | 14 | | _logger.LogInformation("Using connection string: {ConnectionString}", _configuration.GetConnectionString("De |
| 45 | 15 | | Database.EnsureCreated(); |
| 45 | 16 | | _logger.LogInformation("PlannerContext initialized with connection string: {ConnectionString}", _configurati |
| 45 | 17 | | } |
| | 18 | |
|
| | 19 | | private readonly IConfiguration _configuration; |
| | 20 | | private readonly ILogger<PlannerContext> _logger; |
| | 21 | |
|
| 80 | 22 | | public DbSet<User> Users { get; set; } = null!; |
| 47 | 23 | | public DbSet<Category> Categories { get; set; } = null!; |
| 46 | 24 | | public DbSet<Ingredient> Ingredients { get; set; } = null!; |
| 46 | 25 | | public DbSet<PantryItem> PantryItems { get; set; } = null!; |
| 46 | 26 | | public DbSet<Recipe> Recipes { get; set; } = null!; |
| 46 | 27 | | public DbSet<RecipeIngredient> RecipeIngredients { get; set; } = null!; |
| 46 | 28 | | public DbSet<MealPlan> MealPlans { get; set; } = null!; |
| 46 | 29 | | public DbSet<MealPlanEntry> MealPlanEntries { get; set; } = null!; |
| 53 | 30 | | public DbSet<RefreshToken> RefreshTokens { get; set; } = null!; |
| | 31 | |
|
| | 32 | | protected override void OnModelCreating(ModelBuilder modelBuilder) |
| | 33 | | { |
| 10 | 34 | | base.OnModelCreating(modelBuilder); |
| | 35 | |
|
| | 36 | | // Relationships |
| 10 | 37 | | SetUpIngredientRelationships(modelBuilder); |
| 10 | 38 | | SetUpPantryItemRelationships(modelBuilder); |
| 10 | 39 | | SetUpRecipeRelationships(modelBuilder); |
| 10 | 40 | | SetUpRecipeIngredientRelationships(modelBuilder); |
| 10 | 41 | | SetUpMealPlanRelationships(modelBuilder); |
| 10 | 42 | | SetUpMealPlanEntryRelationships(modelBuilder); |
| 10 | 43 | | SeedCategories(modelBuilder); |
| | 44 | |
|
| 10 | 45 | | _logger.LogInformation("Model creating completed with configured relationships and seeded categories."); |
| 10 | 46 | | } |
| | 47 | |
|
| | 48 | | private void SeedCategories(ModelBuilder modelBuilder) |
| | 49 | | { |
| 10 | 50 | | var categories = _configuration.GetSection("Categories").Get<Category[]>(); |
| 10 | 51 | | if (categories != null && categories.Length > 0) |
| 2 | 52 | | modelBuilder.Entity<Category>().HasData(categories); |
| | 53 | |
|
| 10 | 54 | | } |
| | 55 | |
|
| | 56 | | private static void SetUpMealPlanEntryRelationships(ModelBuilder modelBuilder) |
| | 57 | | { |
| 10 | 58 | | modelBuilder.Entity<MealPlanEntry>() |
| 10 | 59 | | .HasOne(mpe => mpe.MealPlan) |
| 10 | 60 | | .WithMany(mp => mp.MealPlanEntries) |
| 10 | 61 | | .HasForeignKey(mpe => mpe.MealPlanId); |
| | 62 | |
|
| 10 | 63 | | modelBuilder.Entity<MealPlanEntry>() |
| 10 | 64 | | .HasOne(mpe => mpe.Recipe) |
| 10 | 65 | | .WithMany(r => r.MealPlanEntries) |
| 10 | 66 | | .HasForeignKey(mpe => mpe.RecipeId); |
| 10 | 67 | | } |
| | 68 | |
|
| | 69 | | private static void SetUpMealPlanRelationships(ModelBuilder modelBuilder) |
| | 70 | | { |
| 10 | 71 | | modelBuilder.Entity<MealPlan>() |
| 10 | 72 | | .HasOne(mp => mp.User) |
| 10 | 73 | | .WithMany(u => u.MealPlans) |
| 10 | 74 | | .HasForeignKey(mp => mp.UserId); |
| 10 | 75 | | } |
| | 76 | |
|
| | 77 | | private static void SetUpRecipeIngredientRelationships(ModelBuilder modelBuilder) |
| | 78 | | { |
| 10 | 79 | | modelBuilder.Entity<RecipeIngredient>() |
| 10 | 80 | | .HasKey(ri => new { ri.RecipeId, ri.IngredientId }); |
| | 81 | |
|
| 10 | 82 | | modelBuilder.Entity<RecipeIngredient>() |
| 10 | 83 | | .HasOne(ri => ri.Recipe) |
| 10 | 84 | | .WithMany(r => r.RecipeIngredients) |
| 10 | 85 | | .HasForeignKey(ri => ri.RecipeId); |
| | 86 | |
|
| 10 | 87 | | modelBuilder.Entity<RecipeIngredient>() |
| 10 | 88 | | .HasOne(ri => ri.Ingredient) |
| 10 | 89 | | .WithMany(i => i.RecipeIngredients) |
| 10 | 90 | | .HasForeignKey(ri => ri.IngredientId); |
| 10 | 91 | | } |
| | 92 | |
|
| | 93 | | private static void SetUpRecipeRelationships(ModelBuilder modelBuilder) |
| | 94 | | { |
| 10 | 95 | | modelBuilder.Entity<Recipe>() |
| 10 | 96 | | .HasOne(r => r.User) |
| 10 | 97 | | .WithMany(u => u.Recipes) |
| 10 | 98 | | .HasForeignKey(r => r.UserId); |
| 10 | 99 | | } |
| | 100 | |
|
| | 101 | | private static void SetUpPantryItemRelationships(ModelBuilder modelBuilder) |
| | 102 | | { |
| 10 | 103 | | modelBuilder.Entity<PantryItem>() |
| 10 | 104 | | .HasOne(p => p.User) |
| 10 | 105 | | .WithMany(u => u.PantryItems) |
| 10 | 106 | | .HasForeignKey(p => p.UserId); |
| | 107 | |
|
| 10 | 108 | | modelBuilder.Entity<PantryItem>() |
| 10 | 109 | | .HasOne(p => p.Ingredient) |
| 10 | 110 | | .WithMany(i => i.PantryItems) |
| 10 | 111 | | .HasForeignKey(p => p.IngredientId); |
| 10 | 112 | | } |
| | 113 | |
|
| | 114 | | private static void SetUpIngredientRelationships(ModelBuilder modelBuilder) |
| | 115 | | { |
| 10 | 116 | | modelBuilder.Entity<Ingredient>() |
| 10 | 117 | | .HasOne(i => i.Category) |
| 10 | 118 | | .WithMany(c => c.Ingredients) |
| 10 | 119 | | .HasForeignKey(i => i.CategoryId); |
| 10 | 120 | | } |
| | 121 | | } |
| | 122 | | } |