Handler.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. using Application.Abstractions.Messaging;
  2. using Application.Abstractions.Data;
  3. using Application.Abstractions.Cache;
  4. using Microsoft.EntityFrameworkCore;
  5. namespace Application.Features.Api.Faq.Category.GetActive;
  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 cached = await cache.GetAsync<Response>(CacheKeys.FaqCategoryActive, ct);
  11. if (cached is not null)
  12. {
  13. return cached;
  14. }
  15. var rows = await db.FaqCategory
  16. .AsNoTracking()
  17. .Where(c => c.IsActive)
  18. .OrderBy(c => c.Order)
  19. .ThenByDescending(c => c.ID)
  20. .Select(c => new Response.Row(
  21. c.ID,
  22. c.Code,
  23. c.Subject,
  24. c.Order
  25. ))
  26. .ToListAsync(ct);
  27. var response = new Response(rows.Count, rows);
  28. await cache.SetAsync(CacheKeys.FaqCategoryActive, response, ct);
  29. return response;
  30. }
  31. }