All thread resolved

This commit is contained in:
franc
2025-04-10 10:23:43 +02:00
parent 127a60b621
commit 603dc4a2c6
8 changed files with 23 additions and 24 deletions
@@ -45,7 +45,7 @@
} }
[HttpPost("ReplaceInstructionImage")] [HttpPost("ReplaceInstructionImage")]
[ValidateAntiForgeryToken] [AutoValidateAntiforgeryToken]
public async Task<IActionResult> ReplaceInstructionImage(Guid instructionId, Guid mediaFileIdToReplace, IFormFile? newPhoto) public async Task<IActionResult> ReplaceInstructionImage(Guid instructionId, Guid mediaFileIdToReplace, IFormFile? newPhoto)
{ {
if (newPhoto is null) if (newPhoto is null)
@@ -4,7 +4,6 @@
using Francesco.Recipes.World.Models; using Francesco.Recipes.World.Models;
using Francesco.Recipes.World.Repositories.ShoppingList; using Francesco.Recipes.World.Repositories.ShoppingList;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
public class ShoppingListController : Controller public class ShoppingListController : Controller
{ {
@@ -21,17 +20,17 @@
[ValidateAntiForgeryToken] [ValidateAntiForgeryToken]
public async Task<IActionResult> CreateOrAddIngredients([FromBody] CreateOrAddIngredientRequestModel request) public async Task<IActionResult> CreateOrAddIngredients([FromBody] CreateOrAddIngredientRequestModel request)
{ {
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (request == null || request.IngredientIds == null || !request.IngredientIds.Any()) if (request == null || request.IngredientIds == null || !request.IngredientIds.Any())
{ {
return BadRequest("No ingredients provided."); return BadRequest("No ingredients provided.");
} }
await _shoppingListRepository.AddIngredientsToShoppingListAsync(request.RecipeId, request.IngredientIds); var shoppingList = await _shoppingListRepository.AddIngredientsToShoppingListAsync(request.RecipeId, request.IngredientIds);
var shoppingList = await _context.ShoppingLists
.Include(sl => sl.RecipeShoppingList)
.ThenInclude(rsl => rsl.Recipe)
.FirstOrDefaultAsync(sl => sl.RecipeShoppingList.Any(rsl => rsl.Recipe.Id == request.RecipeId));
if (shoppingList == null) if (shoppingList == null)
{ {
-1
View File
@@ -7,7 +7,6 @@ using Francesco.Recipes.World.Repositories.MediaFile;
using Francesco.Recipes.World.Repositories.Recipe; using Francesco.Recipes.World.Repositories.Recipe;
using Francesco.Recipes.World.Repositories.ShoppingList; using Francesco.Recipes.World.Repositories.ShoppingList;
using Francesco.Recipes.World.Repositories.Unit; using Francesco.Recipes.World.Repositories.Unit;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
@@ -92,7 +92,7 @@
{ {
await RemoveExistingMediaAsync(recipe, "image/"); await RemoveExistingMediaAsync(recipe, "image/");
} }
else if (isVideo) else
{ {
await RemoveExistingMediaAsync(recipe, "video/"); await RemoveExistingMediaAsync(recipe, "video/");
} }
@@ -52,9 +52,11 @@
} }
var ingredients = await _ingredientRepository.GetIngredientsByNameAsync(ingredientName); var ingredients = await _ingredientRepository.GetIngredientsByNameAsync(ingredientName);
var exactMatch = ingredients
.FirstOrDefault(i => i.Name.Equals(ingredientName, StringComparison.OrdinalIgnoreCase));
Ingredient ingredient; Ingredient ingredient;
if (!ingredients.Any()) if (exactMatch == null)
{ {
ingredient = new Ingredient ingredient = new Ingredient
{ {
@@ -66,7 +68,7 @@
} }
else else
{ {
ingredient = ingredients.First(); ingredient = exactMatch;
} }
var existingEntry = await _context.RecipeIngredients var existingEntry = await _context.RecipeIngredients
@@ -83,6 +85,7 @@
Unit = unit, Unit = unit,
Quantity = quantity, Quantity = quantity,
}; };
_context.Add(recipeIngredient); _context.Add(recipeIngredient);
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
} }
@@ -2,10 +2,11 @@
{ {
using Francesco.Recipes.World.Models.BackendModels.IngredientShoppingList; using Francesco.Recipes.World.Models.BackendModels.IngredientShoppingList;
using Francesco.Recipes.World.Models.BackendModels.Recipe; using Francesco.Recipes.World.Models.BackendModels.Recipe;
using Francesco.Recipes.World.Models.BackendModels.Shoppinglist;
public interface IShoppingListRepository public interface IShoppingListRepository
{ {
Task AddIngredientsToShoppingListAsync(Guid shoppingListId, List<Guid> ingredientIds); Task<ShoppingList> AddIngredientsToShoppingListAsync(Guid recipeId, List<Guid> ingredientIds);
Task<IEnumerable<RecipeIngredientShoppingList>> GetIngredientsOfRecipeInListAsync(Guid shoppingListRecipeId); Task<IEnumerable<RecipeIngredientShoppingList>> GetIngredientsOfRecipeInListAsync(Guid shoppingListRecipeId);
@@ -16,7 +16,7 @@
_context = context; _context = context;
} }
public async Task AddIngredientsToShoppingListAsync(Guid recipeId, List<Guid> ingredientIds) public async Task<ShoppingList> AddIngredientsToShoppingListAsync(Guid recipeId, List<Guid> ingredientIds)
{ {
if (ingredientIds == null || !ingredientIds.Any()) if (ingredientIds == null || !ingredientIds.Any())
{ {
@@ -29,13 +29,12 @@
if (recipe == null) if (recipe == null)
{ {
throw new Exception("Rezept nicht gefunden."); throw new Exception("Recipe not found.");
} }
var shoppingList = await _context.ShoppingLists var shoppingList = await _context.ShoppingLists
.Include(sl => sl.RecipeShoppingList) .Include(sl => sl.RecipeShoppingList)
.ThenInclude(rsl => rsl.SelectedIngredients) .ThenInclude(rsl => rsl.SelectedIngredients)
.ThenInclude(si => si.RecipeIngredient)
.Include(sl => sl.RecipeShoppingList) .Include(sl => sl.RecipeShoppingList)
.ThenInclude(rsl => rsl.Recipe) .ThenInclude(rsl => rsl.Recipe)
.FirstOrDefaultAsync(sl => sl.RecipeShoppingList.Any(rsl => rsl.Recipe.Id == recipeId)); .FirstOrDefaultAsync(sl => sl.RecipeShoppingList.Any(rsl => rsl.Recipe.Id == recipeId));
@@ -46,7 +45,7 @@
if (!recipeIngredients.Any()) if (!recipeIngredients.Any())
{ {
throw new Exception("Keine gültigen Zutaten gefunden."); throw new Exception("No valid ingredients found.");
} }
if (shoppingList == null) if (shoppingList == null)
@@ -55,7 +54,6 @@
{ {
Id = Guid.NewGuid(), Id = Guid.NewGuid(),
CreatedAt = DateTime.UtcNow, CreatedAt = DateTime.UtcNow,
ModifiedAt = null,
RecipeShoppingList = new List<RecipeShoppingList>(), RecipeShoppingList = new List<RecipeShoppingList>(),
}; };
@@ -93,10 +91,7 @@
foreach (var ri in recipeIngredients) foreach (var ri in recipeIngredients)
{ {
var alreadyExists = existingRecipeList.SelectedIngredients if (!existingRecipeList.SelectedIngredients.Any(si => si.RecipeIngredient.Id == ri.Id))
.Any(si => si.RecipeIngredient.Id == ri.Id);
if (!alreadyExists)
{ {
existingRecipeList.SelectedIngredients.Add(new RecipeIngredientShoppingList existingRecipeList.SelectedIngredients.Add(new RecipeIngredientShoppingList
{ {
@@ -109,6 +104,9 @@
shoppingList.ModifiedAt = DateTime.UtcNow; shoppingList.ModifiedAt = DateTime.UtcNow;
} }
await _context.SaveChangesAsync();
return shoppingList;
} }
public async Task<IEnumerable<RecipeIngredientShoppingList>> GetIngredientsOfRecipeInListAsync(Guid shoppingListRecipeId) public async Task<IEnumerable<RecipeIngredientShoppingList>> GetIngredientsOfRecipeInListAsync(Guid shoppingListRecipeId)
@@ -153,8 +151,6 @@
public async Task DeleteShoppingListAsync(Guid shoppingListId) public async Task DeleteShoppingListAsync(Guid shoppingListId)
{ {
var list = await _context.ShoppingLists var list = await _context.ShoppingLists
.Include(sl => sl.RecipeShoppingList)
.ThenInclude(r => r.SelectedIngredients)
.FirstOrDefaultAsync(sl => sl.Id == shoppingListId); .FirstOrDefaultAsync(sl => sl.Id == shoppingListId);
if (list != null) if (list != null)
@@ -44,6 +44,7 @@
<script src="~/lib/jquery/dist/jquery.min.js"></script> <script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script> <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<script src="~/js/site.js" asp-append-version="true"></script> <script src="~/js/site.js" asp-append-version="true"></script>
<script src="https://unpkg.com/htmx.org@2.0.4/dist/htmx.js" integrity="sha384-oeUn82QNXPuVkGCkcrInrS1twIxKhkZiFfr2TdiuObZ3n3yIeMiqcRzkIcguaof1" crossorigin="anonymous"></script>
@await RenderSectionAsync("Scripts", required: false) @await RenderSectionAsync("Scripts", required: false)
</body> </body>
</html> </html>