Handler.cs 1.0 KB

123456789101112131415161718192021222324252627282930313233
  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
  14. .Where(c => request.IDs.Contains(c.ID))
  15. .ToListAsync(ct);
  16. var commentIDs = reports.Select(c => c.CommentID).Distinct().ToList();
  17. var comments = await db.Comment.Where(c => commentIDs.Contains(c.ID)).ToListAsync(ct);
  18. foreach (var comment in comments)
  19. {
  20. var count = reports.Count(c => c.CommentID == comment.ID);
  21. comment.Reports -= count;
  22. comment.UpdatedAt = DateTime.UtcNow;
  23. }
  24. db.CommentReport.RemoveRange(reports);
  25. await db.SaveChangesAsync(ct);
  26. }
  27. }