Finition route favorite

pull/6/head
kekentin 3 weeks ago
parent eb0af16e6a
commit ed678b67eb

@ -27,17 +27,15 @@ namespace Contextlib
.Include(q => q.Favorite) .Include(q => q.Favorite)
.FirstOrDefaultAsync(q => q.Id == quoteid); .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 var user = await _context.users // collection des commentaires est chargée
.Include(u => u.Favorite) .Include(u => u.Favorite)
.FirstOrDefaultAsync(q => q.Id == userId); .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 var fav = new Favorite
{ {
@ -64,15 +62,13 @@ namespace Contextlib
public async Task RemoveAllFavoriteForQuote(int quoteId) public async Task RemoveAllFavoriteForQuote(int quoteId)
{ {
var fav = await _context.quotes.Where(item => item.Id==quoteId) var fav = _context.favorites.Where(item => item.IdQuote == quoteId).ToList();
.Include(q => q.Source).Include(q => q.Character).ThenInclude(c => c.Images).Include(q => q.Favorite)
.ToListAsync();
if (fav == null) throw new KeyNotFoundException(); if (fav == null) throw new KeyNotFoundException();
foreach (var item in fav) foreach (var item in fav)
{ {
_repo.Delete(item); _context.favorites.Remove(item);
} }
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
@ -80,13 +76,13 @@ namespace Contextlib
public async Task RemoveAllFavoriteForUser(int userId) 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(); if (fav == null) throw new KeyNotFoundException();
foreach (var item in fav) foreach (var item in fav)
{ {
_repo.Delete(item); _context.favorites.Remove(item);
} }
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
@ -94,10 +90,11 @@ namespace Contextlib
public async Task RemoveFavorite(int quoteid, int userId) 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(); if (fav == null) throw new KeyNotFoundException();
_repo.Delete(fav);
_context.favorites.Remove(fav);
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
} }

Loading…
Cancel
Save