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.Popup.Delete;
  7. public sealed class Handler(IAppDbContext db, IEditorImageService editorImage, ICacheService cache) : ICommandHandler<Command>
  8. {
  9. public async Task Handle(Command request, CancellationToken ct)
  10. {
  11. if (request.IDs is null || request.IDs.Length < 1)
  12. {
  13. return;
  14. }
  15. var list = await db.Popup.Where(x => request.IDs.Contains(x.ID)).ToListAsync(ct);
  16. if (list.Count < 1)
  17. {
  18. return;
  19. }
  20. var contents = await db.Popup.Where(c => request.IDs.Contains(c.ID)).Select(c => c.Content).ToListAsync(ct);
  21. foreach (var content in contents)
  22. {
  23. await editorImage.CleanupByContentAsync(content, ct);
  24. }
  25. db.Popup.RemoveRange(list);
  26. await db.SaveChangesAsync(ct);
  27. await cache.RemoveByPrefixAsync("popup:", ct);
  28. }
  29. }