Handler.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using Application.Abstractions.Messaging;
  2. using Application.Abstractions.Data;
  3. using Domain.Entities.Forum.ValueObject;
  4. using Microsoft.EntityFrameworkCore;
  5. namespace Application.Features.Admin.Forum.Post.Get;
  6. public sealed class Handler(IAppDbContext db) : IQueryHandler<Query, Response>
  7. {
  8. public async Task<Response> Handle(Query request, CancellationToken ct)
  9. {
  10. var item = await db.Post.AsNoTracking().Include(c => c.Board).ThenInclude(c => c.BoardMeta).Include(c => c.BoardPrefix).Include(c => c.PostFile).Include(c => c.PostImage).Include(c => c.PostTag).ThenInclude(c => c.Tag).FirstOrDefaultAsync(x => x.ID == request.ID, ct);
  11. if (item is null)
  12. {
  13. throw new KeyNotFoundException("게시글을 찾을 수 없습니다.");
  14. }
  15. return new Response(
  16. item.ID,
  17. item.BoardID,
  18. item.Board.Name,
  19. item.BoardPrefixID,
  20. item.BoardPrefix?.Name,
  21. item.BoardPrefix?.Color,
  22. item.Subject,
  23. item.Content,
  24. item.Thumbnail,
  25. item.Name,
  26. item.SID,
  27. item.IsNotice,
  28. item.IsSecret,
  29. item.IsAnonymous,
  30. item.IsSpeaker,
  31. item.IsDeleted,
  32. item.Views,
  33. item.Likes,
  34. item.Dislikes,
  35. item.Comments,
  36. item.Images,
  37. item.Medias,
  38. item.Files,
  39. item.Tags,
  40. [..item.PostFile.OrderByDescending(f => f.ID).Select(f => new Response.FileItem(
  41. f.ID, f.FileName, f.Url, f.Extension, f.Size, f.Downloads, f.IsDisabled, f.CreatedAt
  42. ))],
  43. [..item.PostImage.OrderByDescending(f => f.ID).Select(f => new Response.ImageItem(
  44. f.ID, f.FileName, f.Url, f.Extension, f.Size, f.IsDisabled
  45. ))],
  46. [..item.PostTag.Select(t => new Response.TagItem(
  47. t.TagID, t.Tag.Name, t.Tag.Slug
  48. ))],
  49. item.Board.BoardMeta.List.Layout == BoardLayout.QnA,
  50. item.UpdatedAt,
  51. item.CreatedAt
  52. );
  53. }
  54. }