Handler.cs 775 B

123456789101112131415161718192021222324252627
  1. using Application.Abstractions.Data;
  2. using Application.Abstractions.Messaging;
  3. using Microsoft.EntityFrameworkCore;
  4. namespace Application.Features.Forum.Trash.Comment.Restore;
  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 comments = await db.Comment.Where(c => request.IDs.Contains(c.ID) && c.IsDeleted).ToListAsync(ct);
  14. foreach (var comment in comments)
  15. {
  16. comment.IsDeleted = false;
  17. comment.DeletedAt = null;
  18. comment.UpdatedAt = DateTime.UtcNow;
  19. }
  20. await db.SaveChangesAsync(ct);
  21. }
  22. }