Handler.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using Application.Abstractions.Messaging;
  2. using Application.Abstractions.Data;
  3. using SharedKernel.Extensions;
  4. using Microsoft.EntityFrameworkCore;
  5. namespace Application.Features.Admin.Popup.Search;
  6. public sealed class Handler(IAppDbContext db) : IQueryHandler<Query, Response>
  7. {
  8. public async Task<Response> Handle(Query request, CancellationToken ct)
  9. {
  10. var query = db.Popup.AsNoTracking();
  11. var total = await query.CountAsync(ct);
  12. var skip = (request.PageNum - 1) * request.PerPage;
  13. var list = await query
  14. .OrderByDescending(x => x.ID)
  15. .Skip(skip)
  16. .Take(request.PerPage)
  17. .Select(x => new
  18. {
  19. x.ID,
  20. x.PositionID,
  21. PositionCode = x.PopupPosition.Code,
  22. PositionSubject = x.PopupPosition.Subject,
  23. x.Subject,
  24. x.Link,
  25. x.StartAt,
  26. x.EndAt,
  27. x.Order,
  28. x.IsActive,
  29. x.UpdatedAt,
  30. x.CreatedAt
  31. })
  32. .ToListAsync(ct);
  33. var rows = list
  34. .Select((x, idx) => new Response.Row
  35. {
  36. Num = total - skip - idx,
  37. ID = x.ID,
  38. PositionID = x.PositionID,
  39. PositionCode = x.PositionCode,
  40. PositionSubject = x.PositionSubject,
  41. Subject = x.Subject,
  42. Link = x.Link,
  43. StartAt = x.StartAt?.ToString("yyyy-MM-dd HH:mm"),
  44. EndAt = x.EndAt?.ToString("yyyy-MM-dd HH:mm"),
  45. Order = x.Order,
  46. IsActive = x.IsActive,
  47. UpdatedAt = x.UpdatedAt.GetDateAt(),
  48. CreatedAt = x.CreatedAt.GetDateAt()
  49. })
  50. .ToList();
  51. return new Response
  52. {
  53. Total = total,
  54. List = rows
  55. };
  56. }
  57. }