Handler.cs 1.3 KB

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