From 3ae229b4f342faee6b60c3ede238798092334767 Mon Sep 17 00:00:00 2001 From: Francesco Lorenzo D'Amico Date: Tue, 3 Jun 2025 09:19:44 +0200 Subject: [PATCH] ChangeFavorit to Favorite and change some method logic --- .../Repositories/Favorit/FavoritRepository.cs | 41 ++++++++++--------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/Francesco.Recipes.World/Repositories/Favorit/FavoritRepository.cs b/Francesco.Recipes.World/Repositories/Favorit/FavoritRepository.cs index 9993adc..5ca4fa3 100644 --- a/Francesco.Recipes.World/Repositories/Favorit/FavoritRepository.cs +++ b/Francesco.Recipes.World/Repositories/Favorit/FavoritRepository.cs @@ -17,7 +17,7 @@ { return await _context.Recipes .Where(r => r.IsFavorite) - .Include(r => r.Favorit) + .Include(r => r.Favorite) .Include(r => r.MediaFiles) .ToListAsync(); } @@ -31,28 +31,31 @@ public async Task AddFavoriteAsync(Guid recipeId) { var recipe = await _context.Recipes - .Include(r => r.Favorit) + .Include(r => r.Favorite) .FirstOrDefaultAsync(r => r.Id == recipeId); - if (recipe != null && !recipe.IsFavorite) + if (recipe == null) { - recipe.IsFavorite = true; - - if (recipe.Favorit == null || recipe.Favorit.Id == Guid.Empty) - { - recipe.Favorit = new Models.BackendModels.Favorit.Favorit - { - Id = Guid.NewGuid(), - CreatedAt = DateTime.Now, - }; - } - else - { - recipe.Favorit.CreatedAt = DateTime.Now; - } - - await _context.SaveChangesAsync(); + throw new InvalidOperationException("Recipe not found."); } + + if (recipe.IsFavorite) + { + throw new InvalidOperationException("Recipe is already a favorite."); + } + + recipe.IsFavorite = true; + + if (recipe.Favorite == null || recipe.Favorite.Id == Guid.Empty) + { + recipe.Favorite = new Models.BackendModels.Favorit.Favorit + { + Id = Guid.NewGuid(), + CreatedAt = DateTime.Now, + }; + } + + await _context.SaveChangesAsync(); } public async Task RemoveFavoriteAsync(Guid recipeId)