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.
WF-PmAPI/WF_EF_Api/Contextlib/DbCommentaryManager.cs

112 lines
4.2 KiB

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<Commentary>
{
private WTFContext _context;
public DbCommentManager(WTFContext context)
{
_context = context ?? throw new ArgumentNullException(nameof(context), "Database context cannot be null.");
}
/// <summary>
/// Adds a new comment to the database.
/// </summary>
/// <param name="comment">The comment to add.</param>
/// <returns>A task representing the asynchronous operation.</returns>
/// <exception cref="ArgumentNullException">Thrown when the comment is null.</exception>
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();
}
/// <summary>
/// Deletes all comments associated with a specific quote ID from the database.
/// </summary>
/// <param name="quoteId">The ID of the quote whose comments need to be deleted.</param>
/// <returns>A task representing the asynchronous operation.</returns>
/// <exception cref="KeyNotFoundException">Thrown when no comments are found for the provided quote ID.</exception>
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();
}
/// <summary>
/// Deletes all comments associated with a specific user ID from the database.
/// </summary>
/// <param name="userId">The ID of the user whose comments need to be deleted.</param>
/// <returns>A task representing the asynchronous operation.</returns>
/// <exception cref="KeyNotFoundException">Thrown when no comments are found for the provided user ID.</exception>
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<PaginationResult<Commentary>> GetAllComment()
{
throw new NotImplementedException();
}
public async Task<Commentary> GetCommentById(int id)
{
throw new NotImplementedException();
}
public async Task<PaginationResult<Commentary>> GetCommentByQuote(int quoteId, int index, int pageSize)
{
throw new NotImplementedException();
}
public async Task<PaginationResult<Commentary>> GetCommentByUser(int userId, int index, int pageSize)
{
throw new NotImplementedException();
}
public async Task<int> LastCommentId()
{
throw new NotImplementedException();
}
public async Task RemoveComment(int id)
{
throw new NotImplementedException();
}
public async Task UpdateComment(int id, Commentary comment)
{
throw new NotImplementedException();
}
}
}