Ensure that all favorites are retrieved, and when a recipe is added to favorites, include the creation date and time.

This commit is contained in:
Francesco Lorenzo D'Amico
2025-06-02 09:18:38 +02:00
parent 9ab1efde0e
commit ba61988840
@@ -17,6 +17,8 @@
{ {
return await _context.Recipes return await _context.Recipes
.Where(r => r.IsFavorite) .Where(r => r.IsFavorite)
.Include(r => r.Favorit)
.Include(r => r.MediaFiles)
.ToListAsync(); .ToListAsync();
} }
@@ -28,10 +30,27 @@
public async Task AddFavoriteAsync(Guid recipeId) 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) if (recipe != null && !recipe.IsFavorite)
{ {
recipe.IsFavorite = true; 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(); await _context.SaveChangesAsync();
} }
} }