diff --git a/Francesco.Recipes.World/Controller/Category/CategoryController.cs b/Francesco.Recipes.World/Controller/Category/CategoryController.cs new file mode 100644 index 0000000..0257ab4 --- /dev/null +++ b/Francesco.Recipes.World/Controller/Category/CategoryController.cs @@ -0,0 +1,43 @@ +namespace Francesco.Recipes.World.Controller.Category +{ + using Francesco.Recipes.World.Models.BackendModels.Category; + using Francesco.Recipes.World.Models.BackendModels.Recipe; + using Francesco.Recipes.World.Repositories.Category; + using Microsoft.AspNetCore.Mvc; + + [Route("Category")] + + public class CategoryController : Controller + { + private readonly ICategoryRepository _categoryRepository; + + public CategoryController(ICategoryRepository categoryRepository) + { + _categoryRepository = categoryRepository; + } + + // GET: /Category + [HttpGet] + public async Task>> Index() + { + var categories = await _categoryRepository.GetAllCategoriesAsync(); + return View(categories); + } + + // GET: /Category/{id} + [HttpGet("{id:guid}")] + public async Task Details(Guid id) + { + var category = await _categoryRepository.GetCategoryByIdAsync(id); + return View(category); + } + + // GET: /Category/{id}/recipes + [HttpGet("{id:guid}/recipes")] + public async Task>> GetRecipesByCategory(Guid id) + { + var recipes = await _categoryRepository.GetRecipesByCategoryAsync(id); + return Ok(recipes); + } + } +} diff --git a/Francesco.Recipes.World/Controller/Ingredient/IngredientController.cs b/Francesco.Recipes.World/Controller/Ingredient/IngredientController.cs new file mode 100644 index 0000000..d59fb2a --- /dev/null +++ b/Francesco.Recipes.World/Controller/Ingredient/IngredientController.cs @@ -0,0 +1,6 @@ +namespace Francesco.Recipes.World.Controller.Ingredient +{ + public class IngredientController + { + } +} diff --git a/Francesco.Recipes.World/Controller/Instruction/InstructionController.cs b/Francesco.Recipes.World/Controller/Instruction/InstructionController.cs new file mode 100644 index 0000000..d86aadf --- /dev/null +++ b/Francesco.Recipes.World/Controller/Instruction/InstructionController.cs @@ -0,0 +1,6 @@ +namespace Francesco.Recipes.World.Controller.Instruction +{ + public class InstructionController + { + } +} diff --git a/Francesco.Recipes.World/Controller/MediaFile/MediaFileController.cs b/Francesco.Recipes.World/Controller/MediaFile/MediaFileController.cs new file mode 100644 index 0000000..6cec16d --- /dev/null +++ b/Francesco.Recipes.World/Controller/MediaFile/MediaFileController.cs @@ -0,0 +1,6 @@ +namespace Francesco.Recipes.World.Controller.MediaFile +{ + public class MediaFileController + { + } +} diff --git a/Francesco.Recipes.World/Controller/Recipe/RecipeController.cs b/Francesco.Recipes.World/Controller/Recipe/RecipeController.cs new file mode 100644 index 0000000..5b21475 --- /dev/null +++ b/Francesco.Recipes.World/Controller/Recipe/RecipeController.cs @@ -0,0 +1,156 @@ +namespace Francesco.Recipes.World.Controller.Recipe +{ + using Francesco.Recipes.World.Models.BackendModels.Recipe; + using Francesco.Recipes.World.Repositories.Category; + using Francesco.Recipes.World.Repositories.Ingredient; + using Francesco.Recipes.World.Repositories.Recipe; + using Francesco.Recipes.World.Repositories.Unit; + using Francesco.Recipes.World.Views.Recipe; + using Microsoft.AspNetCore.Mvc; + using Microsoft.AspNetCore.Mvc.Rendering; + + [Route("categories/{categoryId}/Recipe")] + public class RecipeController : Controller + { + private readonly IRecipeRepository _recipeRepository; + private readonly IUnitRepository _unitRepository; + private readonly ICategoryRepository _categoryRepository; + private readonly IIngredientRepository _ingredientRepository; + + public RecipeController(IRecipeRepository recipeRepository, IUnitRepository unitRepository, ICategoryRepository categoryRepository, IIngredientRepository ingredientRepository) + { + _recipeRepository = recipeRepository; + _unitRepository = unitRepository; + _categoryRepository = categoryRepository; + _ingredientRepository = ingredientRepository; + } + + // GET: /Recipe/AddOrCreateIngredient + [HttpGet("{recipeId}/AddOrCreateIngredient")] + public async Task AddOrCreateIngredient() + { + var units = await _unitRepository.GetAllUnitsAsync(); + ViewBag.Units = new SelectList(units, "Id", "Name"); + return View(); + } + + // POST: /Recipe/AddOrCreateIngredient + [HttpPost("{recipeId}/AddOrCreateIngredient")] + [ValidateAntiForgeryToken] + public async Task AddOrCreateIngredient(Guid recipeId, string ingredientName, int quantity, Guid unitId) + { + if (quantity <= 0) + { + ModelState.AddModelError(nameof(quantity), "Die Menge muss größer als 0 sein."); + } + + if (!ModelState.IsValid) + { + var units = await _unitRepository.GetAllUnitsAsync(); + ViewBag.Units = new SelectList(units, "Id", "Name"); + return View(); + } + + await _recipeRepository.AddOrCreateIngredientToRecipeAsync(recipeId, ingredientName, quantity, unitId); + return RedirectToAction("Details", new { id = recipeId }); + } + + // GET: /categories/{categoryId}/Recipe/Create + [HttpGet("Create")] + public async Task Create(Guid categoryId) + { + var category = await _categoryRepository.GetCategoryByIdAsync(categoryId); + if (category == null) + { + return NotFound($"Kategorie mit ID {categoryId} wurde nicht gefunden."); + } + + ViewBag.CategoryName = category.Name; + return View(); + } + + // POST: /categories/{categoryId}/Recipe/Create + [HttpPost("Create")] + [ValidateAntiForgeryToken] + public async Task Create(Guid categoryId, string name, string description, Difficulty difficulty, int servings, TimeSpan preparationTime, TimeSpan cookingTime) + { + if (string.IsNullOrWhiteSpace(name)) + { + ModelState.AddModelError(nameof(name), "Name darf nicht leer sein."); + } + + if (servings <= 0) + { + ModelState.AddModelError(nameof(servings), "Anzahl der Portionen muss größer als 0 sein."); + } + + if (!ModelState.IsValid) + { + var category = await _categoryRepository.GetCategoryByIdAsync(categoryId); + if (category == null) + { + return NotFound($"Kategorie mit ID {categoryId} wurde nicht gefunden."); + } + + ViewBag.CategoryName = category.Name; + return View(); + } + + var categoryEntity = await _categoryRepository.GetCategoryByIdAsync(categoryId); + if (categoryEntity == null) + { + return NotFound($"Kategorie mit ID {categoryId} wurde nicht gefunden."); + } + + await _recipeRepository.CreateRecipeForCategoryAsync(categoryEntity, name, description, difficulty, servings, preparationTime, cookingTime); + return RedirectToAction("Details", "Category", new { id = categoryId }); + } + + // GET: /categories/{categoryId}/Recipe/{recipeId}/RemoveIngredient/{ingredientId} + [HttpGet("{recipeId}/RemoveIngredient/{ingredientId}")] + public async Task RemoveIngredient(Guid categoryId, Guid recipeId, Guid ingredientId) + { + var recipe = await _recipeRepository.GetRecipeAsync(recipeId); + var ingredient = await _ingredientRepository.GetIngredientByIdAsync(ingredientId); + + if (recipe == null || ingredient == null) + { + return NotFound("Recipe or Ingredient not found."); + } + + ViewBag.RecipeId = recipeId; + ViewBag.IngredientId = ingredientId; + ViewBag.CategoryId = categoryId; + ViewBag.IngredientName = ingredient.Name; + + return View(); + } + + // POST: /categories/{categoryId}/Recipe/{recipeId}/RemoveIngredient/{ingredientId} + [HttpPost("{recipeId}/RemoveIngredient/{ingredientId}")] + [ValidateAntiForgeryToken] + public async Task RemoveIngredientConfirmed(Guid categoryId, Guid recipeId, Guid ingredientId) + { + await _recipeRepository.RemoveIngredientFromRecipeAsync(recipeId, ingredientId); + TempData["SuccessMessage"] = "Ingredient removed successfully."; + return RedirectToAction("Details", new { id = recipeId }); + } + + // GET: /categories/{categoryId}/Recipe/FilterByDifficulty + [HttpGet("FilterByDifficulty")] + public async Task FilterByDifficulty(Difficulty? difficulty) + { + var viewModel = new FilterByDifficultyViewModel + { + SelectedDifficulty = difficulty, + }; + + if (difficulty.HasValue) + { + viewModel.Recipes = (await _recipeRepository.GetRecipesByDifficultyAsync(difficulty.Value)).ToList(); + } + + return View(viewModel); + } + } +} diff --git a/Francesco.Recipes.World/Controller/ShoppingList/ShoppingListController.cs b/Francesco.Recipes.World/Controller/ShoppingList/ShoppingListController.cs new file mode 100644 index 0000000..38fdf4b --- /dev/null +++ b/Francesco.Recipes.World/Controller/ShoppingList/ShoppingListController.cs @@ -0,0 +1,6 @@ +namespace Francesco.Recipes.World.Controller.ShoppingList +{ + public class ShoppingListController + { + } +} diff --git a/Francesco.Recipes.World/Controller/Unit/UnitController.cs b/Francesco.Recipes.World/Controller/Unit/UnitController.cs new file mode 100644 index 0000000..c0efcda --- /dev/null +++ b/Francesco.Recipes.World/Controller/Unit/UnitController.cs @@ -0,0 +1,6 @@ +namespace Francesco.Recipes.World.Controller.Unit +{ + public class UnitController + { + } +}