Handler.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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.Faq.Item.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.FaqItem.AnyAsync(c => c.Question == request.Question))
  13. {
  14. throw new Exception("�̹� ��ϵ� FAQ�Դϴ�.");
  15. }
  16. var faq = Domain.Entities.Page.Faq.FaqItem.Create(
  17. request.CategoryID,
  18. request.Question,
  19. null,
  20. request.Order,
  21. request.IsActive
  22. );
  23. await db.FaqItem.AddAsync(faq, ct);
  24. int affectedRows = await db.SaveChangesAsync();
  25. if (affectedRows <= 0)
  26. {
  27. throw new Exception("FAQ ��� �� ������ �߻��߽��ϴ�.");
  28. }
  29. var path = new FileStoragePath(UploadTarget.Editor, UploadFolder.Faq, faq.ID);
  30. var html = await editorImage.UploadAsync(request.Answer, path, ct);
  31. faq.SetContent(html);
  32. await db.SaveChangesAsync();
  33. await cache.RemoveByPrefixAsync("faq:item:", ct);
  34. }
  35. }
  36. }