Handler.cs 777 B

12345678910111213141516171819202122232425262728
  1. using Application.Abstractions.Data;
  2. using Application.Abstractions.Messaging;
  3. using SharedKernel.Storage;
  4. using Microsoft.EntityFrameworkCore;
  5. namespace Application.Features.Admin.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.Where(c => request.IDs.Contains(c.ID)).ToListAsync(ct);
  15. foreach (var file in files)
  16. {
  17. fileStorage.DeleteByUrl(file.Url);
  18. }
  19. db.CommentFile.RemoveRange(files);
  20. await db.SaveChangesAsync(ct);
  21. }
  22. }