Handler.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using Application.Abstractions.Messaging;
  2. using Application.Abstractions.Data;
  3. using Microsoft.EntityFrameworkCore;
  4. namespace Application.Features.Admin.Forum.BoardGroup.GetAll;
  5. public sealed class Handler(IAppDbContext db) : IQueryHandler<Query, Response>
  6. {
  7. public async Task<Response> Handle(Query request, CancellationToken ct)
  8. {
  9. var items = await db.BoardGroup
  10. .AsNoTracking()
  11. .OrderBy(c => c.Order)
  12. .ThenByDescending(c => c.ID)
  13. .Select(c => new
  14. {
  15. c.ID,
  16. c.Code,
  17. c.Name,
  18. c.Order,
  19. c.Boards,
  20. c.Posts,
  21. c.Comments,
  22. c.UpdatedAt,
  23. c.CreatedAt
  24. })
  25. .ToListAsync(ct);
  26. return new Response(
  27. items.Count,
  28. [..items.Select((c, i) => new Response.Row(
  29. i + 1,
  30. c.ID,
  31. i,
  32. c.Code,
  33. c.Name,
  34. c.Order,
  35. c.Boards,
  36. c.Posts,
  37. c.Comments,
  38. c.UpdatedAt,
  39. c.CreatedAt
  40. ))]);
  41. }
  42. }