|
|
|
@ -43,14 +43,13 @@ namespace Contextlib
|
|
|
|
|
/// <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
|
|
|
|
|
if (!comments.Any())
|
|
|
|
|
{
|
|
|
|
|
throw new KeyNotFoundException($"No comments found for the quote ID: {quoteId}.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_context.comments.RemoveRange(comments); // Efficiently remove all comments in one batch
|
|
|
|
|
_context.comments.RemoveRange(comments);
|
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -62,45 +61,95 @@ namespace Contextlib
|
|
|
|
|
/// <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.IdUser == userId).ToListAsync();
|
|
|
|
|
if (!comments.Any()) // If no comments exist for this userId
|
|
|
|
|
if (!comments.Any())
|
|
|
|
|
{
|
|
|
|
|
throw new KeyNotFoundException($"No comments found for the user ID: {userId}.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_context.comments.RemoveRange(comments); // Efficiently remove all comments in one batch
|
|
|
|
|
_context.comments.RemoveRange(comments);
|
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<PaginationResult<Commentary>> GetAllComment()
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
var comments = _context.comments.ToList();
|
|
|
|
|
return new PaginationResult<Commentary>(comments.Count, 0, comments.Count, comments);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<Commentary> GetCommentById(int id)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
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<PaginationResult<Commentary>> GetCommentByQuote(int quoteId, int index, int pageSize)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
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<Commentary>(comments.Count(), index, pageSize, comments);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return new PaginationResult<Commentary>(pageSize, index, pageSize, comments.Skip(index * pageSize - (((index * pageSize) + pageSize) - comments.Count)).Take(pageSize).ToList());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return new PaginationResult<Commentary>(comments.Count, index, pageSize, comments.Skip(index * pageSize).Take(pageSize).ToList());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<PaginationResult<Commentary>> GetCommentByUser(int userId, int index, int pageSize)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
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<Commentary>(comments.Count(), index, pageSize, comments);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return new PaginationResult<Commentary>(pageSize, index, pageSize, comments.Skip(index * pageSize - (((index * pageSize) + pageSize) - comments.Count)).Take(pageSize).ToList());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return new PaginationResult<Commentary>(comments.Count, index, pageSize, comments.Skip(index * pageSize).Take(pageSize).ToList());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<int> LastCommentId()
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
var last = _context.comments.OrderByDescending(x => x.Id).FirstOrDefault();
|
|
|
|
|
if(last == null)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return last.Id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task RemoveComment(int id)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
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)
|
|
|
|
|