Handler.cs 810 B

12345678910111213141516171819202122232425
  1. using Application.Abstractions.Messaging;
  2. using Application.Abstractions.Data;
  3. using SharedKernel.Storage;
  4. using Microsoft.EntityFrameworkCore;
  5. namespace Application.Features.Admin.Forum.Post.DeleteThumbnail;
  6. public sealed class Handler(IAppDbContext db, IFileStorage fileStorage) : ICommandHandler<Command>
  7. {
  8. public async Task Handle(Command request, CancellationToken ct)
  9. {
  10. var post = await db.Post.FirstOrDefaultAsync(x => x.ID == request.ID, ct);
  11. if (post is null)
  12. {
  13. throw new KeyNotFoundException("게시글을 찾을 수 없습니다.");
  14. }
  15. if (!string.IsNullOrEmpty(post.Thumbnail))
  16. {
  17. fileStorage.DeleteByUrl(post.Thumbnail);
  18. post.Thumbnail = null;
  19. await db.SaveChangesAsync(ct);
  20. }
  21. }
  22. }