From adcb469a6f89ae91f8c618075de831250d8df302 Mon Sep 17 00:00:00 2001 From: Jeas0001 Date: Wed, 30 Apr 2025 13:05:51 +0200 Subject: [PATCH] Made Unit and Amount nullable and directions to a list --- backend/API/BusinessLogic/RecipeLogic.cs | 28 ++- ...0454_MadeAmountAndUnitNullable.Designer.cs | 200 ++++++++++++++++++ ...0250430110454_MadeAmountAndUnitNullable.cs | 175 +++++++++++++++ .../API/Migrations/DBContextModelSnapshot.cs | 66 +++--- backend/API/Models/RecipeModels/Directions.cs | 9 + .../API/Models/RecipeModels/DirectionsDTO.cs | 7 + backend/API/Models/RecipeModels/Ingredient.cs | 4 +- .../API/Models/RecipeModels/IngredientDTO.cs | 4 +- backend/API/Models/RecipeModels/Recipe.cs | 2 +- backend/API/Models/RecipeModels/RecipeDTO.cs | 2 +- .../Models/ShoppingListModels/ShoppingList.cs | 4 +- .../ShoppingListModels/ShoppingListItemDTO.cs | 4 +- 12 files changed, 453 insertions(+), 52 deletions(-) create mode 100644 backend/API/Migrations/20250430110454_MadeAmountAndUnitNullable.Designer.cs create mode 100644 backend/API/Migrations/20250430110454_MadeAmountAndUnitNullable.cs create mode 100644 backend/API/Models/RecipeModels/Directions.cs create mode 100644 backend/API/Models/RecipeModels/DirectionsDTO.cs diff --git a/backend/API/BusinessLogic/RecipeLogic.cs b/backend/API/BusinessLogic/RecipeLogic.cs index d50b02f..88b307f 100644 --- a/backend/API/BusinessLogic/RecipeLogic.cs +++ b/backend/API/BusinessLogic/RecipeLogic.cs @@ -42,21 +42,27 @@ namespace API.BusinessLogic } } - Recipe recipe1 = new Recipe(); - recipe1.Name = recipe.Name; - recipe1.Directions = recipe.Directions; - recipe1.Description = recipe.Description; - recipe1.Ingredients = new List(); + Recipe dish = new Recipe(); + dish.Name = recipe.Name; + dish.Description = recipe.Description; + dish.Ingredients = new List(); + dish.Directions = new List(); foreach (var item in recipe.Ingredients) { Ingredient ingredient = new Ingredient(); ingredient.Unit = item.Unit; ingredient.Name = item.Name; ingredient.Amount = item.Amount; - recipe1.Ingredients.Add(ingredient); + dish.Ingredients.Add(ingredient); + } + foreach (var item in recipe.Directions) + { + Directions directions = new Directions(); + directions.Instruktions = item.Instructions; + dish.Directions.Add(directions); } - return await _dbAccess.CreateRecipe(recipe1, userId); + return await _dbAccess.CreateRecipe(dish, userId); } public async Task EditRecipe(RecipeDTO recipe, int recipeId, int userId) @@ -74,7 +80,7 @@ namespace API.BusinessLogic dish.Name = recipe.Name; dish.Description = recipe.Description; - dish.Directions = recipe.Directions; + dish.Directions = new List(); foreach (var item in recipe.Ingredients) { Ingredient ingredient = new Ingredient(); @@ -83,6 +89,12 @@ namespace API.BusinessLogic ingredient.Amount = item.Amount; dish.Ingredients.Add(ingredient); } + foreach (var item in recipe.Directions) + { + Directions directions = new Directions(); + directions.Instruktions = item.Instructions; + dish.Directions.Add(directions); + } return await _dbAccess.UpdateRecipe(dish); } diff --git a/backend/API/Migrations/20250430110454_MadeAmountAndUnitNullable.Designer.cs b/backend/API/Migrations/20250430110454_MadeAmountAndUnitNullable.Designer.cs new file mode 100644 index 0000000..75e9da7 --- /dev/null +++ b/backend/API/Migrations/20250430110454_MadeAmountAndUnitNullable.Designer.cs @@ -0,0 +1,200 @@ +// +using System; +using API.DBAccess; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace API.Migrations +{ + [DbContext(typeof(DBContext))] + [Migration("20250430110454_MadeAmountAndUnitNullable")] + partial class MadeAmountAndUnitNullable + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.10") + .HasAnnotation("Relational:MaxIdentifierLength", 64); + + modelBuilder.Entity("API.Models.RecipeModels.Directions", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + b.Property("Instruktions") + .IsRequired() + .HasColumnType("longtext"); + + b.Property("RecipeId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("RecipeId"); + + b.ToTable("Directions"); + }); + + modelBuilder.Entity("API.Models.RecipeModels.Ingredient", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + b.Property("Amount") + .HasColumnType("double"); + + b.Property("Name") + .IsRequired() + .HasColumnType("longtext"); + + b.Property("RecipeId") + .HasColumnType("int"); + + b.Property("Unit") + .HasColumnType("longtext"); + + b.HasKey("Id"); + + b.HasIndex("RecipeId"); + + b.ToTable("Ingredient"); + }); + + modelBuilder.Entity("API.Models.RecipeModels.Recipe", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + b.Property("Description") + .IsRequired() + .HasColumnType("longtext"); + + b.Property("Name") + .IsRequired() + .HasColumnType("longtext"); + + b.Property("UserId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("Recipes"); + }); + + modelBuilder.Entity("API.Models.ShoppingListModels.ShoppingList", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + b.Property("Amount") + .HasColumnType("double"); + + b.Property("Checked") + .HasColumnType("tinyint(1)"); + + b.Property("Name") + .IsRequired() + .HasColumnType("longtext"); + + b.Property("Unit") + .HasColumnType("longtext"); + + b.Property("UserId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("ShoppingList"); + }); + + modelBuilder.Entity("API.Models.UserModels.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + b.Property("Email") + .IsRequired() + .HasColumnType("longtext"); + + b.Property("Password") + .IsRequired() + .HasColumnType("longtext"); + + b.Property("RefreshToken") + .HasColumnType("longtext"); + + b.Property("RefreshTokenExpireAt") + .HasColumnType("datetime(6)"); + + b.Property("Salt") + .HasColumnType("longtext"); + + b.Property("UserName") + .IsRequired() + .HasColumnType("longtext"); + + b.HasKey("Id"); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("API.Models.RecipeModels.Directions", b => + { + b.HasOne("API.Models.RecipeModels.Recipe", null) + .WithMany("Directions") + .HasForeignKey("RecipeId"); + }); + + modelBuilder.Entity("API.Models.RecipeModels.Ingredient", b => + { + b.HasOne("API.Models.RecipeModels.Recipe", null) + .WithMany("Ingredients") + .HasForeignKey("RecipeId"); + }); + + modelBuilder.Entity("API.Models.RecipeModels.Recipe", b => + { + b.HasOne("API.Models.UserModels.User", null) + .WithMany("Recipes") + .HasForeignKey("UserId"); + }); + + modelBuilder.Entity("API.Models.ShoppingListModels.ShoppingList", b => + { + b.HasOne("API.Models.UserModels.User", null) + .WithMany("ShoppingList") + .HasForeignKey("UserId"); + }); + + modelBuilder.Entity("API.Models.RecipeModels.Recipe", b => + { + b.Navigation("Directions"); + + b.Navigation("Ingredients"); + }); + + modelBuilder.Entity("API.Models.UserModels.User", b => + { + b.Navigation("Recipes"); + + b.Navigation("ShoppingList"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/backend/API/Migrations/20250430110454_MadeAmountAndUnitNullable.cs b/backend/API/Migrations/20250430110454_MadeAmountAndUnitNullable.cs new file mode 100644 index 0000000..4758f61 --- /dev/null +++ b/backend/API/Migrations/20250430110454_MadeAmountAndUnitNullable.cs @@ -0,0 +1,175 @@ +using Microsoft.EntityFrameworkCore.Migrations; +using MySql.EntityFrameworkCore.Metadata; + +#nullable disable + +namespace API.Migrations +{ + /// + public partial class MadeAmountAndUnitNullable : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Recipes_PrefereredRecipes_PrefereredRecipesId", + table: "Recipes"); + + migrationBuilder.DropTable( + name: "PrefereredRecipes"); + + migrationBuilder.DropIndex( + name: "IX_Recipes_PrefereredRecipesId", + table: "Recipes"); + + migrationBuilder.DropColumn( + name: "Directions", + table: "Recipes"); + + migrationBuilder.DropColumn( + name: "PrefereredRecipesId", + table: "Recipes"); + + migrationBuilder.AlterColumn( + name: "Unit", + table: "ShoppingList", + type: "longtext", + nullable: true, + oldClrType: typeof(string), + oldType: "longtext"); + + migrationBuilder.AlterColumn( + name: "Amount", + table: "ShoppingList", + type: "double", + nullable: true, + oldClrType: typeof(double), + oldType: "double"); + + migrationBuilder.AlterColumn( + name: "Unit", + table: "Ingredient", + type: "longtext", + nullable: true, + oldClrType: typeof(string), + oldType: "longtext"); + + migrationBuilder.AlterColumn( + name: "Amount", + table: "Ingredient", + type: "double", + nullable: true, + oldClrType: typeof(int), + oldType: "int"); + + migrationBuilder.CreateTable( + name: "Directions", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("MySQL:ValueGenerationStrategy", MySQLValueGenerationStrategy.IdentityColumn), + Instruktions = table.Column(type: "longtext", nullable: false), + RecipeId = table.Column(type: "int", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Directions", x => x.Id); + table.ForeignKey( + name: "FK_Directions_Recipes_RecipeId", + column: x => x.RecipeId, + principalTable: "Recipes", + principalColumn: "Id"); + }) + .Annotation("MySQL:Charset", "utf8mb4"); + + migrationBuilder.CreateIndex( + name: "IX_Directions_RecipeId", + table: "Directions", + column: "RecipeId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Directions"); + + migrationBuilder.AlterColumn( + name: "Unit", + table: "ShoppingList", + type: "longtext", + nullable: false, + defaultValue: "", + oldClrType: typeof(string), + oldType: "longtext", + oldNullable: true); + + migrationBuilder.AlterColumn( + name: "Amount", + table: "ShoppingList", + type: "double", + nullable: false, + defaultValue: 0.0, + oldClrType: typeof(double), + oldType: "double", + oldNullable: true); + + migrationBuilder.AddColumn( + name: "Directions", + table: "Recipes", + type: "longtext", + nullable: false); + + migrationBuilder.AddColumn( + name: "PrefereredRecipesId", + table: "Recipes", + type: "int", + nullable: true); + + migrationBuilder.AlterColumn( + name: "Unit", + table: "Ingredient", + type: "longtext", + nullable: false, + defaultValue: "", + oldClrType: typeof(string), + oldType: "longtext", + oldNullable: true); + + migrationBuilder.AlterColumn( + name: "Amount", + table: "Ingredient", + type: "int", + nullable: false, + defaultValue: 0, + oldClrType: typeof(double), + oldType: "double", + oldNullable: true); + + migrationBuilder.CreateTable( + name: "PrefereredRecipes", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("MySQL:ValueGenerationStrategy", MySQLValueGenerationStrategy.IdentityColumn) + }, + constraints: table => + { + table.PrimaryKey("PK_PrefereredRecipes", x => x.Id); + }) + .Annotation("MySQL:Charset", "utf8mb4"); + + migrationBuilder.CreateIndex( + name: "IX_Recipes_PrefereredRecipesId", + table: "Recipes", + column: "PrefereredRecipesId"); + + migrationBuilder.AddForeignKey( + name: "FK_Recipes_PrefereredRecipes_PrefereredRecipesId", + table: "Recipes", + column: "PrefereredRecipesId", + principalTable: "PrefereredRecipes", + principalColumn: "Id"); + } + } +} diff --git a/backend/API/Migrations/DBContextModelSnapshot.cs b/backend/API/Migrations/DBContextModelSnapshot.cs index 61fb5f3..d8a2315 100644 --- a/backend/API/Migrations/DBContextModelSnapshot.cs +++ b/backend/API/Migrations/DBContextModelSnapshot.cs @@ -19,14 +19,34 @@ namespace API.Migrations .HasAnnotation("ProductVersion", "8.0.10") .HasAnnotation("Relational:MaxIdentifierLength", 64); + modelBuilder.Entity("API.Models.RecipeModels.Directions", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + b.Property("Instruktions") + .IsRequired() + .HasColumnType("longtext"); + + b.Property("RecipeId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("RecipeId"); + + b.ToTable("Directions"); + }); + modelBuilder.Entity("API.Models.RecipeModels.Ingredient", b => { b.Property("Id") .ValueGeneratedOnAdd() .HasColumnType("int"); - b.Property("Amount") - .HasColumnType("int"); + b.Property("Amount") + .HasColumnType("double"); b.Property("Name") .IsRequired() @@ -36,7 +56,6 @@ namespace API.Migrations .HasColumnType("int"); b.Property("Unit") - .IsRequired() .HasColumnType("longtext"); b.HasKey("Id"); @@ -46,17 +65,6 @@ namespace API.Migrations b.ToTable("Ingredient"); }); - modelBuilder.Entity("API.Models.RecipeModels.PrefereredRecipes", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - b.HasKey("Id"); - - b.ToTable("PrefereredRecipes"); - }); - modelBuilder.Entity("API.Models.RecipeModels.Recipe", b => { b.Property("Id") @@ -67,24 +75,15 @@ namespace API.Migrations .IsRequired() .HasColumnType("longtext"); - b.Property("Directions") - .IsRequired() - .HasColumnType("longtext"); - b.Property("Name") .IsRequired() .HasColumnType("longtext"); - b.Property("PrefereredRecipesId") - .HasColumnType("int"); - b.Property("UserId") .HasColumnType("int"); b.HasKey("Id"); - b.HasIndex("PrefereredRecipesId"); - b.HasIndex("UserId"); b.ToTable("Recipes"); @@ -96,7 +95,7 @@ namespace API.Migrations .ValueGeneratedOnAdd() .HasColumnType("int"); - b.Property("Amount") + b.Property("Amount") .HasColumnType("double"); b.Property("Checked") @@ -107,7 +106,6 @@ namespace API.Migrations .HasColumnType("longtext"); b.Property("Unit") - .IsRequired() .HasColumnType("longtext"); b.Property("UserId") @@ -152,6 +150,13 @@ namespace API.Migrations b.ToTable("Users"); }); + modelBuilder.Entity("API.Models.RecipeModels.Directions", b => + { + b.HasOne("API.Models.RecipeModels.Recipe", null) + .WithMany("Directions") + .HasForeignKey("RecipeId"); + }); + modelBuilder.Entity("API.Models.RecipeModels.Ingredient", b => { b.HasOne("API.Models.RecipeModels.Recipe", null) @@ -161,10 +166,6 @@ namespace API.Migrations modelBuilder.Entity("API.Models.RecipeModels.Recipe", b => { - b.HasOne("API.Models.RecipeModels.PrefereredRecipes", null) - .WithMany("Recipes") - .HasForeignKey("PrefereredRecipesId"); - b.HasOne("API.Models.UserModels.User", null) .WithMany("Recipes") .HasForeignKey("UserId"); @@ -177,13 +178,10 @@ namespace API.Migrations .HasForeignKey("UserId"); }); - modelBuilder.Entity("API.Models.RecipeModels.PrefereredRecipes", b => - { - b.Navigation("Recipes"); - }); - modelBuilder.Entity("API.Models.RecipeModels.Recipe", b => { + b.Navigation("Directions"); + b.Navigation("Ingredients"); }); diff --git a/backend/API/Models/RecipeModels/Directions.cs b/backend/API/Models/RecipeModels/Directions.cs new file mode 100644 index 0000000..336c430 --- /dev/null +++ b/backend/API/Models/RecipeModels/Directions.cs @@ -0,0 +1,9 @@ +namespace API.Models.RecipeModels +{ + public class Directions + { + public int Id { get; set; } + + public string Instruktions { get; set; } + } +} diff --git a/backend/API/Models/RecipeModels/DirectionsDTO.cs b/backend/API/Models/RecipeModels/DirectionsDTO.cs new file mode 100644 index 0000000..9cc9c85 --- /dev/null +++ b/backend/API/Models/RecipeModels/DirectionsDTO.cs @@ -0,0 +1,7 @@ +namespace API.Models.RecipeModels +{ + public class DirectionsDTO + { + public string Instructions { get; set; } + } +} diff --git a/backend/API/Models/RecipeModels/Ingredient.cs b/backend/API/Models/RecipeModels/Ingredient.cs index cd9cb96..250e7e9 100644 --- a/backend/API/Models/RecipeModels/Ingredient.cs +++ b/backend/API/Models/RecipeModels/Ingredient.cs @@ -4,9 +4,9 @@ { public int Id { get; set; } - public int Amount { get; set; } + public double? Amount { get; set; } - public string Unit { get; set; } + public string? Unit { get; set; } public string Name { get; set; } } diff --git a/backend/API/Models/RecipeModels/IngredientDTO.cs b/backend/API/Models/RecipeModels/IngredientDTO.cs index 22de909..4667502 100644 --- a/backend/API/Models/RecipeModels/IngredientDTO.cs +++ b/backend/API/Models/RecipeModels/IngredientDTO.cs @@ -2,9 +2,9 @@ { public class IngredientDTO { - public int Amount { get; set; } + public double? Amount { get; set; } - public string Unit { get; set; } + public string? Unit { get; set; } public string Name { get; set; } } diff --git a/backend/API/Models/RecipeModels/Recipe.cs b/backend/API/Models/RecipeModels/Recipe.cs index 053aa64..2c449f2 100644 --- a/backend/API/Models/RecipeModels/Recipe.cs +++ b/backend/API/Models/RecipeModels/Recipe.cs @@ -8,7 +8,7 @@ public string Description { get; set; } - public string Directions { get; set; } + public List Directions { get; set; } public List Ingredients { get; set; } } diff --git a/backend/API/Models/RecipeModels/RecipeDTO.cs b/backend/API/Models/RecipeModels/RecipeDTO.cs index 0f27cdb..289fe2e 100644 --- a/backend/API/Models/RecipeModels/RecipeDTO.cs +++ b/backend/API/Models/RecipeModels/RecipeDTO.cs @@ -6,7 +6,7 @@ public string Description { get; set; } - public string Directions { get; set; } + public List Directions { get; set; } public List Ingredients { get; set; } } diff --git a/backend/API/Models/ShoppingListModels/ShoppingList.cs b/backend/API/Models/ShoppingListModels/ShoppingList.cs index a2c206a..648e9dc 100644 --- a/backend/API/Models/ShoppingListModels/ShoppingList.cs +++ b/backend/API/Models/ShoppingListModels/ShoppingList.cs @@ -4,9 +4,9 @@ { public int Id { get; set; } - public double Amount { get; set; } + public double? Amount { get; set; } - public string Unit { get; set; } + public string? Unit { get; set; } public string Name { get; set; } diff --git a/backend/API/Models/ShoppingListModels/ShoppingListItemDTO.cs b/backend/API/Models/ShoppingListModels/ShoppingListItemDTO.cs index 4b1959e..5089e3e 100644 --- a/backend/API/Models/ShoppingListModels/ShoppingListItemDTO.cs +++ b/backend/API/Models/ShoppingListModels/ShoppingListItemDTO.cs @@ -5,9 +5,9 @@ public string Name { get; set; } - public double Amount { get; set; } + public double? Amount { get; set; } - public string Unit { get; set; } + public string? Unit { get; set; } public bool Checked { get; set; } }