You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
118 lines
4.1 KiB
118 lines
4.1 KiB
using Entity;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Metadata.Internal;
|
|
using Shared;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Contextlib
|
|
{
|
|
public class DbFavoriteManager : IFavoriteService<Quote>
|
|
{
|
|
private WTFContext _context;
|
|
private GenericRepository<Quote> _repo;
|
|
|
|
public DbFavoriteManager(WTFContext context)
|
|
{
|
|
_context = context ?? throw new ArgumentNullException(nameof(context), "Database context cannot be null.");
|
|
_repo = new GenericRepository<Quote>(context);
|
|
}
|
|
|
|
public async Task AddFavorite(int quoteid, int userId)
|
|
{
|
|
var quote = await _context.quotes // collection des commentaires est chargée
|
|
.Include(q => q.Favorite)
|
|
.FirstOrDefaultAsync(q => q.Id == quoteid);
|
|
|
|
var user = await _context.users // collection des commentaires est chargée
|
|
.Include(u => u.Favorite)
|
|
.FirstOrDefaultAsync(q => q.Id == userId);
|
|
|
|
if (quote == null && user == null)
|
|
{
|
|
throw new ArgumentException("Quote or User not exist", nameof(quoteid));
|
|
}
|
|
|
|
|
|
var fav = new Favorite
|
|
{
|
|
// Lien entre le favorite et la citation
|
|
Quote = quote,
|
|
IdQuote = quoteid,
|
|
// Lien entre le favorite et le user
|
|
Users = user,
|
|
IdUsers = userId,
|
|
};
|
|
|
|
if (fav != null)
|
|
{
|
|
// Ajout favorite à la collection des favorites de la citation
|
|
quote.Favorite.Add(user);
|
|
|
|
// Ajout favorite à la collection des favorites de la citation
|
|
user.Favorite.Add(quote);
|
|
}
|
|
_context.Add(fav);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
|
|
public async Task RemoveAllFavoriteForQuote(int quoteId)
|
|
{
|
|
var fav = _context.favorites.Where(item => item.IdQuote == quoteId).ToList();
|
|
|
|
if (fav == null) throw new KeyNotFoundException();
|
|
|
|
foreach (var item in fav)
|
|
{
|
|
_context.favorites.Remove(item);
|
|
}
|
|
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task RemoveAllFavoriteForUser(int userId)
|
|
{
|
|
var fav = _context.favorites.Where(item => item.IdUsers == userId).ToList();
|
|
|
|
if (fav == null) throw new KeyNotFoundException();
|
|
|
|
foreach (var item in fav)
|
|
{
|
|
_context.favorites.Remove(item);
|
|
}
|
|
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task RemoveFavorite(int quoteid, int userId)
|
|
{
|
|
var fav = await _context.favorites.Where(item=>item.IdQuote==quoteid && item.IdUsers==userId).FirstAsync();
|
|
|
|
if (fav == null) throw new KeyNotFoundException();
|
|
|
|
_context.favorites.Remove(fav);
|
|
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task<Quote?> GetFavorite(int userId, int idQuote)
|
|
{
|
|
return await _context.quotes.Where( item => item.Id == idQuote && item.Favorite.Any(u => u.Id == userId) )
|
|
.Include(q => q.Source).Include(q => q.Character).ThenInclude(c => c.Images).Include(q=>q.Favorite)
|
|
.FirstOrDefaultAsync();
|
|
}
|
|
|
|
public async Task<PaginationResult<Quote>> GetFavoriteByIdUser(int userId, int index, int count)
|
|
{
|
|
List<Quote> fav = await _context.quotes.Where(item => item.Favorite.Any(u => u.Id == userId))
|
|
.Include(q => q.Source).Include(q => q.Character).ThenInclude(c => c.Images).Include(q => q.Favorite)
|
|
.Skip(index * count).Take(count).ToListAsync();
|
|
return new PaginationResult<Quote>(fav.Count,index,count,fav);
|
|
}
|
|
}
|
|
}
|