using Entity; using Microsoft.EntityFrameworkCore; using Shared; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Contextlib { public class DbCommentManager : ICommentService { private WTFContext _context; public DbCommentManager(WTFContext context) { _context = context ?? throw new ArgumentNullException(nameof(context), "Database context cannot be null."); } /// /// 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) { if (comment == null) { throw new ArgumentNullException(nameof(comment), "Comment cannot be null."); } await _context.comments.AddAsync(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 DeleteCommentForQuote(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 DeleteCommentForUser(int userId) { var comments = await _context.comments.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> GetAllComment() { var comments = _context.comments.ToList(); return new PaginationResult(comments.Count, 0, comments.Count, comments); } public async Task GetCommentById(int id) { var comment = await _context.comments.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> GetCommentByQuote(int quoteId, int index, int pageSize) { 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}."); } 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> GetCommentByUser(int userId, int index, int pageSize) { var comments = await _context.comments.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 LastCommentId() { var last = _context.comments.OrderByDescending(x => x.Id).FirstOrDefault(); if(last == null) { return 0; } return last.Id; } public async Task RemoveComment(int id) { var comment = await _context.comments.Where(x => x.Id == id).FirstOrDefaultAsync(); if (comment == null) { throw new KeyNotFoundException($"No comments found with the given ID: {id}."); } _context.comments.Remove(comment); await _context.SaveChangesAsync(); } public async Task UpdateComment(int id, Commentary comment) { throw new NotImplementedException(); } } }