pull/5/head
Kevin MONDEJAR 1 month ago
parent 8ccde0b125
commit 61fa6f89d7

@ -43,14 +43,13 @@ namespace Contextlib
/// <exception cref="KeyNotFoundException">Thrown when no comments are found for the provided quote ID.</exception> /// <exception cref="KeyNotFoundException">Thrown when no comments are found for the provided quote ID.</exception>
public async Task DeleteCommentForQuote(int quoteId) public async Task DeleteCommentForQuote(int quoteId)
{ {
// Retrieve comments for the specific quoteId
var comments = await _context.comments.Where(x => x.IdQuote == quoteId).ToListAsync(); 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}."); 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(); await _context.SaveChangesAsync();
} }
@ -62,45 +61,95 @@ namespace Contextlib
/// <exception cref="KeyNotFoundException">Thrown when no comments are found for the provided user ID.</exception> /// <exception cref="KeyNotFoundException">Thrown when no comments are found for the provided user ID.</exception>
public async Task DeleteCommentForUser(int userId) public async Task DeleteCommentForUser(int userId)
{ {
// Retrieve comments for the specific userId
var comments = await _context.comments.Where(x => x.IdUser == userId).ToListAsync(); 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}."); 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(); await _context.SaveChangesAsync();
} }
public async Task<PaginationResult<Commentary>> GetAllComment() 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) 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) 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) 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() 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) 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) public async Task UpdateComment(int id, Commentary comment)

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\DTO\DTO.csproj" />
<ProjectReference Include="..\Entity\Entity.csproj" />
</ItemGroup>
</Project>

File diff suppressed because it is too large Load Diff

@ -0,0 +1,22 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace StubbedContextLib.Migrations
{
/// <inheritdoc />
public partial class migr1 : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
}
}
}
Loading…
Cancel
Save