Handler.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using Application.Abstractions.Messaging;
  2. using Application.Abstractions.Data;
  3. using Application.Abstractions.Cache;
  4. using Microsoft.EntityFrameworkCore;
  5. namespace Application.Features.Api.Popup.GetAll;
  6. public sealed class Handler(IAppDbContext db, ICacheService cache) : IQueryHandler<Query, Response>
  7. {
  8. public async Task<Response> Handle(Query request, CancellationToken ct)
  9. {
  10. var cacheKey = CacheKeys.PopupByCode(request.Code);
  11. var cached = await cache.GetAsync<Response>(cacheKey, ct);
  12. if (cached is not null)
  13. {
  14. return cached;
  15. }
  16. var query = db.Popup.AsNoTracking().Where(x => x.PopupPosition.Code == request.Code);
  17. var total = await query.CountAsync(ct);
  18. var list = await query
  19. .OrderBy(x => x.Order)
  20. .ThenByDescending(x => x.ID)
  21. .Select(x => new
  22. {
  23. x.ID,
  24. x.PositionID,
  25. x.Subject,
  26. x.Link,
  27. x.StartAt,
  28. x.EndAt,
  29. x.Order,
  30. x.IsActive,
  31. x.UpdatedAt,
  32. x.CreatedAt
  33. })
  34. .ToListAsync(ct);
  35. var response = new Response
  36. {
  37. Total = total,
  38. List = [..list.Select(x => new Response.Row
  39. {
  40. ID = x.ID,
  41. PositionID = x.PositionID,
  42. Subject = x.Subject,
  43. Link = x.Link,
  44. StartAt = x.StartAt,
  45. EndAt = x.EndAt,
  46. Order = x.Order,
  47. IsActive = x.IsActive,
  48. UpdatedAt = x.UpdatedAt,
  49. CreatedAt = x.CreatedAt
  50. })]
  51. };
  52. await cache.SetAsync(cacheKey, response, ct);
  53. return response;
  54. }
  55. }