using Entity; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Update; using Shared; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Contextlib { public class DbCommentaryManager : ICommentaryService { private WTFContext _context; private GenericRepository _repo; public DbCommentaryManager(WTFContext context) { _context = context ?? throw new ArgumentNullException(nameof(context), "Database context cannot be null."); _repo = new GenericRepository(context); } /// /// Adds a new comment to the database. /// /// The comment to add. /// A task representing the asynchronous operation. /// Thrown when the comment is null. public async Task AddComment(Commentary comment, int idQuote) { if (comment == null) { throw new ArgumentNullException(nameof(comment), "Comment cannot be null."); } var quote = await _context.quotes .Include(q => q.Commentarys) // collection des commentaires est chargée .FirstOrDefaultAsync(q => q.Id == idQuote); var dbU = new DbUsersManager(_context); var User = await dbU.GetUserByUsername(comment.User.UserName); if (User == null) { throw new ArgumentException("Quote not exist", nameof(comment.User.UserName)); } if (quote == null) { throw new ArgumentException("Quote not exist", nameof(idQuote)); } comment.User = User; comment.IdUser = User.Id; // Lien entre le commentaire et la citation comment.Quote = quote; comment.IdQuote = idQuote; // Ajout commentaire à la collection des commentaires de la citation //_repo.Insert(comment); _context.Add(comment); await _context.SaveChangesAsync(); } /// /// Deletes all comments associated with a specific quote ID from the database. /// /// The ID of the quote whose comments need to be deleted. /// A task representing the asynchronous operation. /// Thrown when no comments are found for the provided quote ID. public async Task DeleteCommentaryForQuote(int quoteId) { var comments = await _context.comments.Where(x => x.IdQuote == quoteId).ToListAsync(); if (!comments.Any()) { throw new KeyNotFoundException($"No comments found for the quote ID: {quoteId}."); } _context.comments.RemoveRange(comments); await _context.SaveChangesAsync(); } /// /// Deletes all comments associated with a specific user ID from the database. /// /// The ID of the user whose comments need to be deleted. /// A task representing the asynchronous operation. /// Thrown when no comments are found for the provided user ID. public async Task DeleteCommentaryForUser(int userId) { var comments = await _context.comments.Include(c => c.User).Where(x => x.IdUser == userId).ToListAsync(); if (!comments.Any()) { throw new KeyNotFoundException($"No comments found for the user ID: {userId}."); } _context.comments.RemoveRange(comments); await _context.SaveChangesAsync(); } public async Task> GetAllCommentary() { var comments = await _context.comments.Include(c => c.User).Include(c=>c.User.Images).ToListAsync(); return new PaginationResult(comments.Count, 0, comments.Count, comments); } public async Task GetCommentaryById(int id) { var comment = await _context.comments.Include(c => c.User).Include(c => c.User.Images).Where(x => x.Id == id).FirstOrDefaultAsync(); if(comment == null) { throw new KeyNotFoundException($"No comments found with the given ID: {id}."); } return comment; } public async Task> GetCommentaryByQuote(int quoteId, int index, int pageSize) { var comments = await _context.comments.Include(c => c.User).Include(c => c.User.Images).Where(x => x.IdQuote == quoteId).ToListAsync(); if (!comments.Any()) { throw new KeyNotFoundException($"No comments found for the quote ID: {quoteId}."); } if ((index * pageSize + pageSize) > comments.Count) { if (pageSize > comments.Count) { return new PaginationResult(comments.Count(), index, pageSize, comments); } else { return new PaginationResult(pageSize, index, pageSize, comments.Skip(index * pageSize - (((index * pageSize) + pageSize) - comments.Count)).Take(pageSize).ToList()); } } return new PaginationResult(comments.Count, index, pageSize, comments.Skip(index * pageSize).Take(pageSize).ToList()); } public async Task> GetCommentaryByUser(int userId, int index, int pageSize) { var comments = await _context.comments.Include(c => c.User).Where(x => x.IdUser == userId).ToListAsync(); if (!comments.Any()) { throw new KeyNotFoundException($"No comments found for the user ID: {userId}."); } if ((index * pageSize + pageSize) > comments.Count) { if (pageSize > comments.Count) { return new PaginationResult(comments.Count(), index, pageSize, comments); } else { return new PaginationResult(pageSize, index, pageSize, comments.Skip(index * pageSize - (((index * pageSize) + pageSize) - comments.Count)).Take(pageSize).ToList()); } } return new PaginationResult(comments.Count, index, pageSize, comments.Skip(index * pageSize).Take(pageSize).ToList()); } public async Task LastCommentaryId() { var last = await _context.comments.OrderByDescending(x => x.Id).FirstOrDefaultAsync(); if(last == null) { return 0; } return last.Id; } public async Task RemoveCommentary(int id) { Commentary? commentary = await GetCommentaryById(id); if (commentary == null) { throw new KeyNotFoundException($"Error : No comment found with the ID: {id}."); } _repo.Delete(commentary); await _context.SaveChangesAsync(); } public async Task UpdateCommentary(int id, Commentary comment) { var modif = false; var com = await _context.comments.Where(x => x.Id == id).FirstOrDefaultAsync(); if (comment == null) { throw new ArgumentNullException(nameof(comment), "The updated comment data cannot be null."); } if (com == null) { throw new KeyNotFoundException($"No comments found with the given ID: {id}."); } if (comment.Comment != null) { com.Comment = comment.Comment; modif = true; } if(comment.DateCommentary != null){ com.DateCommentary = comment.DateCommentary; modif = true; } if(comment.IdQuote != 0) { com.IdQuote = comment.IdQuote; modif = true; } if (comment.IdUser != 0) { com.IdUser = comment.IdUser; modif = true; } if (modif) { await _context.SaveChangesAsync(); } } } }