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) { // Retrieve comments for the specific quoteId var comments = await _context.comments.Where(x => x.IdQuote == quoteId).ToListAsync(); if (!comments.Any()) // If no comments exist for this quoteId { throw new KeyNotFoundException($"No comments found for the quote ID: {quoteId}."); } _context.comments.RemoveRange(comments); // Efficiently remove all comments in one batch 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) { // Retrieve comments for the specific userId var comments = await _context.comments.Where(x => x.IdUsers == userId).ToListAsync(); if (!comments.Any()) // If no comments exist for this userId { throw new KeyNotFoundException($"No comments found for the user ID: {userId}."); } _context.comments.RemoveRange(comments); // Efficiently remove all comments in one batch await _context.SaveChangesAsync(); } public async Task> GetAllComment() { throw new NotImplementedException(); } public async Task GetCommentById(int id) { throw new NotImplementedException(); } public async Task> GetCommentByQuote(int quoteId, int index, int pageSize) { throw new NotImplementedException(); } public async Task> GetCommentByUser(int userId, int index, int pageSize) { throw new NotImplementedException(); } public async Task LastCommentId() { throw new NotImplementedException(); } public async Task RemoveComment(int id) { throw new NotImplementedException(); } public async Task UpdateComment(int id, Commentary comment) { throw new NotImplementedException(); } } }