Refactoring some Code and clean Code
This commit is contained in:
@@ -1,13 +1,8 @@
|
||||
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;
|
||||
|
||||
[ValidateAntiForgeryToken]
|
||||
[Route("Category")]
|
||||
|
||||
public class CategoryController : Controller
|
||||
{
|
||||
private readonly ICategoryRepository _categoryRepository;
|
||||
@@ -19,7 +14,7 @@
|
||||
|
||||
// GET: /Category
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<IEnumerable<Category>>> Index()
|
||||
public async Task<IActionResult> Index()
|
||||
{
|
||||
var categories = await _categoryRepository.GetAllCategoriesAsync();
|
||||
return View(categories);
|
||||
@@ -29,16 +24,16 @@
|
||||
[HttpGet("{id:guid}")]
|
||||
public async Task<IActionResult> Details(Guid id)
|
||||
{
|
||||
var category = await _categoryRepository.GetCategoryByIdAsync(id);
|
||||
return View(category);
|
||||
var category = await _categoryRepository.GetCategoryByIdAsync(id);
|
||||
return View(category);
|
||||
}
|
||||
|
||||
// GET: /Category/{id}/recipes
|
||||
[HttpGet("{id:guid}/recipes")]
|
||||
public async Task<ActionResult<IEnumerable<Recipe>>> GetRecipesByCategory(Guid id)
|
||||
public async Task<IActionResult> GetRecipesByCategory(Guid id)
|
||||
{
|
||||
var recipes = await _categoryRepository.GetRecipesByCategoryAsync(id);
|
||||
return View(recipes);
|
||||
var recipes = await _categoryRepository.GetRecipesByCategoryAsync(id);
|
||||
return Ok(recipes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,9 +4,7 @@
|
||||
using Francesco.Recipes.World.Repositories.MediaFile;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
[ValidateAntiForgeryToken]
|
||||
|
||||
[Route("categories/{categoryId}/Recipe")]
|
||||
[Route("Category/{categoryId}/Recipe")]
|
||||
public class MediaFileController : Controller
|
||||
{
|
||||
private readonly IMediaFileRepository _mediaFileRepository;
|
||||
@@ -20,6 +18,7 @@
|
||||
|
||||
// POST: /UploadImage
|
||||
[HttpPost("UploadImage")]
|
||||
[AutoValidateAntiforgeryToken]
|
||||
public async Task<IActionResult> UploadImage(Guid recipeId, IFormFile? mediaFile)
|
||||
{
|
||||
if (mediaFile is null)
|
||||
@@ -44,5 +43,32 @@
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
[HttpPost("ReplaceInstructionImage")]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> ReplaceInstructionImage(Guid instructionId, Guid mediaFileIdToReplace, IFormFile? newPhoto)
|
||||
{
|
||||
if (newPhoto is null)
|
||||
{
|
||||
return BadRequest("Photo is required.");
|
||||
}
|
||||
|
||||
using (var memoryStream = new MemoryStream())
|
||||
{
|
||||
await newPhoto.CopyToAsync(memoryStream);
|
||||
|
||||
var newMediaData = memoryStream.ToArray();
|
||||
|
||||
try
|
||||
{
|
||||
await _mediaFileRepository.ReplaceInstructionImageAsync(instructionId, mediaFileIdToReplace, newPhoto.FileName, newPhoto.ContentType, newMediaData);
|
||||
return Ok("Image replaced successfully.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return StatusCode(500, $"Internal server error: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
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;
|
||||
@@ -14,8 +13,6 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Rendering;
|
||||
|
||||
[ValidateAntiForgeryToken]
|
||||
[Route("Recipe")]
|
||||
public class RecipeController : Controller
|
||||
{
|
||||
private readonly IRecipeRepository _recipeRepository;
|
||||
@@ -24,13 +21,18 @@
|
||||
private readonly IIngredientRepository _ingredientRepository;
|
||||
private readonly IMediaFileRepository _mediaFileRepository;
|
||||
private readonly IInstructionRepository _instructionRepository;
|
||||
private readonly IFavoritRepository _favoritRepository;
|
||||
private readonly IFavoriteRepository _favoriteRepository;
|
||||
|
||||
[Display(Name = "Schwierigkeitsgrad")]
|
||||
[BindProperty(SupportsGet = true)]
|
||||
public Difficulty? SelectedDifficulty { get; set; }
|
||||
public IReadOnlyCollection<Recipe> Recipes { 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,
|
||||
IMediaFileRepository mediaFileRepository,
|
||||
IInstructionRepository instructionRepository,
|
||||
IFavoriteRepository favoriteRepository)
|
||||
{
|
||||
_recipeRepository = recipeRepository;
|
||||
_unitRepository = unitRepository;
|
||||
@@ -39,11 +41,9 @@
|
||||
Recipes = new List<Recipe>();
|
||||
_mediaFileRepository = mediaFileRepository;
|
||||
_instructionRepository = instructionRepository;
|
||||
_favoritRepository = favoritRepository;
|
||||
_favoriteRepository = favoriteRepository;
|
||||
}
|
||||
|
||||
public IReadOnlyCollection<Recipe> Recipes { get; set; }
|
||||
|
||||
// GET: /Recipe/{recipeId}/AddOrCreateIngredient
|
||||
[HttpGet("{recipeId}/AddOrCreateIngredient")]
|
||||
public async Task<IActionResult> AddOrCreateIngredient(Guid recipeId)
|
||||
@@ -71,24 +71,20 @@
|
||||
[HttpGet("CategoryRecipes")]
|
||||
public async Task<IActionResult> CategoryRecipes()
|
||||
{
|
||||
var categories = await _categoryRepository.GetAllCategoriesAsync();
|
||||
var viewModel = new List<CategoryRecipesViewModel>();
|
||||
var categories = await _categoryRepository.GetAllCategoriesWithRecipesAsync();
|
||||
|
||||
foreach (var category in categories)
|
||||
var viewModel = categories.Select(c => new CategoryRecipesViewModel
|
||||
{
|
||||
var recipes = await _categoryRepository.GetRecipesByCategoryAsync(category.Id);
|
||||
viewModel.Add(new CategoryRecipesViewModel
|
||||
{
|
||||
Category = category,
|
||||
Recipes = recipes,
|
||||
});
|
||||
}
|
||||
Category = c,
|
||||
Recipes = c.Recipes,
|
||||
}).ToList();
|
||||
|
||||
return View(viewModel);
|
||||
}
|
||||
|
||||
// POST: /Recipe/{recipeId}/AddOrCreateIngredient
|
||||
[HttpPost("{recipeId}/AddOrCreateIngredient")]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> AddOrCreateIngredient(Guid recipeId, string ingredientName, int quantity, Guid unitId)
|
||||
{
|
||||
if (quantity <= 0)
|
||||
@@ -123,6 +119,7 @@
|
||||
|
||||
// POST: /Recipe/Create/{categoryId}
|
||||
[HttpPost("Create/{categoryId}")]
|
||||
[AutoValidateAntiforgeryToken]
|
||||
public async Task<IActionResult> Create(Guid categoryId, string name, string description, Difficulty difficulty, int servings, TimeSpan preparationTime, TimeSpan cookingTime, IFormFile? photo)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(name))
|
||||
@@ -153,6 +150,7 @@
|
||||
return NotFound($"Kategorie mit ID {categoryId} wurde nicht gefunden.");
|
||||
}
|
||||
|
||||
await _recipeRepository.CreateRecipeForCategoryAsync(categoryEntity, name, description, difficulty, servings, preparationTime, cookingTime);
|
||||
var recipe = await _recipeRepository.CreateRecipeForCategoryAsync(categoryEntity, name, description, difficulty, servings, preparationTime, cookingTime);
|
||||
if (photo != null)
|
||||
{
|
||||
@@ -176,13 +174,13 @@
|
||||
|
||||
ViewBag.RecipeId = recipeId;
|
||||
ViewBag.IngredientId = ingredientId;
|
||||
ViewBag.IngredientName = ingredient.Name;
|
||||
|
||||
return View();
|
||||
}
|
||||
|
||||
// POST: /Recipe/{recipeId}/RemoveIngredient/{ingredientId}
|
||||
[HttpPost("{recipeId}/RemoveIngredient/{ingredientId}")]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> RemoveIngredientConfirmed(Guid recipeId, Guid ingredientId)
|
||||
{
|
||||
await _recipeRepository.RemoveIngredientFromRecipeAsync(recipeId, ingredientId);
|
||||
@@ -219,6 +217,7 @@
|
||||
|
||||
// POST: /Recipe/{recipeId}/AddInstruction
|
||||
[HttpPost("{recipeId}/AddInstruction")]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> AddInstruction(Guid recipeId, string description, int number)
|
||||
{
|
||||
try
|
||||
@@ -239,23 +238,25 @@
|
||||
[HttpGet("Favorites")]
|
||||
public async Task<IActionResult> Favorites()
|
||||
{
|
||||
var favoriteRecipes = await _favoritRepository.GetFavoriteRecipesAsync();
|
||||
var favoriteRecipes = await _favoriteRepository.GetFavoriteRecipesAsync();
|
||||
return View(favoriteRecipes);
|
||||
}
|
||||
|
||||
// POST: /Recipe/AddFavorite
|
||||
[HttpPost("AddFavorite")]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> AddFavorite(Guid recipeId)
|
||||
{
|
||||
await _favoritRepository.AddFavoriteAsync(recipeId);
|
||||
await _favoriteRepository.AddFavoriteAsync(recipeId);
|
||||
return RedirectToAction("Details", new { recipeId });
|
||||
}
|
||||
|
||||
// POST: /Recipe/RemoveFavorite
|
||||
[HttpPost("RemoveFavorite")]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> RemoveFavorite(Guid recipeId)
|
||||
{
|
||||
await _favoritRepository.RemoveFavoriteAsync(recipeId);
|
||||
await _favoriteRepository.RemoveFavoriteAsync(recipeId);
|
||||
return RedirectToAction("Details", new { recipeId });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
namespace Francesco.Recipes.World.Controllers
|
||||
{
|
||||
using Francesco.Recipes.World.Data;
|
||||
using Francesco.Recipes.World.Models;
|
||||
using Francesco.Recipes.World.Repositories.ShoppingList;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
[Route("ShoppingList")]
|
||||
public class ShoppingListController : Controller
|
||||
{
|
||||
private readonly IShoppingListRepository _shoppingListRepository;
|
||||
@@ -18,7 +18,8 @@
|
||||
}
|
||||
|
||||
[HttpPost("CreateOrAddIngredients")]
|
||||
public async Task<IActionResult> CreateOrAddIngredients([FromBody] CreateOrAddIngredientsRequest request)
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> CreateOrAddIngredients([FromBody] CreateOrAddIngredientRequestModel request)
|
||||
{
|
||||
if (request == null || request.IngredientIds == null || !request.IngredientIds.Any())
|
||||
{
|
||||
@@ -39,12 +40,5 @@
|
||||
|
||||
return Json(new { shoppingListId = shoppingList.Id });
|
||||
}
|
||||
|
||||
public class CreateOrAddIngredientsRequest
|
||||
{
|
||||
public Guid RecipeId { get; set; }
|
||||
|
||||
public List<Guid> IngredientIds { get; set; } = new ();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user