diff --git a/Francesco.Recipes.World/Controller/HomeController/HomeController.cs b/Francesco.Recipes.World/Controller/HomeController/HomeController.cs new file mode 100644 index 0000000..9bd9a0a --- /dev/null +++ b/Francesco.Recipes.World/Controller/HomeController/HomeController.cs @@ -0,0 +1,32 @@ +namespace Francesco.Recipes.World.Controller.HomeController +{ + using Francesco.Recipes.World.Repositories.Category; + using Francesco.Recipes.World.Repositories.Recipe; + using Microsoft.AspNetCore.Mvc; + + public class HomeController : Controller + { + private readonly IRecipeRepository _recipeRepository; + private readonly ICategoryRepository _categoryRepository; + + public HomeController(ICategoryRepository categoryRepository, IRecipeRepository recipeRepository) + { + _recipeRepository = recipeRepository; + _categoryRepository = categoryRepository; + } + + [HttpGet] + public async Task Index() + { + var categories = await _categoryRepository.GetAllCategoriesWithRecipesAsync(); + return View("Index", categories); + } + + [HttpGet("/Home/Search")] + public async Task Search(string term) + { + var recipes = await _recipeRepository.SearchInRecipesAndIngredients(term); + return PartialView("_SearchResultsPartial", recipes); + } + } +} diff --git a/Francesco.Recipes.World/Repositories/Recipe/IRecipeRepository.cs b/Francesco.Recipes.World/Repositories/Recipe/IRecipeRepository.cs index 03414e1..63c20af 100644 --- a/Francesco.Recipes.World/Repositories/Recipe/IRecipeRepository.cs +++ b/Francesco.Recipes.World/Repositories/Recipe/IRecipeRepository.cs @@ -13,10 +13,10 @@ Task RemoveIngredientFromRecipeAsync(Guid recipeId, Guid ingredientId); - Task> GetRecipesByNameAndIngredientAsync(string name, string ingredient); - Task> GetRecipesByDifficultyAsync(Difficulty? difficulty); Task CreateRecipeForCategoryAsync(Category category, string name, string description, Difficulty difficulty, int servings, TimeSpan preparationTime, TimeSpan cookingTime); + + Task> SearchInRecipesAndIngredients(string searchterm); } } diff --git a/Francesco.Recipes.World/Repositories/Recipe/RecipeRepository.cs b/Francesco.Recipes.World/Repositories/Recipe/RecipeRepository.cs index f71303f..c0f6b27 100644 --- a/Francesco.Recipes.World/Repositories/Recipe/RecipeRepository.cs +++ b/Francesco.Recipes.World/Repositories/Recipe/RecipeRepository.cs @@ -147,30 +147,21 @@ await _context.SaveChangesAsync(); } - public async Task> GetRecipesByNameAndIngredientAsync(string name, string ingredient) + public async Task> SearchInRecipesAndIngredients(string searchTerm) { - var query = _context.Recipes + var queryable = _context.Recipes .Include(r => r.RecipeIngredients) - .ThenInclude(ri => ri.Ingredient) + .ThenInclude(ri => ri.Ingredient) .AsQueryable(); - if (!string.IsNullOrWhiteSpace(name)) + if (!string.IsNullOrWhiteSpace(searchTerm)) { - query = query.Where(r => r.Name.Contains(name, StringComparison.OrdinalIgnoreCase)); + searchTerm = searchTerm.ToLower(); + queryable = queryable.Where(r => r.Name.ToLower().Contains(searchTerm) || + r.RecipeIngredients.Any(ri => ri.Ingredient.Name.ToLower().Contains(searchTerm))); } - if (!string.IsNullOrWhiteSpace(ingredient)) - { - var ingredientMatches = await _ingredientRepository.GetIngredientsByNameAsync(ingredient); - var ingredientIds = ingredientMatches.Select(i => i.Id).ToList(); - - if (ingredientIds.Any()) - { - query = query.Where(r => r.RecipeIngredients.Any(ri => ingredientIds.Contains(ri.Ingredient.Id))); - } - } - - return await query.ToListAsync(); + return await queryable.ToListAsync(); } public async Task> GetRecipesByDifficultyAsync(Difficulty? difficulty) diff --git a/Francesco.Recipes.World/Views/Home/Index.cshtml b/Francesco.Recipes.World/Views/Home/Index.cshtml new file mode 100644 index 0000000..8de8ea4 --- /dev/null +++ b/Francesco.Recipes.World/Views/Home/Index.cshtml @@ -0,0 +1,6 @@ +
+ +
+ +
+ diff --git a/Francesco.Recipes.World/Views/Home/_SearchResultsPartial.cshtml b/Francesco.Recipes.World/Views/Home/_SearchResultsPartial.cshtml new file mode 100644 index 0000000..4edbb64 --- /dev/null +++ b/Francesco.Recipes.World/Views/Home/_SearchResultsPartial.cshtml @@ -0,0 +1,58 @@ +@model IEnumerable + +@{ + if (!Model.Any()) +{ +

Keine Ergebnisse gefunden.

+} +else +{ +
+ @foreach (var recipe in Model) + { +
+
+
+ @{ + var mediaFile = recipe.MediaFiles.FirstOrDefault(); + + if (mediaFile?.Data != null) + { + @recipe.Name + } + else + { + Platzhalter + } + } +
+ +
+
+
@recipe.Name
+

+ + @recipe.PreparationTime.Hours h @recipe.PreparationTime.Minutes min +

+
+ +
+ +
+
+
+
+ } +
+} +} + diff --git a/Francesco.Recipes.World/Views/Shared/_Layout.cshtml b/Francesco.Recipes.World/Views/Shared/_Layout.cshtml index 9d445a9..fa98366 100644 --- a/Francesco.Recipes.World/Views/Shared/_Layout.cshtml +++ b/Francesco.Recipes.World/Views/Shared/_Layout.cshtml @@ -1,50 +1,79 @@  - - - @ViewData["Title"] - Francesco.Recipes.World - - - + + + @ViewData["Title"] - Francesco.Recipes.World + + + -
- -
-
-
- @RenderBody() -
-
+
+ +
+
+
+ @RenderBody() +
+
-
-
- © 2024 - Francesco.Recipes.World - Privacy -
-
- - - +
+
+ © 2024 - Francesco.Recipes.World - Privacy +
+
+ + + - @await RenderSectionAsync("Scripts", required: false) + + @await RenderSectionAsync("Scripts", required: false) +