Simplify controllers: use repo view models, streamline favorites, add HTMX redirects
This commit is contained in:
@@ -5,7 +5,6 @@
|
|||||||
using Francesco.Recipes.World.Repositories.Favorit;
|
using Francesco.Recipes.World.Repositories.Favorit;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
[Route("Favorite")]
|
|
||||||
public class FavoriteController : Controller
|
public class FavoriteController : Controller
|
||||||
{
|
{
|
||||||
private readonly IFavoriteRepository _favoriteRepository;
|
private readonly IFavoriteRepository _favoriteRepository;
|
||||||
|
|||||||
@@ -2,7 +2,6 @@
|
|||||||
{
|
{
|
||||||
using Francesco.Recipes.World.Repositories.Category;
|
using Francesco.Recipes.World.Repositories.Category;
|
||||||
using Francesco.Recipes.World.Repositories.Recipe;
|
using Francesco.Recipes.World.Repositories.Recipe;
|
||||||
using Francesco.Recipes.World.Views.Category;
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
public class HomeController : Controller
|
public class HomeController : Controller
|
||||||
@@ -19,15 +18,8 @@
|
|||||||
[HttpGet]
|
[HttpGet]
|
||||||
public async Task<IActionResult> Index()
|
public async Task<IActionResult> Index()
|
||||||
{
|
{
|
||||||
var categories = await _categoryRepository.GetAllCategoriesWithRecipesAsync();
|
var categoriesWithRecipes = await _categoryRepository.GetAllCategoriesWithRecipesViewModelAsync();
|
||||||
|
return View(categoriesWithRecipes);
|
||||||
var viewModel = categories.Select(category => new CategoryRecipesViewModel
|
|
||||||
{
|
|
||||||
Category = category,
|
|
||||||
Recipes = category.Recipes,
|
|
||||||
});
|
|
||||||
|
|
||||||
return View("Index", viewModel);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("/Home/Search")]
|
[HttpGet("/Home/Search")]
|
||||||
|
|||||||
@@ -12,7 +12,6 @@
|
|||||||
using Francesco.Recipes.World.Repositories.MediaFile;
|
using Francesco.Recipes.World.Repositories.MediaFile;
|
||||||
using Francesco.Recipes.World.Repositories.Recipe;
|
using Francesco.Recipes.World.Repositories.Recipe;
|
||||||
using Francesco.Recipes.World.Repositories.Unit;
|
using Francesco.Recipes.World.Repositories.Unit;
|
||||||
using Francesco.Recipes.World.Views.Category;
|
|
||||||
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;
|
||||||
@@ -91,15 +90,8 @@
|
|||||||
[HttpGet("CategoryRecipes")]
|
[HttpGet("CategoryRecipes")]
|
||||||
public async Task<IActionResult> CategoryRecipes()
|
public async Task<IActionResult> CategoryRecipes()
|
||||||
{
|
{
|
||||||
var categories = await _categoryRepository.GetAllCategoriesWithRecipesAsync();
|
var categoryViewModels = await _categoryRepository.GetAllCategoriesWithRecipesAsync();
|
||||||
|
return View(categoryViewModels);
|
||||||
var viewModel = categories.Select(c => new CategoryRecipesViewModel
|
|
||||||
{
|
|
||||||
Category = c,
|
|
||||||
Recipes = c.Recipes,
|
|
||||||
}).ToList();
|
|
||||||
|
|
||||||
return View(viewModel);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// POST: /Recipe/{recipeId}/AddOrCreateIngredient
|
// POST: /Recipe/{recipeId}/AddOrCreateIngredient
|
||||||
@@ -424,34 +416,35 @@
|
|||||||
return View(favoriteRecipes);
|
return View(favoriteRecipes);
|
||||||
}
|
}
|
||||||
|
|
||||||
// POST: /Recipe/AddFavorite
|
|
||||||
[HttpPost("AddFavorite")]
|
[HttpPost("AddFavorite")]
|
||||||
[ValidateAntiForgeryToken]
|
[ValidateAntiForgeryToken]
|
||||||
public async Task<IActionResult> AddFavorite(Guid recipeId)
|
public async Task<IActionResult> AddFavorite(Guid recipeId)
|
||||||
{
|
{
|
||||||
await _favoriteRepository.AddFavoriteAsync(recipeId);
|
await _favoriteRepository.AddFavoriteAsync(recipeId);
|
||||||
var recipe = await _recipeRepository.GetRecipeByIdAsync(recipeId);
|
|
||||||
if (recipe == null)
|
// We only need to know the Id and IsFavorite status
|
||||||
|
var viewModel = new FavoritButtonViewModel
|
||||||
{
|
{
|
||||||
return NotFound("Recipe not found.");
|
Id = recipeId,
|
||||||
|
IsFavorite = true,
|
||||||
|
};
|
||||||
|
|
||||||
|
return PartialView("_FavoriteButton", viewModel);
|
||||||
}
|
}
|
||||||
|
|
||||||
return PartialView("_FavoriteButton", recipe);
|
|
||||||
}
|
|
||||||
|
|
||||||
// POST: /Recipe/RemoveFavorite
|
|
||||||
[HttpPost("RemoveFavorite")]
|
[HttpPost("RemoveFavorite")]
|
||||||
[ValidateAntiForgeryToken]
|
[ValidateAntiForgeryToken]
|
||||||
public async Task<IActionResult> RemoveFavorite(Guid recipeId)
|
public async Task<IActionResult> RemoveFavorite(Guid recipeId)
|
||||||
{
|
{
|
||||||
await _favoriteRepository.RemoveFavoriteAsync(recipeId);
|
await _favoriteRepository.RemoveFavoriteAsync(recipeId);
|
||||||
var recipe = await _recipeRepository.GetRecipeByIdAsync(recipeId);
|
|
||||||
if (recipe == null)
|
|
||||||
{
|
|
||||||
return NotFound("Recipe not found.");
|
|
||||||
}
|
|
||||||
|
|
||||||
return PartialView("_FavoriteButton", recipe);
|
var viewModel = new FavoritButtonViewModel
|
||||||
|
{
|
||||||
|
Id = recipeId,
|
||||||
|
IsFavorite = false,
|
||||||
|
};
|
||||||
|
|
||||||
|
return PartialView("_FavoriteButton", viewModel);
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET: /Recipe/{recipeId}/GetIngredients
|
// GET: /Recipe/{recipeId}/GetIngredients
|
||||||
@@ -497,7 +490,6 @@
|
|||||||
return View(recipe);
|
return View(recipe);
|
||||||
}
|
}
|
||||||
|
|
||||||
// DELETE: /Recipe/{recipeId}/Delete
|
|
||||||
[HttpDelete("{recipeId}/Delete")]
|
[HttpDelete("{recipeId}/Delete")]
|
||||||
[ValidateAntiForgeryToken]
|
[ValidateAntiForgeryToken]
|
||||||
public async Task<IActionResult> DeleteConfirmed(Guid recipeId)
|
public async Task<IActionResult> DeleteConfirmed(Guid recipeId)
|
||||||
@@ -506,12 +498,24 @@
|
|||||||
|
|
||||||
if (deleted)
|
if (deleted)
|
||||||
{
|
{
|
||||||
TempData["SuccessMessage"] = "Rezept wurde erfolgreich gelöscht.";
|
if (Request.Headers.ContainsKey("HX-Request"))
|
||||||
|
{
|
||||||
|
Response.Headers["HX-Redirect"] = Url.Action("Index", "Home");
|
||||||
|
return Ok();
|
||||||
|
}
|
||||||
|
|
||||||
return RedirectToAction("Index", "Home");
|
return RedirectToAction("Index", "Home");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
TempData["ErrorMessage"] = "Rezept nicht gefunden oder konnte nicht gelöscht werden.";
|
TempData["ErrorMessage"] = "Rezept nicht gefunden oder konnte nicht gelöscht werden.";
|
||||||
|
|
||||||
|
if (Request.Headers.ContainsKey("HX-Request"))
|
||||||
|
{
|
||||||
|
Response.Headers["HX-Redirect"] = Url.Action("Details", new { recipeId });
|
||||||
|
return Ok();
|
||||||
|
}
|
||||||
|
|
||||||
return RedirectToAction("Details", new { recipeId });
|
return RedirectToAction("Details", new { recipeId });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user