Handler.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using Application.Abstractions.Data;
  2. using Application.Abstractions.Messaging;
  3. using Microsoft.EntityFrameworkCore;
  4. namespace Application.Features.Api.V1.Members.List;
  5. internal sealed class Handler(IAppDbContext db) : IQueryHandler<Query, Response>
  6. {
  7. public async Task<Response> Handle(Query request, CancellationToken ct)
  8. {
  9. var page = Math.Max(1, request.Page);
  10. var size = Math.Clamp(request.Size, 1, 200);
  11. var baseQuery = db.Member
  12. .AsNoTracking()
  13. .Where(c => !c.IsWithdraw && c.DeletedAt == null);
  14. // hasChannel 필터: true → 채널 보유, false → 채널 미보유, null → 전체
  15. if (request.HasChannel == true)
  16. {
  17. baseQuery = baseQuery.Where(c => c.Channel != null);
  18. }
  19. else if (request.HasChannel == false)
  20. {
  21. baseQuery = baseQuery.Where(c => c.Channel == null);
  22. }
  23. var total = await baseQuery.CountAsync(ct);
  24. var rows = await baseQuery
  25. .OrderByDescending(c => c.ID)
  26. .Skip((page - 1) * size)
  27. .Take(size)
  28. .Select(c => new
  29. {
  30. c.ID,
  31. c.SID,
  32. c.Name,
  33. c.Email,
  34. c.CreatedAt,
  35. ChannelSID = c.Channel != null ? c.Channel.SID : null,
  36. ChannelName = c.Channel != null ? c.Channel.Name : null
  37. })
  38. .ToListAsync(ct);
  39. var items = rows.Select(r => new Response.Row(
  40. r.ID,
  41. r.SID,
  42. r.Name,
  43. MaskEmail(r.Email),
  44. r.ChannelSID != null,
  45. r.ChannelSID,
  46. r.ChannelName,
  47. r.CreatedAt
  48. )).ToList();
  49. return new Response(total, page, size, items);
  50. }
  51. private static string MaskEmail(string email)
  52. {
  53. if (string.IsNullOrWhiteSpace(email))
  54. {
  55. return string.Empty;
  56. }
  57. var atIndex = email.IndexOf('@');
  58. if (atIndex <= 0)
  59. {
  60. return "***";
  61. }
  62. var local = email[..atIndex];
  63. var domain = email[(atIndex + 1)..];
  64. var prefix = local.Length <= 2 ? local[..1] : local[..2];
  65. return $"{prefix}***@{domain}";
  66. }
  67. }