From ba619888403580b5974cd2e92cab074e196f3807 Mon Sep 17 00:00:00 2001 From: Francesco Lorenzo D'Amico Date: Mon, 2 Jun 2025 09:18:38 +0200 Subject: [PATCH] Ensure that all favorites are retrieved, and when a recipe is added to favorites, include the creation date and time. --- .../Repositories/Favorit/FavoritRepository.cs | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/Francesco.Recipes.World/Repositories/Favorit/FavoritRepository.cs b/Francesco.Recipes.World/Repositories/Favorit/FavoritRepository.cs index eb8cae2..9993adc 100644 --- a/Francesco.Recipes.World/Repositories/Favorit/FavoritRepository.cs +++ b/Francesco.Recipes.World/Repositories/Favorit/FavoritRepository.cs @@ -16,8 +16,10 @@ public async Task> GetFavoriteRecipesAsync() { return await _context.Recipes - .Where(r => r.IsFavorite) - .ToListAsync(); + .Where(r => r.IsFavorite) + .Include(r => r.Favorit) + .Include(r => r.MediaFiles) + .ToListAsync(); } public async Task IsFavoriteAsync(Guid recipeId) @@ -28,10 +30,27 @@ public async Task AddFavoriteAsync(Guid recipeId) { - var recipe = await _context.Recipes.FindAsync(recipeId); + var recipe = await _context.Recipes + .Include(r => r.Favorit) + .FirstOrDefaultAsync(r => r.Id == recipeId); + if (recipe != null && !recipe.IsFavorite) { 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(); } }