Compare commits

..

No commits in common. "f5d580afe705b365bd2bed7afc99d7c1d0bcac65" and "75559daacb621bb5bc4f17d64b33d974aac9d229" have entirely different histories.

16 changed files with 79 additions and 457 deletions

View File

@ -13,7 +13,7 @@ namespace API.BusinessLogic
_dbAccess = dbAccess;
}
public async Task<IActionResult> GetRecipes(int userId)
public async Task<IActionResult> GetPrefereredRecipes(int userId)
{
var recipes = await _dbAccess.ReadRecipes(userId);
if (recipes == null || recipes.Count == 0) { return new ConflictObjectResult(new { message = "Could not find any recipes" }); }
@ -30,11 +30,11 @@ namespace API.BusinessLogic
return new OkObjectResult(recipe);
}
public async Task<IActionResult> CreateRecipe(RecipeDTO recipe, int userId)
public async Task<IActionResult> CreateRecipe(RecipeDTO recipe, int prefereredRecipesId)
{
var recipes = await _dbAccess.ReadRecipes(userId);
var prefereredRecipes = await _dbAccess.ReadAllRecipe(prefereredRecipesId);
foreach (var item in recipes)
foreach (var item in prefereredRecipes.Recipes)
{
if (item.Name == recipe.Name)
{
@ -42,27 +42,21 @@ namespace API.BusinessLogic
}
}
Recipe dish = new Recipe();
dish.Name = recipe.Name;
dish.Description = recipe.Description;
dish.Ingredients = new List<Ingredient>();
dish.Directions = new List<Directions>();
Recipe recipe1 = new Recipe();
recipe1.Name = recipe.Name;
recipe1.Directions = recipe.Directions;
recipe1.Description = recipe.Description;
recipe1.Ingredients = new List<Ingredient>();
foreach (var item in recipe.Ingredients)
{
Ingredient ingredient = new Ingredient();
ingredient.Unit = item.Unit;
ingredient.Name = item.Name;
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);
recipe1.Ingredients.Add(ingredient);
}
return await _dbAccess.CreateRecipe(dish, userId);
return await _dbAccess.CreateRecipe(recipe1, prefereredRecipesId);
}
public async Task<IActionResult> EditRecipe(RecipeDTO recipe, int recipeId, int userId)
@ -80,7 +74,7 @@ namespace API.BusinessLogic
dish.Name = recipe.Name;
dish.Description = recipe.Description;
dish.Directions = new List<Directions>();
dish.Directions = recipe.Directions;
foreach (var item in recipe.Ingredients)
{
Ingredient ingredient = new Ingredient();
@ -89,12 +83,6 @@ 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);
}

View File

@ -19,12 +19,12 @@ namespace API.Controllers
[Authorize]
[HttpGet("getall")]
public async Task<IActionResult> ReadRecipes()
public async Task<IActionResult> ReadPrefereredRecipes()
{
var claims = HttpContext.User.Claims;
string userIdString = claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value;
int userId = Convert.ToInt32(userIdString);
return await _recipeLogic.GetRecipes(userId);
return await _recipeLogic.GetPrefereredRecipes(userId);
}
[Authorize]
@ -35,10 +35,10 @@ namespace API.Controllers
}
[Authorize]
[HttpPost("create/{RecipesId}")]
public async Task<IActionResult> CreateRecipe([FromBody] RecipeDTO recipe, int RecipesId)
[HttpPost("create/{prefereredRecipesId}")]
public async Task<IActionResult> CreateRecipe([FromBody] RecipeDTO recipe, int prefereredRecipesId)
{
return await _recipeLogic.CreateRecipe(recipe, RecipesId);
return await _recipeLogic.CreateRecipe(recipe, prefereredRecipesId);
}
[Authorize]

View File

@ -9,6 +9,8 @@ namespace API.DBAccess
{
public DbSet<User> Users { get; set; }
public DbSet<PrefereredRecipes> PrefereredRecipes { get; set; }
public DbSet<Recipe> Recipes { get; set; }
public DbSet<ShoppingList> ShoppingList { get; set; }

View File

@ -21,14 +21,19 @@ namespace API.DBAccess
return recipes.Recipes;
}
public async Task<PrefereredRecipes> ReadAllRecipe(int prefereredRecipesId)
{
return await _context.PrefereredRecipes.Include(p => p.Recipes).FirstOrDefaultAsync(r => r.Id == prefereredRecipesId);
}
public async Task<Recipe> ReadRecipe(int recipeId)
{
return await _context.Recipes.Include(r => r.Ingredients).FirstOrDefaultAsync(r => r.Id == recipeId);
}
public async Task<IActionResult> CreateRecipe(Recipe recipe, int userId)
public async Task<IActionResult> CreateRecipe(Recipe recipe, int prefereredRecipeId)
{
var recipes = await _context.Users.Include(p => p.Recipes).FirstOrDefaultAsync(u => u.Id == userId);
var recipes = await _context.PrefereredRecipes.Include(p => p.Recipes).FirstOrDefaultAsync(p => p.Id == prefereredRecipeId);
recipes.Recipes.Add(recipe);

View File

@ -1,200 +0,0 @@
// <auto-generated />
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
{
/// <inheritdoc />
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<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<string>("Instruktions")
.IsRequired()
.HasColumnType("longtext");
b.Property<int?>("RecipeId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("RecipeId");
b.ToTable("Directions");
});
modelBuilder.Entity("API.Models.RecipeModels.Ingredient", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<double?>("Amount")
.HasColumnType("double");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("longtext");
b.Property<int?>("RecipeId")
.HasColumnType("int");
b.Property<string>("Unit")
.HasColumnType("longtext");
b.HasKey("Id");
b.HasIndex("RecipeId");
b.ToTable("Ingredient");
});
modelBuilder.Entity("API.Models.RecipeModels.Recipe", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<string>("Description")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("longtext");
b.Property<int?>("UserId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("UserId");
b.ToTable("Recipes");
});
modelBuilder.Entity("API.Models.ShoppingListModels.ShoppingList", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<double?>("Amount")
.HasColumnType("double");
b.Property<bool>("Checked")
.HasColumnType("tinyint(1)");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("Unit")
.HasColumnType("longtext");
b.Property<int?>("UserId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("UserId");
b.ToTable("ShoppingList");
});
modelBuilder.Entity("API.Models.UserModels.User", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<string>("Email")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("Password")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("RefreshToken")
.HasColumnType("longtext");
b.Property<DateTime>("RefreshTokenExpireAt")
.HasColumnType("datetime(6)");
b.Property<string>("Salt")
.HasColumnType("longtext");
b.Property<string>("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
}
}
}

View File

@ -1,175 +0,0 @@
using Microsoft.EntityFrameworkCore.Migrations;
using MySql.EntityFrameworkCore.Metadata;
#nullable disable
namespace API.Migrations
{
/// <inheritdoc />
public partial class MadeAmountAndUnitNullable : Migration
{
/// <inheritdoc />
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<string>(
name: "Unit",
table: "ShoppingList",
type: "longtext",
nullable: true,
oldClrType: typeof(string),
oldType: "longtext");
migrationBuilder.AlterColumn<double>(
name: "Amount",
table: "ShoppingList",
type: "double",
nullable: true,
oldClrType: typeof(double),
oldType: "double");
migrationBuilder.AlterColumn<string>(
name: "Unit",
table: "Ingredient",
type: "longtext",
nullable: true,
oldClrType: typeof(string),
oldType: "longtext");
migrationBuilder.AlterColumn<double>(
name: "Amount",
table: "Ingredient",
type: "double",
nullable: true,
oldClrType: typeof(int),
oldType: "int");
migrationBuilder.CreateTable(
name: "Directions",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("MySQL:ValueGenerationStrategy", MySQLValueGenerationStrategy.IdentityColumn),
Instruktions = table.Column<string>(type: "longtext", nullable: false),
RecipeId = table.Column<int>(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");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Directions");
migrationBuilder.AlterColumn<string>(
name: "Unit",
table: "ShoppingList",
type: "longtext",
nullable: false,
defaultValue: "",
oldClrType: typeof(string),
oldType: "longtext",
oldNullable: true);
migrationBuilder.AlterColumn<double>(
name: "Amount",
table: "ShoppingList",
type: "double",
nullable: false,
defaultValue: 0.0,
oldClrType: typeof(double),
oldType: "double",
oldNullable: true);
migrationBuilder.AddColumn<string>(
name: "Directions",
table: "Recipes",
type: "longtext",
nullable: false);
migrationBuilder.AddColumn<int>(
name: "PrefereredRecipesId",
table: "Recipes",
type: "int",
nullable: true);
migrationBuilder.AlterColumn<string>(
name: "Unit",
table: "Ingredient",
type: "longtext",
nullable: false,
defaultValue: "",
oldClrType: typeof(string),
oldType: "longtext",
oldNullable: true);
migrationBuilder.AlterColumn<int>(
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<int>(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");
}
}
}

View File

@ -19,34 +19,14 @@ namespace API.Migrations
.HasAnnotation("ProductVersion", "8.0.10")
.HasAnnotation("Relational:MaxIdentifierLength", 64);
modelBuilder.Entity("API.Models.RecipeModels.Directions", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<string>("Instruktions")
.IsRequired()
.HasColumnType("longtext");
b.Property<int?>("RecipeId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("RecipeId");
b.ToTable("Directions");
});
modelBuilder.Entity("API.Models.RecipeModels.Ingredient", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<double?>("Amount")
.HasColumnType("double");
b.Property<int>("Amount")
.HasColumnType("int");
b.Property<string>("Name")
.IsRequired()
@ -56,6 +36,7 @@ namespace API.Migrations
.HasColumnType("int");
b.Property<string>("Unit")
.IsRequired()
.HasColumnType("longtext");
b.HasKey("Id");
@ -65,6 +46,17 @@ namespace API.Migrations
b.ToTable("Ingredient");
});
modelBuilder.Entity("API.Models.RecipeModels.PrefereredRecipes", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.HasKey("Id");
b.ToTable("PrefereredRecipes");
});
modelBuilder.Entity("API.Models.RecipeModels.Recipe", b =>
{
b.Property<int>("Id")
@ -75,15 +67,24 @@ namespace API.Migrations
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("Directions")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("longtext");
b.Property<int?>("PrefereredRecipesId")
.HasColumnType("int");
b.Property<int?>("UserId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("PrefereredRecipesId");
b.HasIndex("UserId");
b.ToTable("Recipes");
@ -95,7 +96,7 @@ namespace API.Migrations
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<double?>("Amount")
b.Property<double>("Amount")
.HasColumnType("double");
b.Property<bool>("Checked")
@ -106,6 +107,7 @@ namespace API.Migrations
.HasColumnType("longtext");
b.Property<string>("Unit")
.IsRequired()
.HasColumnType("longtext");
b.Property<int?>("UserId")
@ -150,13 +152,6 @@ 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)
@ -166,6 +161,10 @@ 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");
@ -178,10 +177,13 @@ 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");
});

View File

@ -1,7 +0,0 @@
namespace API.Models.RecipeModels
{
public class DirectionsDTO
{
public string Instructions { get; set; }
}
}

View File

@ -4,9 +4,9 @@
{
public int Id { get; set; }
public double? Amount { get; set; }
public int Amount { get; set; }
public string? Unit { get; set; }
public string Unit { get; set; }
public string Name { get; set; }
}

View File

@ -2,9 +2,9 @@
{
public class IngredientDTO
{
public double? Amount { get; set; }
public int Amount { get; set; }
public string? Unit { get; set; }
public string Unit { get; set; }
public string Name { get; set; }
}

View File

@ -1,9 +1,9 @@
namespace API.Models.RecipeModels
{
public class Directions
public class PrefereredRecipes
{
public int Id { get; set; }
public string Instruktions { get; set; }
public List<Recipe> Recipes { get; set; }
}
}

View File

@ -0,0 +1,7 @@
namespace API.Models.RecipeModels
{
public class PrefereredRecipesDTO
{
public List<RecipeDTO> Recipes { get; set; }
}
}

View File

@ -8,7 +8,7 @@
public string Description { get; set; }
public List<Directions> Directions { get; set; }
public string Directions { get; set; }
public List<Ingredient> Ingredients { get; set; }
}

View File

@ -6,7 +6,7 @@
public string Description { get; set; }
public List<DirectionsDTO> Directions { get; set; }
public string Directions { get; set; }
public List<IngredientDTO> Ingredients { get; set; }
}

View File

@ -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; }

View File

@ -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; }
}