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:
@@ -16,8 +16,10 @@
|
|||||||
public async Task<IEnumerable<Recipe>> GetFavoriteRecipesAsync()
|
public async Task<IEnumerable<Recipe>> GetFavoriteRecipesAsync()
|
||||||
{
|
{
|
||||||
return await _context.Recipes
|
return await _context.Recipes
|
||||||
.Where(r => r.IsFavorite)
|
.Where(r => r.IsFavorite)
|
||||||
.ToListAsync();
|
.Include(r => r.Favorit)
|
||||||
|
.Include(r => r.MediaFiles)
|
||||||
|
.ToListAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<bool> IsFavoriteAsync(Guid recipeId)
|
public async Task<bool> IsFavoriteAsync(Guid recipeId)
|
||||||
@@ -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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user