Handler.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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.Document.Create
  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. if (await db.Document.AnyAsync(c => c.Code == request.Code))
  13. {
  14. throw new Exception("이미 등록된 문서입니다.");
  15. }
  16. var document = Domain.Entities.Page.Document.Create(
  17. request.Code,
  18. request.Subject,
  19. null,
  20. request.IsActive
  21. );
  22. await db.Document.AddAsync(document, ct);
  23. int affectedRows = await db.SaveChangesAsync();
  24. if (affectedRows <= 0)
  25. {
  26. throw new Exception("문서 등록 중 오류가 발생했습니다.");
  27. }
  28. var path = new FileStoragePath(UploadTarget.Editor, UploadFolder.Document, document.ID);
  29. var html = await editorImage.UploadAsync(request.Content, path, ct);
  30. document.SetContent(html);
  31. await db.SaveChangesAsync();
  32. await cache.RemoveByPrefixAsync("document:", ct);
  33. }
  34. }
  35. }