Handler.cs 1019 B

1234567891011121314151617181920212223242526272829303132
  1. using Application.Abstractions.Data;
  2. using Application.Abstractions.Messaging;
  3. using Microsoft.EntityFrameworkCore;
  4. namespace Application.Features.Forum.CommentReport.Delete;
  5. public sealed class Handler(IAppDbContext db) : ICommandHandler<Command>
  6. {
  7. public async Task Handle(Command request, CancellationToken ct)
  8. {
  9. if (request.IDs is null || request.IDs.Length == 0)
  10. {
  11. return;
  12. }
  13. var reports = await db.CommentReport.Where(c => request.IDs.Contains(c.ID)).ToListAsync(ct);
  14. var commentIDs = reports.Select(c => c.CommentID).Distinct().ToList();
  15. var comments = await db.Comment.Where(c => commentIDs.Contains(c.ID)).ToListAsync(ct);
  16. foreach (var comment in comments)
  17. {
  18. var count = reports.Count(c => c.CommentID == comment.ID);
  19. comment.Reports -= count;
  20. comment.UpdatedAt = DateTime.UtcNow;
  21. }
  22. db.CommentReport.RemoveRange(reports);
  23. await db.SaveChangesAsync(ct);
  24. }
  25. }