Solve all thread from mergeRequest to RecipeController

This commit is contained in:
franc
2025-05-07 08:53:12 +02:00
parent 7168d93621
commit abb1d774fe
@@ -2,6 +2,8 @@
{ {
using Francesco.Recipes.World.Data; using Francesco.Recipes.World.Data;
using Francesco.Recipes.World.Models; using Francesco.Recipes.World.Models;
using Francesco.Recipes.World.Models.BackendModels.Ingredient;
using Francesco.Recipes.World.Models.BackendModels.Instruction;
using Francesco.Recipes.World.Models.BackendModels.Recipe; using Francesco.Recipes.World.Models.BackendModels.Recipe;
using Francesco.Recipes.World.Repositories.Category; using Francesco.Recipes.World.Repositories.Category;
using Francesco.Recipes.World.Repositories.Favorit; using Francesco.Recipes.World.Repositories.Favorit;
@@ -14,7 +16,6 @@
using Francesco.Recipes.World.Views.Recipe; using Francesco.Recipes.World.Views.Recipe;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
public class RecipeController : Controller public class RecipeController : Controller
{ {
@@ -101,6 +102,11 @@
[ValidateAntiForgeryToken] [ValidateAntiForgeryToken]
public async Task<IActionResult> AddOrCreateIngredient(Guid recipeId, string ingredientName, int quantity, Guid unitId) public async Task<IActionResult> AddOrCreateIngredient(Guid recipeId, string ingredientName, int quantity, Guid unitId)
{ {
if (recipeId == Guid.Empty)
{
return BadRequest("Recipe ID cannot be empty.");
}
if (quantity <= 0) if (quantity <= 0)
{ {
ModelState.AddModelError(nameof(quantity), "Die Menge muss größer als 0 sein."); ModelState.AddModelError(nameof(quantity), "Die Menge muss größer als 0 sein.");
@@ -113,7 +119,7 @@
return View(); return View();
} }
await _recipeRepository.AddOrCreateIngredientToRecipeAsync(recipeId, ingredientName, quantity, unitId); await _recipeRepository.CreateRecipeIngredientAsync(recipeId, ingredientName, quantity, unitId);
return RedirectToAction("Details", new { id = recipeId }); return RedirectToAction("Details", new { id = recipeId });
} }
@@ -128,32 +134,30 @@
} }
var units = await _unitRepository.GetAllUnitsAsync(); var units = await _unitRepository.GetAllUnitsAsync();
ViewBag.Units = new SelectList(units, "Id", "Name");
var viewModel = new CreateRecipeViewModel var viewModel = new CreateRecipeViewModel
{ {
CategoryId = categoryId, CategoryId = categoryId,
CategoryName = category.Name,
IngredientViewModel = new IngredientViewModel IngredientViewModel = new IngredientViewModel
{ {
RecipeId = Guid.Empty, RecipeId = Guid.Empty,
Ingredients = new List<Models.BackendModels.Ingredient.Ingredient>(), Ingredients = new List<Ingredient>(),
Units = units.ToList(), Units = units.ToList(),
}, },
InstructionViewModel = new InstructionViewModel InstructionViewModel = new InstructionViewModel
{ {
RecipeId = Guid.Empty, RecipeId = Guid.Empty,
Instructions = new List<Models.BackendModels.Instruction.Instruction>(), Instructions = new List<Instruction>(),
}, },
}; };
ViewBag.CategoryName = category.Name;
return View(viewModel); return View(viewModel);
} }
// POST: /Recipe/Create/{categoryId} // POST: /Recipe/Create/{categoryId}
[HttpPost("Create/{categoryId}")] [HttpPost("Create/{categoryId}")]
[ValidateAntiForgeryToken] [ValidateAntiForgeryToken]
public async Task<IActionResult> Create(Guid categoryId, CreateRecipeViewModel model, IFormFile? photo, IFormFile? video) public async Task<IActionResult> Create(Guid categoryId, CreateRecipeViewModel model)
{ {
if (model == null) if (model == null)
{ {
@@ -179,79 +183,124 @@
} }
var units = await _unitRepository.GetAllUnitsAsync(); var units = await _unitRepository.GetAllUnitsAsync();
ViewBag.Units = new SelectList(units, "Id", "Name");
ViewBag.CategoryName = category.Name; if (model.IngredientViewModel == null)
{
model.IngredientViewModel = new IngredientViewModel
{
RecipeId = Guid.Empty,
Ingredients = new List<Ingredient>(),
Units = units.ToList(),
};
}
else
{
model.IngredientViewModel.Units = units.ToList();
}
model.CategoryName = category.Name;
return View(model); return View(model);
} }
var categoryEntity = await _categoryRepository.GetCategoryByIdAsync(categoryId); using var transaction = await _context.Database.BeginTransactionAsync();
if (categoryEntity == null) try
{ {
return NotFound($"Kategorie mit ID {categoryId} wurde nicht gefunden."); var categoryEntity = await _categoryRepository.GetCategoryByIdAsync(categoryId);
} if (categoryEntity == null)
model.PreparationTime = new TimeSpan(model.PrepHours, model.PrepMinutes, 0);
model.CookingTime = new TimeSpan(model.CookHours, model.CookMinutes, 0);
var recipe = await _recipeRepository.CreateRecipeForCategoryAsync(
categoryEntity,
model.Name,
model.Description ?? string.Empty,
model.Difficulty,
model.Servings,
model.PreparationTime,
model.CookingTime);
if (photo != null)
{
await _mediaFileRepository.UploadRecipeMediaAsync(recipe.Id, photo);
}
if (video != null)
{
await _mediaFileRepository.UploadRecipeMediaAsync(recipe.Id, video);
}
if (model.IngredientViewModel?.Ingredients != null)
{
foreach (var ingredient in model.IngredientViewModel.Ingredients)
{ {
var ri = ingredient.RecipeIngredients?.FirstOrDefault(); return NotFound($"Kategorie mit ID {categoryId} wurde nicht gefunden.");
if (!string.IsNullOrWhiteSpace(ingredient.Name) && ri?.Quantity > 0 && ri?.Unit?.Id != null)
{
await _recipeRepository.AddOrCreateIngredientToRecipeAsync(
recipe.Id,
ingredient.Name,
ri.Quantity,
ri.Unit.Id);
}
} }
}
if (model.InstructionViewModel?.Instructions != null) model.PreparationTime = new TimeSpan(model.PrepHours, model.PrepMinutes, 0);
{ model.CookingTime = new TimeSpan(model.CookHours, model.CookMinutes, 0);
for (var i = 0; i < model.InstructionViewModel.Instructions.Count; i++)
var recipe = await _recipeRepository.CreateRecipeForCategoryAsync(
categoryEntity,
model.Name,
model.Description ?? string.Empty,
model.Difficulty,
model.Servings,
model.PreparationTime,
model.CookingTime);
if (model.Photo != null)
{ {
var instruction = model.InstructionViewModel.Instructions[i]; await _mediaFileRepository.UploadRecipeMediaAsync(recipe.Id, model.Photo);
if (!string.IsNullOrWhiteSpace(instruction.Description)) }
{
var fileKey = $"InstructionViewModel.Instructions[{i}].MediaFile";
IFormFile? imageFile = null;
if (Request.Form.Files.Any(f => f.Name == fileKey)) if (model.Video != null)
{
await _mediaFileRepository.UploadRecipeMediaAsync(recipe.Id, model.Video);
}
if (model.IngredientViewModel?.Ingredients != null)
{
foreach (var ingredient in model.IngredientViewModel.Ingredients)
{
var ri = ingredient.RecipeIngredients?.FirstOrDefault();
if (!string.IsNullOrWhiteSpace(ingredient.Name) && ri?.Quantity > 0 && ri?.Unit?.Id != null)
{ {
imageFile = Request.Form.Files[fileKey]; await _recipeRepository.CreateRecipeIngredientAsync(
recipe.Id,
ingredient.Name,
ri.Quantity,
ri.Unit.Id);
} }
await _instructionRepository.CreateInstructionWithImageToRecipeAsync(
recipe.Id,
instruction.Description,
imageFile);
} }
} }
}
return RedirectToAction("Details", new { recipeId = recipe.Id }); if (model.InstructionViewModel?.Instructions != null)
{
for (var i = 0; i < model.InstructionViewModel.Instructions.Count; i++)
{
var instruction = model.InstructionViewModel.Instructions[i];
if (!string.IsNullOrWhiteSpace(instruction.Description))
{
var fileKey = $"InstructionViewModel.Instructions[{i}].MediaFile";
IFormFile? imageFile = null;
if (Request.Form.Files.Any(f => f.Name == fileKey))
{
imageFile = Request.Form.Files[fileKey];
}
await _instructionRepository.CreateInstructionAsync(
recipe.Id,
instruction.Description,
imageFile);
}
}
}
await transaction.CommitAsync();
return RedirectToAction("Details", new { recipeId = recipe.Id });
}
catch (Exception ex)
{
await transaction.RollbackAsync();
ModelState.AddModelError(string.Empty, $"Ein Fehler ist aufgetreten beim Erstellen des Rezepts: {ex.Message}");
var category = await _categoryRepository.GetCategoryByIdAsync(categoryId);
var units = await _unitRepository.GetAllUnitsAsync();
if (model.IngredientViewModel == null)
{
model.IngredientViewModel = new IngredientViewModel
{
RecipeId = Guid.Empty,
Ingredients = new List<Models.BackendModels.Ingredient.Ingredient>(),
Units = units.ToList(),
};
}
else
{
model.IngredientViewModel.Units = units.ToList();
}
model.CategoryName = category?.Name ?? string.Empty;
return View(model);
}
} }
// GET: /Recipe/{recipeId}/RemoveIngredient/{ingredientId} // GET: /Recipe/{recipeId}/RemoveIngredient/{ingredientId}
@@ -346,11 +395,7 @@
return NotFound("Recipe not found."); return NotFound("Recipe not found.");
} }
var instructions = await _context.Instructions var instructions = await _instructionRepository.GetInstructionsOfRecipeAsync(recipeId);
.Include(i => i.MediaFiles)
.Where(i => i.Recipe.Id == recipeId)
.OrderBy(i => i.Number)
.ToListAsync();
var viewModel = new InstructionViewModel var viewModel = new InstructionViewModel
{ {
@@ -368,7 +413,7 @@
{ {
try try
{ {
await _instructionRepository.CreateInstructionWithImageToRecipeAsync(recipeId, description, image); await _instructionRepository.CreateInstructionAsync(recipeId, description, image);
var recipe = await _recipeRepository.GetRecipeByIdAsync(recipeId); var recipe = await _recipeRepository.GetRecipeByIdAsync(recipeId);
var instructions = recipe?.Instructions?.ToList() ?? new List<Models.BackendModels.Instruction.Instruction>(); var instructions = recipe?.Instructions?.ToList() ?? new List<Models.BackendModels.Instruction.Instruction>();
@@ -383,18 +428,7 @@
} }
catch (Exception ex) catch (Exception ex)
{ {
ModelState.AddModelError(string.Empty, ex.Message); return BadRequest(ex.Message);
var recipe = await _recipeRepository.GetRecipeByIdAsync(recipeId);
var instructions = recipe?.Instructions?.ToList() ?? new List<Models.BackendModels.Instruction.Instruction>();
var viewModel = new InstructionViewModel
{
RecipeId = recipeId,
Instructions = instructions,
};
return View(viewModel);
} }
} }
@@ -441,7 +475,7 @@
public async Task<IActionResult> GetIngredients(Guid recipeId) public async Task<IActionResult> GetIngredients(Guid recipeId)
{ {
var recipeIngredients = (await _ingredientRepository.GetIngredientsByRecipeIdAsync(recipeId)).Select(ri => ri.Ingredient).ToList(); var recipeIngredients = (await _ingredientRepository.GetIngredientsByRecipeIdAsync(recipeId)).Select(ri => ri.Ingredient).ToList();
var units = (await _unitRepository.GetAllUnitsAsync()).ToList(); var units = await _unitRepository.GetAllUnitsAsync();
var viewModel = new IngredientViewModel var viewModel = new IngredientViewModel
{ {