Handler.cs 776 B

123456789101112131415161718192021222324252627
  1. using Application.Abstractions.Data;
  2. using Application.Abstractions.Messaging;
  3. using SharedKernel.Storage;
  4. using Microsoft.EntityFrameworkCore;
  5. namespace Application.Features.Forum.CommentFile.Delete;
  6. public sealed class Handler(IAppDbContext db, IFileStorage fileStorage) : ICommandHandler<Command>
  7. {
  8. public async Task Handle(Command request, CancellationToken ct)
  9. {
  10. if (request.IDs is null || request.IDs.Length == 0)
  11. {
  12. return;
  13. }
  14. var files = await db.CommentFile
  15. .Where(c => request.IDs.Contains(c.ID))
  16. .ToListAsync(ct);
  17. foreach (var file in files)
  18. fileStorage.DeleteByUrl(file.Url);
  19. db.CommentFile.RemoveRange(files);
  20. await db.SaveChangesAsync(ct);
  21. }
  22. }