Revert "add CategoryController with endpoints"

This reverts commit 36bca4d658.
This commit is contained in:
franc
2025-04-08 15:23:57 +02:00
parent 36bca4d658
commit 928743303c
24 changed files with 142 additions and 1535 deletions
@@ -5,7 +5,6 @@
using Francesco.Recipes.World.Repositories.Category;
using Microsoft.AspNetCore.Mvc;
[ValidateAntiForgeryToken]
[Route("Category")]
public class CategoryController : Controller
@@ -38,7 +37,7 @@
public async Task<ActionResult<IEnumerable<Recipe>>> GetRecipesByCategory(Guid id)
{
var recipes = await _categoryRepository.GetRecipesByCategoryAsync(id);
return View(recipes);
return Ok(recipes);
}
}
}
@@ -1,48 +1,6 @@
namespace Francesco.Recipes.World.Controller.MediaFile
{
using Francesco.Recipes.World.Data;
using Francesco.Recipes.World.Repositories.MediaFile;
using Microsoft.AspNetCore.Mvc;
[ValidateAntiForgeryToken]
[Route("categories/{categoryId}/Recipe")]
public class MediaFileController : Controller
public class MediaFileController
{
private readonly IMediaFileRepository _mediaFileRepository;
private readonly FrancescosRecipesWorldDbContext _context;
public MediaFileController(IMediaFileRepository mediaFileRepository, FrancescosRecipesWorldDbContext context)
{
_mediaFileRepository = mediaFileRepository;
_context = context;
}
// POST: /UploadImage
[HttpPost("UploadImage")]
public async Task<IActionResult> UploadImage(Guid recipeId, IFormFile? mediaFile)
{
if (mediaFile is null)
{
return BadRequest("Photo is required.");
}
try
{
await _mediaFileRepository.UploadRecipeMediaAsync(recipeId, mediaFile);
return Ok("Image uploaded successfully.");
}
catch (Exception ex)
{
return StatusCode(500, $"Internal server error: {ex.Message}");
}
}
// GET: /UploadImage
[HttpGet("UploadImage")]
public IActionResult UploadImageView()
{
return View();
}
}
}
@@ -1,94 +1,43 @@
namespace Francesco.Recipes.World.Controller.Recipe
{
using System.ComponentModel.DataAnnotations;
using Francesco.Recipes.World.Models.BackendModels.Recipe;
using Francesco.Recipes.World.Repositories.Category;
using Francesco.Recipes.World.Repositories.Favorit;
using Francesco.Recipes.World.Repositories.Ingredient;
using Francesco.Recipes.World.Repositories.Instruction;
using Francesco.Recipes.World.Repositories.MediaFile;
using Francesco.Recipes.World.Repositories.Recipe;
using Francesco.Recipes.World.Repositories.Unit;
using Francesco.Recipes.World.Views.Category;
using Francesco.Recipes.World.Views.Recipe;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
[ValidateAntiForgeryToken]
[Route("Recipe")]
[Route("categories/{categoryId}/Recipe")]
public class RecipeController : Controller
{
private readonly IRecipeRepository _recipeRepository;
private readonly IUnitRepository _unitRepository;
private readonly ICategoryRepository _categoryRepository;
private readonly IIngredientRepository _ingredientRepository;
private readonly IMediaFileRepository _mediaFileRepository;
private readonly IInstructionRepository _instructionRepository;
private readonly IFavoritRepository _favoritRepository;
[Display(Name = "Schwierigkeitsgrad")]
[BindProperty(SupportsGet = true)]
public Difficulty? SelectedDifficulty { get; set; }
public RecipeController(IRecipeRepository recipeRepository, IUnitRepository unitRepository, ICategoryRepository categoryRepository, IIngredientRepository ingredientRepository, IMediaFileRepository mediaFileRepository, IInstructionRepository instructionRepository, IFavoritRepository favoritRepository)
public RecipeController(IRecipeRepository recipeRepository, IUnitRepository unitRepository, ICategoryRepository categoryRepository, IIngredientRepository ingredientRepository)
{
_recipeRepository = recipeRepository;
_unitRepository = unitRepository;
_categoryRepository = categoryRepository;
_ingredientRepository = ingredientRepository;
Recipes = new List<Recipe>();
_mediaFileRepository = mediaFileRepository;
_instructionRepository = instructionRepository;
_favoritRepository = favoritRepository;
}
public IReadOnlyCollection<Recipe> Recipes { get; set; }
// GET: /Recipe/{recipeId}/AddOrCreateIngredient
// GET: /Recipe/AddOrCreateIngredient
[HttpGet("{recipeId}/AddOrCreateIngredient")]
public async Task<IActionResult> AddOrCreateIngredient(Guid recipeId)
public async Task<IActionResult> AddOrCreateIngredient()
{
var units = await _unitRepository.GetAllUnitsAsync();
ViewBag.Units = new SelectList(units, "Id", "Name");
ViewBag.RecipeId = recipeId;
return View();
}
// GET: /Recipe/Details/{recipeId}
[HttpGet("Details/{recipeId}")]
public async Task<IActionResult> Details(Guid recipeId)
{
var recipe = await _recipeRepository.GetRecipeByIdAsync(recipeId);
if (recipe == null)
{
return NotFound("Recipe not found.");
}
return View(recipe);
}
// GET: /Recipe/CategoryRecipes
[HttpGet("CategoryRecipes")]
public async Task<IActionResult> CategoryRecipes()
{
var categories = await _categoryRepository.GetAllCategoriesAsync();
var viewModel = new List<CategoryRecipesViewModel>();
foreach (var category in categories)
{
var recipes = await _categoryRepository.GetRecipesByCategoryAsync(category.Id);
viewModel.Add(new CategoryRecipesViewModel
{
Category = category,
Recipes = recipes,
});
}
return View(viewModel);
}
// POST: /Recipe/{recipeId}/AddOrCreateIngredient
// POST: /Recipe/AddOrCreateIngredient
[HttpPost("{recipeId}/AddOrCreateIngredient")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> AddOrCreateIngredient(Guid recipeId, string ingredientName, int quantity, Guid unitId)
{
if (quantity <= 0)
@@ -107,8 +56,8 @@
return RedirectToAction("Details", new { id = recipeId });
}
// GET: /Recipe/Create/{categoryId}
[HttpGet("Create/{categoryId}")]
// GET: /categories/{categoryId}/Recipe/Create
[HttpGet("Create")]
public async Task<IActionResult> Create(Guid categoryId)
{
var category = await _categoryRepository.GetCategoryByIdAsync(categoryId);
@@ -121,9 +70,10 @@
return View();
}
// POST: /Recipe/Create/{categoryId}
[HttpPost("Create/{categoryId}")]
public async Task<IActionResult> Create(Guid categoryId, string name, string description, Difficulty difficulty, int servings, TimeSpan preparationTime, TimeSpan cookingTime, IFormFile? photo)
// POST: /categories/{categoryId}/Recipe/Create
[HttpPost("Create")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(Guid categoryId, string name, string description, Difficulty difficulty, int servings, TimeSpan preparationTime, TimeSpan cookingTime)
{
if (string.IsNullOrWhiteSpace(name))
{
@@ -153,18 +103,13 @@
return NotFound($"Kategorie mit ID {categoryId} wurde nicht gefunden.");
}
var recipe = await _recipeRepository.CreateRecipeForCategoryAsync(categoryEntity, name, description, difficulty, servings, preparationTime, cookingTime);
if (photo != null)
{
await _mediaFileRepository.UploadRecipeMediaAsync(recipe.Id, photo);
}
await _recipeRepository.CreateRecipeForCategoryAsync(categoryEntity, name, description, difficulty, servings, preparationTime, cookingTime);
return RedirectToAction("Details", "Category", new { id = categoryId });
}
// GET: /Recipe/{recipeId}/RemoveIngredient/{ingredientId}
// GET: /categories/{categoryId}/Recipe/{recipeId}/RemoveIngredient/{ingredientId}
[HttpGet("{recipeId}/RemoveIngredient/{ingredientId}")]
public async Task<IActionResult> RemoveIngredient(Guid recipeId, Guid ingredientId)
public async Task<IActionResult> RemoveIngredient(Guid categoryId, Guid recipeId, Guid ingredientId)
{
var recipe = await _recipeRepository.GetRecipeAsync(recipeId);
var ingredient = await _ingredientRepository.GetIngredientByIdAsync(ingredientId);
@@ -176,87 +121,28 @@
ViewBag.RecipeId = recipeId;
ViewBag.IngredientId = ingredientId;
ViewBag.CategoryId = categoryId;
ViewBag.IngredientName = ingredient.Name;
return View();
}
// POST: /Recipe/{recipeId}/RemoveIngredient/{ingredientId}
// POST: /categories/{categoryId}/Recipe/{recipeId}/RemoveIngredient/{ingredientId}
[HttpPost("{recipeId}/RemoveIngredient/{ingredientId}")]
public async Task<IActionResult> RemoveIngredientConfirmed(Guid recipeId, Guid ingredientId)
[ValidateAntiForgeryToken]
public async Task<IActionResult> RemoveIngredientConfirmed(Guid categoryId, Guid recipeId, Guid ingredientId)
{
await _recipeRepository.RemoveIngredientFromRecipeAsync(recipeId, ingredientId);
TempData["SuccessMessage"] = "Ingredient removed successfully.";
return RedirectToAction("Details", new { id = recipeId });
}
// GET: /Recipe/FilterByDifficulty
// GET: /categories/{categoryId}/Recipe/FilterByDifficulty
[HttpGet("FilterByDifficulty")]
public async Task<IActionResult> FilterByDifficulty(Difficulty? selectedDifficulty)
public async Task<IActionResult> FilterByDifficulty(Difficulty? difficulty)
{
var recipes = await _recipeRepository.GetRecipesByDifficultyAsync(selectedDifficulty);
var viewModel = new FilterByDifficultyViewModel
{
SelectedDifficulty = selectedDifficulty,
Recipes = recipes,
};
return View(viewModel);
}
// GET: /Recipe/{recipeId}/AddInstruction
[HttpGet("{recipeId}/AddInstruction")]
public async Task<IActionResult> AddInstruction(Guid recipeId)
{
var recipe = await _recipeRepository.GetRecipeByIdAsync(recipeId);
if (recipe == null)
{
return NotFound("Recipe not found.");
}
ViewBag.RecipeId = recipeId;
return View(recipe);
}
// POST: /Recipe/{recipeId}/AddInstruction
[HttpPost("{recipeId}/AddInstruction")]
public async Task<IActionResult> AddInstruction(Guid recipeId, string description, int number)
{
try
{
await _instructionRepository.CreateInstructionToRecipeAsync(recipeId, description, number);
return RedirectToAction("AddInstruction", new { recipeId });
}
catch (Exception ex)
{
ModelState.AddModelError(string.Empty, ex.Message);
var recipe = await _recipeRepository.GetRecipeByIdAsync(recipeId);
ViewBag.RecipeId = recipeId;
return View(recipe);
}
}
// GET: /Recipe/Favorites
[HttpGet("Favorites")]
public async Task<IActionResult> Favorites()
{
var favoriteRecipes = await _favoritRepository.GetFavoriteRecipesAsync();
return View(favoriteRecipes);
}
// POST: /Recipe/AddFavorite
[HttpPost("AddFavorite")]
public async Task<IActionResult> AddFavorite(Guid recipeId)
{
await _favoritRepository.AddFavoriteAsync(recipeId);
return RedirectToAction("Details", new { recipeId });
}
// POST: /Recipe/RemoveFavorite
[HttpPost("RemoveFavorite")]
public async Task<IActionResult> RemoveFavorite(Guid recipeId)
{
await _favoritRepository.RemoveFavoriteAsync(recipeId);
return RedirectToAction("Details", new { recipeId });
Recipes = await _recipeRepository.GetRecipesByDifficultyAsync(difficulty);
return View(Recipes);
}
}
}
@@ -1,50 +1,6 @@
namespace Francesco.Recipes.World.Controllers
namespace Francesco.Recipes.World.Controller.ShoppingList
{
using Francesco.Recipes.World.Data;
using Francesco.Recipes.World.Repositories.ShoppingList;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
[Route("ShoppingList")]
public class ShoppingListController : Controller
public class ShoppingListController
{
private readonly IShoppingListRepository _shoppingListRepository;
private readonly FrancescosRecipesWorldDbContext _context;
public ShoppingListController(IShoppingListRepository shoppingListRepository, FrancescosRecipesWorldDbContext context)
{
_shoppingListRepository = shoppingListRepository;
_context = context;
}
[HttpPost("CreateOrAddIngredients")]
public async Task<IActionResult> CreateOrAddIngredients([FromBody] CreateOrAddIngredientsRequest request)
{
if (request == null || request.IngredientIds == null || !request.IngredientIds.Any())
{
return BadRequest("No ingredients provided.");
}
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)
{
return BadRequest("Error creating shopping list.");
}
return Json(new { shoppingListId = shoppingList.Id });
}
public class CreateOrAddIngredientsRequest
{
public Guid RecipeId { get; set; }
public List<Guid> IngredientIds { get; set; } = new ();
}
}
}