Handler.cs 1.3 KB

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