Handler.cs 777 B

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