Handler.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using Application.Abstractions.Messaging;
  2. using Application.Abstractions.Data;
  3. using Application.Abstractions.Cache;
  4. using Microsoft.EntityFrameworkCore;
  5. using SharedKernel.Storage;
  6. namespace Application.Features.Admin.Document.Update
  7. {
  8. public sealed class Handler(IAppDbContext db, IEditorImageService editorImage, ICacheService cache) : ICommandHandler<Command>
  9. {
  10. public async Task Handle(Command request, CancellationToken ct)
  11. {
  12. var document = await db.Document.FirstOrDefaultAsync(c => c.ID == request.ID, ct);
  13. if (document is null)
  14. {
  15. throw new Exception("문서를 찾을 수 없습니다.");
  16. }
  17. if (await db.Document.AnyAsync(c => c.Code == request.Code && c.ID != request.ID))
  18. {
  19. throw new Exception("이미 등록된 문서입니다.");
  20. }
  21. var path = new FileStoragePath(UploadTarget.Editor, UploadFolder.Document, request.ID);
  22. document.Update(
  23. request.Code,
  24. request.Subject,
  25. await editorImage.UploadAsync(request.Content, path, ct),
  26. request.IsActive
  27. );
  28. int affectedRows = await db.SaveChangesAsync();
  29. if (affectedRows <= 0)
  30. {
  31. throw new Exception("문서 수정 중 오류가 발생했습니다.");
  32. }
  33. await cache.RemoveByPrefixAsync("document:", ct);
  34. }
  35. }
  36. }