Handler.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using Application.Abstractions.Data;
  2. using SharedKernel.Extensions;
  3. using MediatR;
  4. using Microsoft.EntityFrameworkCore;
  5. namespace Application.Features.Faq.Category.Get
  6. {
  7. public sealed class Handler(IAppDbContext db) : IRequestHandler<Query, Response>
  8. {
  9. public async Task<Response> Handle(Query request, CancellationToken ct)
  10. {
  11. var items = await db.FaqCategory
  12. .AsNoTracking()
  13. .Include(c => c.FaqItems)
  14. .OrderBy(c => c.Order)
  15. .ThenByDescending(c => c.ID)
  16. .Select(c => new
  17. {
  18. c.ID,
  19. c.Code,
  20. c.Subject,
  21. c.Order,
  22. c.IsActive,
  23. FaqItemCount = c.FaqItems.Count,
  24. c.UpdatedAt,
  25. c.CreatedAt
  26. })
  27. .ToListAsync(ct);
  28. int total = items.Count;
  29. var rows = items.Select((c, index) => new Response.Row(
  30. Num: total - index,
  31. ID: c.ID,
  32. Index: index,
  33. Code: c.Code,
  34. Subject: c.Subject,
  35. Order: c.Order,
  36. IsActive: c.IsActive,
  37. (ushort)c.FaqItemCount,
  38. c.UpdatedAt.GetDateAt() ?? "-",
  39. c.CreatedAt.GetDateAt()
  40. )).ToList();
  41. return new Response(total, rows);
  42. }
  43. }
  44. }