From ba0e35e52657b40212ebdfa6d26707705fef1a6d Mon Sep 17 00:00:00 2001 From: Francesco Lorenzo D'Amico Date: Tue, 27 May 2025 12:07:19 +0200 Subject: [PATCH] Delete unnecessary Methods and add some better Methods that improves the process --- .../ShoppingList/IShoppingListRepository.cs | 16 +-- .../ShoppingList/ShoppingListRepository.cs | 110 ++++++++++-------- 2 files changed, 68 insertions(+), 58 deletions(-) diff --git a/Francesco.Recipes.World/Repositories/ShoppingList/IShoppingListRepository.cs b/Francesco.Recipes.World/Repositories/ShoppingList/IShoppingListRepository.cs index 65e438a..63085e4 100644 --- a/Francesco.Recipes.World/Repositories/ShoppingList/IShoppingListRepository.cs +++ b/Francesco.Recipes.World/Repositories/ShoppingList/IShoppingListRepository.cs @@ -1,27 +1,19 @@ namespace Francesco.Recipes.World.Repositories.ShoppingList { - using Francesco.Recipes.World.Models.BackendModels.IngredientShoppingList; - using Francesco.Recipes.World.Models.BackendModels.Recipe; using Francesco.Recipes.World.Models.BackendModels.Shoppinglist; public interface IShoppingListRepository { Task AddIngredientsToShoppingListAsync(Guid recipeId, List ingredientIds); - Task> GetIngredientsOfRecipeInListAsync(Guid shoppingListRecipeId); - - Task UpdateIngredientCheckedAsync(Guid shoppingListRecipeId, Guid recipeIngredientId, bool isChecked); - Task RemoveRecipeIfEmptyAsync(Guid shoppingListRecipeId); - Task DeleteShoppingListAsync(Guid shoppingListId); - - Task GetRecipeByNameAndImageAsync(string recipeName, string imageFileName); - - Task GetByIdAsync(Guid shoppingListId); - Task CountAllRecipeShoppinglistsAsync(); Task> GetAllShoppingListsAsync(); + + Task RemoveIngredientsFromShoppingListAsync(List recipeIngredientShoppngListIds); + + Task RemoveRecipeFromShoppingListAsync(Guid recipeShoppingListId); } } diff --git a/Francesco.Recipes.World/Repositories/ShoppingList/ShoppingListRepository.cs b/Francesco.Recipes.World/Repositories/ShoppingList/ShoppingListRepository.cs index d101931..be8bba1 100644 --- a/Francesco.Recipes.World/Repositories/ShoppingList/ShoppingListRepository.cs +++ b/Francesco.Recipes.World/Repositories/ShoppingList/ShoppingListRepository.cs @@ -109,12 +109,6 @@ return shoppingList; } - public async Task GetByIdAsync(Guid shoppingListId) - { - return await GetShoppingListQuery() - .FirstOrDefaultAsync(sl => sl.Id == shoppingListId); - } - public async Task> GetAllShoppingListsAsync() { return await GetShoppingListQuery() @@ -122,63 +116,57 @@ .ToListAsync(); } - public async Task> GetIngredientsOfRecipeInListAsync(Guid shoppingListRecipeId) - { - return await _context.RecipeIngredientsShoppingLists - .Include(i => i.RecipeIngredient) - .ThenInclude(ri => ri.Ingredient) - .Include(i => i.RecipeIngredient.Unit) - .Where(i => i.RecipeShoppingList.Id == shoppingListRecipeId) - .ToListAsync(); - } - - public async Task UpdateIngredientCheckedAsync(Guid shoppingListRecipeId, Guid recipeIngredientId, bool isChecked) - { - var item = await _context.RecipeIngredientsShoppingLists - .FirstOrDefaultAsync(i => - i.RecipeShoppingList.Id == shoppingListRecipeId && - i.RecipeIngredient.Id == recipeIngredientId); - - if (item == null) - { - throw new Exception("Zutat nicht gefunden."); - } - - item.IsChecked = isChecked; - await _context.SaveChangesAsync(); - } - public async Task RemoveRecipeIfEmptyAsync(Guid shoppingListRecipeId) { var recipeEntry = await _context.RecipeShoppingLists .Include(r => r.SelectedIngredients) + .Include(r => r.ShoppingList) + .ThenInclude(sl => sl.RecipeShoppingList) .FirstOrDefaultAsync(r => r.Id == shoppingListRecipeId); if (recipeEntry != null && !recipeEntry.SelectedIngredients.Any()) { + var shoppingList = recipeEntry.ShoppingList; + _context.RecipeShoppingLists.Remove(recipeEntry); await _context.SaveChangesAsync(); + + if (shoppingList != null && (shoppingList.RecipeShoppingList == null || !shoppingList.RecipeShoppingList.Any())) + { + _context.ShoppingLists.Remove(shoppingList); + await _context.SaveChangesAsync(); + } } } - public async Task DeleteShoppingListAsync(Guid shoppingListId) + public async Task RemoveRecipeFromShoppingListAsync(Guid recipeShoppingListId) { - var list = await _context.ShoppingLists - .FirstOrDefaultAsync(sl => sl.Id == shoppingListId); + var recipeEntry = await _context.RecipeShoppingLists + .Include(r => r.SelectedIngredients) + .Include(r => r.ShoppingList) + .ThenInclude(sl => sl.RecipeShoppingList) + .FirstOrDefaultAsync(r => r.Id == recipeShoppingListId); - if (list != null) + if (recipeEntry != null) { - _context.ShoppingLists.Remove(list); - await _context.SaveChangesAsync(); - } - } + var shoppingList = recipeEntry.ShoppingList; - public async Task GetRecipeByNameAndImageAsync(string recipeName, string imageFileName) - { - return await _context.Recipes - .Where(r => r.Name == recipeName && r.MediaFiles.Any(mf => mf.FileName == imageFileName)) - .Include(r => r.MediaFiles.Where(mf => mf.FileName == imageFileName)) - .FirstOrDefaultAsync(); + _context.RecipeIngredientsShoppingLists.RemoveRange(recipeEntry.SelectedIngredients); + + _context.RecipeShoppingLists.Remove(recipeEntry); + await _context.SaveChangesAsync(); + + if (shoppingList != null) + { + await _context.Entry(shoppingList).Collection(sl => sl.RecipeShoppingList).LoadAsync(); + + if (!shoppingList.RecipeShoppingList.Any()) + { + _context.ShoppingLists.Remove(shoppingList); + await _context.SaveChangesAsync(); + } + } + } } public async Task CountAllRecipeShoppinglistsAsync() @@ -187,6 +175,36 @@ .CountAsync(); } + public async Task RemoveIngredientsFromShoppingListAsync(List recipeIngredientShoppingListIds) + { + if (recipeIngredientShoppingListIds == null || !recipeIngredientShoppingListIds.Any()) + { + throw new ArgumentNullException(nameof(recipeIngredientShoppingListIds)); + } + + var affectedRecipeShoppingListIds = await _context.RecipeIngredientsShoppingLists + .Where(risl => recipeIngredientShoppingListIds.Contains(risl.Id)) + .Select(risl => risl.RecipeShoppingList.Id) + .Distinct() + .ToListAsync(); + + foreach (var id in recipeIngredientShoppingListIds) + { + var entry = await _context.RecipeIngredientsShoppingLists.FindAsync(id); + if (entry != null) + { + _context.RecipeIngredientsShoppingLists.Remove(entry); + } + } + + await _context.SaveChangesAsync(); + + foreach (var recipeShoppingListId in affectedRecipeShoppingListIds) + { + await RemoveRecipeIfEmptyAsync(recipeShoppingListId); + } + } + private IQueryable GetShoppingListQuery() { return _context.ShoppingLists