Handler.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using Application.Abstractions.Data;
  2. using Application.Abstractions.Messaging;
  3. using Domain.Entities.Donations.ValueObject;
  4. using Microsoft.EntityFrameworkCore;
  5. namespace Application.Features.Admin.Donation.Alert.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.DonationAlert.AsNoTracking().Include(x => x.Sponsor).Include(x => x.Receiver).AsQueryable();
  11. if (!string.IsNullOrWhiteSpace(request.Keyword))
  12. {
  13. query = request.Search switch
  14. {
  15. 1 => query.Where(x => x.Sponsor != null && x.Sponsor.ID.ToString().Contains(request.Keyword)),
  16. 2 => query.Where(x => x.Sponsor != null && x.Sponsor.SID.Contains(request.Keyword)),
  17. 3 => query.Where(x => x.Sponsor != null && x.Sponsor.Name != null && x.Sponsor.Name.Contains(request.Keyword)),
  18. 4 => query.Where(x => x.Receiver != null && x.Receiver.ID.ToString().Contains(request.Keyword)),
  19. 5 => query.Where(x => x.Receiver != null && x.Receiver.SID.Contains(request.Keyword)),
  20. 6 => query.Where(x => x.Receiver != null && x.Receiver.Name != null && x.Receiver.Name.Contains(request.Keyword)),
  21. _ => query
  22. };
  23. }
  24. if (!string.IsNullOrWhiteSpace(request.StartAt) && DateTime.TryParse(request.StartAt, out var startAt))
  25. {
  26. query = query.Where(x => x.CreatedAt >= startAt);
  27. }
  28. if (!string.IsNullOrWhiteSpace(request.EndAt) && DateTime.TryParse(request.EndAt, out var endAt))
  29. {
  30. query = query.Where(x => x.CreatedAt <= endAt);
  31. }
  32. // 탭별 건수 집계 (탭 필터 적용 전)
  33. var counts = await query.GroupBy(c => 1).Select(c => new
  34. {
  35. Received = c.Count(x => x.Status == AlertStatus.Delivered),
  36. NotReceived = c.Count(x => x.Status != AlertStatus.Delivered)
  37. }).FirstOrDefaultAsync(ct);
  38. var received = counts?.Received ?? 0;
  39. var notReceived = counts?.NotReceived ?? 0;
  40. query = request.Tab switch
  41. {
  42. 1 => query.Where(x => x.Status == AlertStatus.Delivered),
  43. 2 => query.Where(x => x.Status != AlertStatus.Delivered),
  44. _ => query
  45. };
  46. var total = await query.CountAsync(ct);
  47. var skip = (request.Page - 1) * request.PerPage;
  48. var list = await query
  49. .OrderByDescending(x => x.ID)
  50. .Skip(skip)
  51. .Take(request.PerPage)
  52. .Select(x => new
  53. {
  54. x.ID,
  55. x.DonationID,
  56. x.Status,
  57. x.StartAt,
  58. x.ArrivedAt,
  59. x.IpAddress,
  60. x.CreatedAt,
  61. SponsorID = x.Sponsor != null ? x.Sponsor.ID : 0,
  62. SponsorEmail = x.Sponsor != null ? x.Sponsor.Email : string.Empty,
  63. SponsorName = x.Sponsor != null ? x.Sponsor.Name : null,
  64. ReceiverID = x.Receiver != null ? x.Receiver.ID : 0,
  65. ReceiverEmail = x.Receiver != null ? x.Receiver.Email : string.Empty,
  66. ReceiverName = x.Receiver != null ? x.Receiver.Name : null
  67. })
  68. .ToListAsync(ct);
  69. var rows = list.Select((x, idx) => new Response.Row
  70. {
  71. Num = total - skip - idx,
  72. ID = x.ID,
  73. DonationID = x.DonationID,
  74. Sponsor = new Response.MemberInfo
  75. {
  76. ID = x.SponsorID,
  77. Email = x.SponsorEmail,
  78. Name = x.SponsorName
  79. },
  80. Receiver = new Response.MemberInfo
  81. {
  82. ID = x.ReceiverID,
  83. Email = x.ReceiverEmail,
  84. Name = x.ReceiverName
  85. },
  86. Status = x.Status.ToString(),
  87. IsReceived = x.Status == AlertStatus.Delivered,
  88. StartAt = x.StartAt,
  89. ArrivedAt = x.ArrivedAt,
  90. IpAddress = x.IpAddress,
  91. CreatedAt = x.CreatedAt
  92. }).ToList();
  93. return new Response
  94. {
  95. Total = total,
  96. Received = received,
  97. NotReceived = notReceived,
  98. List = rows
  99. };
  100. }
  101. }