Handler.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. using Application.Abstractions.Messaging;
  2. using Application.Abstractions.Data;
  3. using Application.Abstractions.Cache;
  4. using SharedKernel.Storage;
  5. using Microsoft.EntityFrameworkCore;
  6. namespace Application.Features.Admin.Banner.Item.Delete;
  7. public sealed class Handler(IAppDbContext db, IFileStorage fileStorage, ICacheService cache) : ICommandHandler<Command>
  8. {
  9. public async Task Handle(Command request, CancellationToken ct)
  10. {
  11. if (request.IDs is null || request.IDs.Length == 0)
  12. {
  13. return;
  14. }
  15. var images = await db.BannerItem.Where(c => request.IDs.Contains(c.ID)).Select(c => new { c.DesktopImage, c.MobileImage }).ToListAsync(ct);
  16. foreach (var img in images)
  17. {
  18. if (img.DesktopImage != null)
  19. {
  20. fileStorage.DeleteByUrl(img.DesktopImage);
  21. }
  22. if (img.MobileImage != null)
  23. {
  24. fileStorage.DeleteByUrl(img.MobileImage);
  25. }
  26. }
  27. await db.BannerItem.Where(c => request.IDs.Contains(c.ID)).ExecuteDeleteAsync(ct);
  28. await cache.RemoveByPrefixAsync("banner:", ct);
  29. }
  30. }