< Summary

Information
Class: Backend.PlannerContext
Assembly: Backend
File(s): D:\a\smart-meal-planner\smart-meal-planner\backend\Backend\PlannerContext.cs
Line coverage
100%
Covered lines: 75
Uncovered lines: 0
Coverable lines: 75
Total lines: 122
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

D:\a\smart-meal-planner\smart-meal-planner\backend\Backend\PlannerContext.cs

#LineLine coverage
 1using Microsoft.EntityFrameworkCore;
 2using Backend.Model;
 3
 4namespace Backend
 5{
 6    public class PlannerContext : DbContext
 7    {
 8        public PlannerContext(DbContextOptions<PlannerContext> options, IConfiguration configuration, ILogger<PlannerCon
 459            : base(options)
 10        {
 4511            _configuration = configuration;
 4512            _logger = logger;
 4513            _logger.LogInformation("PlannerContext created with options: {Options}", options);
 4514            _logger.LogInformation("Using connection string: {ConnectionString}", _configuration.GetConnectionString("De
 4515            Database.EnsureCreated();
 4516            _logger.LogInformation("PlannerContext initialized with connection string: {ConnectionString}", _configurati
 4517        }
 18
 19        private readonly IConfiguration _configuration;
 20        private readonly ILogger<PlannerContext> _logger;
 21
 8022        public DbSet<User> Users { get; set; } = null!;
 4723        public DbSet<Category> Categories { get; set; } = null!;
 4624        public DbSet<Ingredient> Ingredients { get; set; } = null!;
 4625        public DbSet<PantryItem> PantryItems { get; set; } = null!;
 4626        public DbSet<Recipe> Recipes { get; set; } = null!;
 4627        public DbSet<RecipeIngredient> RecipeIngredients { get; set; } = null!;
 4628        public DbSet<MealPlan> MealPlans { get; set; } = null!;
 4629        public DbSet<MealPlanEntry> MealPlanEntries { get; set; } = null!;
 5330        public DbSet<RefreshToken> RefreshTokens { get; set; } = null!;
 31
 32        protected override void OnModelCreating(ModelBuilder modelBuilder)
 33        {
 1034            base.OnModelCreating(modelBuilder);
 35
 36            // Relationships
 1037            SetUpIngredientRelationships(modelBuilder);
 1038            SetUpPantryItemRelationships(modelBuilder);
 1039            SetUpRecipeRelationships(modelBuilder);
 1040            SetUpRecipeIngredientRelationships(modelBuilder);
 1041            SetUpMealPlanRelationships(modelBuilder);
 1042            SetUpMealPlanEntryRelationships(modelBuilder);
 1043            SeedCategories(modelBuilder);
 44
 1045            _logger.LogInformation("Model creating completed with configured relationships and seeded categories.");
 1046        }
 47
 48        private void SeedCategories(ModelBuilder modelBuilder)
 49        {
 1050            var categories = _configuration.GetSection("Categories").Get<Category[]>();
 1051            if (categories != null && categories.Length > 0)
 252                modelBuilder.Entity<Category>().HasData(categories);
 53
 1054        }
 55
 56        private static void SetUpMealPlanEntryRelationships(ModelBuilder modelBuilder)
 57        {
 1058            modelBuilder.Entity<MealPlanEntry>()
 1059                .HasOne(mpe => mpe.MealPlan)
 1060                .WithMany(mp => mp.MealPlanEntries)
 1061                .HasForeignKey(mpe => mpe.MealPlanId);
 62
 1063            modelBuilder.Entity<MealPlanEntry>()
 1064                .HasOne(mpe => mpe.Recipe)
 1065                .WithMany(r => r.MealPlanEntries)
 1066                .HasForeignKey(mpe => mpe.RecipeId);
 1067        }
 68
 69        private static void SetUpMealPlanRelationships(ModelBuilder modelBuilder)
 70        {
 1071            modelBuilder.Entity<MealPlan>()
 1072                .HasOne(mp => mp.User)
 1073                .WithMany(u => u.MealPlans)
 1074                .HasForeignKey(mp => mp.UserId);
 1075        }
 76
 77        private static void SetUpRecipeIngredientRelationships(ModelBuilder modelBuilder)
 78        {
 1079            modelBuilder.Entity<RecipeIngredient>()
 1080                .HasKey(ri => new { ri.RecipeId, ri.IngredientId });
 81
 1082            modelBuilder.Entity<RecipeIngredient>()
 1083                .HasOne(ri => ri.Recipe)
 1084                .WithMany(r => r.RecipeIngredients)
 1085                .HasForeignKey(ri => ri.RecipeId);
 86
 1087            modelBuilder.Entity<RecipeIngredient>()
 1088                .HasOne(ri => ri.Ingredient)
 1089                .WithMany(i => i.RecipeIngredients)
 1090                .HasForeignKey(ri => ri.IngredientId);
 1091        }
 92
 93        private static void SetUpRecipeRelationships(ModelBuilder modelBuilder)
 94        {
 1095            modelBuilder.Entity<Recipe>()
 1096                .HasOne(r => r.User)
 1097                .WithMany(u => u.Recipes)
 1098                .HasForeignKey(r => r.UserId);
 1099        }
 100
 101        private static void SetUpPantryItemRelationships(ModelBuilder modelBuilder)
 102        {
 10103            modelBuilder.Entity<PantryItem>()
 10104                .HasOne(p => p.User)
 10105                .WithMany(u => u.PantryItems)
 10106                .HasForeignKey(p => p.UserId);
 107
 10108            modelBuilder.Entity<PantryItem>()
 10109                .HasOne(p => p.Ingredient)
 10110                .WithMany(i => i.PantryItems)
 10111                .HasForeignKey(p => p.IngredientId);
 10112        }
 113
 114        private static void SetUpIngredientRelationships(ModelBuilder modelBuilder)
 115        {
 10116            modelBuilder.Entity<Ingredient>()
 10117                .HasOne(i => i.Category)
 10118                .WithMany(c => c.Ingredients)
 10119                .HasForeignKey(i => i.CategoryId);
 10120        }
 121    }
 122}