Handler.cs 987 B

12345678910111213141516171819202122232425262728293031323334
  1. using Application.Abstractions.Data;
  2. using SharedKernel.Storage;
  3. using MediatR;
  4. using Microsoft.EntityFrameworkCore;
  5. namespace Application.Features.Member.List.Delete;
  6. public sealed class Handler(IAppDbContext db, IFileStorage fileStorage) : IRequestHandler<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 images = await db.Member.Where(c => request.IDs.Contains(c.ID)).Select(c => new { c.Thumb, c.Icon }).ToListAsync(ct);
  15. foreach (var img in images)
  16. {
  17. if (!string.IsNullOrEmpty(img.Thumb))
  18. {
  19. fileStorage.DeleteByUrl(img.Thumb);
  20. }
  21. if (!string.IsNullOrEmpty(img.Icon))
  22. {
  23. fileStorage.DeleteByUrl(img.Icon);
  24. }
  25. }
  26. await db.Member.Where(x => request.IDs.Contains(x.ID)).ExecuteDeleteAsync(ct);
  27. }
  28. }