diff --git a/WF_EF_Api/Contextlib/DbFavoriteManager.cs b/WF_EF_Api/Contextlib/DbFavoriteManager.cs index 1b84dd2..0fbdd59 100644 --- a/WF_EF_Api/Contextlib/DbFavoriteManager.cs +++ b/WF_EF_Api/Contextlib/DbFavoriteManager.cs @@ -27,17 +27,15 @@ namespace Contextlib .Include(q => q.Favorite) .FirstOrDefaultAsync(q => q.Id == quoteid); - if (quote == null) - { - throw new ArgumentException("Quote not exist", nameof(quoteid)); - } var user = await _context.users // collection des commentaires est chargée .Include(u => u.Favorite) .FirstOrDefaultAsync(q => q.Id == userId); - if (user == null) + + if (quote == null && user == null) { - throw new ArgumentException("User not exist", nameof(userId)); + throw new ArgumentException("Quote or User not exist", nameof(quoteid)); } + var fav = new Favorite { @@ -64,15 +62,13 @@ namespace Contextlib public async Task RemoveAllFavoriteForQuote(int quoteId) { - var fav = await _context.quotes.Where(item => item.Id==quoteId) - .Include(q => q.Source).Include(q => q.Character).ThenInclude(c => c.Images).Include(q => q.Favorite) - .ToListAsync(); + var fav = _context.favorites.Where(item => item.IdQuote == quoteId).ToList(); if (fav == null) throw new KeyNotFoundException(); foreach (var item in fav) { - _repo.Delete(item); + _context.favorites.Remove(item); } await _context.SaveChangesAsync(); @@ -80,13 +76,13 @@ namespace Contextlib public async Task RemoveAllFavoriteForUser(int userId) { - var fav = (await this.GetFavoriteByIdUser(userId,0,_repo.Count())).items; + var fav = _context.favorites.Where(item => item.IdUsers == userId).ToList(); if (fav == null) throw new KeyNotFoundException(); foreach (var item in fav) { - _repo.Delete(item); + _context.favorites.Remove(item); } await _context.SaveChangesAsync(); @@ -94,10 +90,11 @@ namespace Contextlib public async Task RemoveFavorite(int quoteid, int userId) { - var fav = await this.GetFavorite(userId, quoteid); + var fav = await _context.favorites.Where(item=>item.IdQuote==quoteid && item.IdUsers==userId).FirstAsync(); if (fav == null) throw new KeyNotFoundException(); - _repo.Delete(fav); + + _context.favorites.Remove(fav); await _context.SaveChangesAsync(); }