Handler.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using Application.Abstractions.Data;
  2. using Application.Abstractions.Messaging;
  3. using Microsoft.EntityFrameworkCore;
  4. namespace Application.Features.Api.Stocks.GetSriBonds;
  5. /// <summary>
  6. /// 사회책임투자채권 목록 — 지정일(미지정 시 최신 TradeDate) 스냅샷을 발행금액 내림차순으로 페이징 (KRX 사회책임투자채권 수집, 익명).
  7. /// SriBondType(SRI_BND_TP_NM 채권종류: 녹색/사회적/지속가능) / BondType(BND_TP_NM 채권유형: 회사채 등) 지정 시 각각 해당 컬럼을 필터한다
  8. /// (감사 P3: 기존엔 bondType 파라미터가 SriBondType 컬럼을 대조해 '회사채' 전달 시 상시 0건이던 버그 수정).
  9. /// </summary>
  10. internal sealed class Handler(IAppDbContext db) : IQueryHandler<Query, Response>
  11. {
  12. private const ushort MaxPerPage = 100;
  13. public async Task<Response> Handle(Query request, CancellationToken ct)
  14. {
  15. var page = request.Page < 1 ? 1 : request.Page;
  16. var perPage = request.PerPage is 0 or > MaxPerPage ? (ushort)20 : request.PerPage;
  17. var targetDate = request.Date;
  18. if (targetDate is null)
  19. {
  20. targetDate = await db.SriBond.AsNoTracking()
  21. .OrderByDescending(c => c.TradeDate)
  22. .Select(c => (DateOnly?)c.TradeDate)
  23. .FirstOrDefaultAsync(ct);
  24. }
  25. if (targetDate is null)
  26. {
  27. return new Response
  28. {
  29. Total = 0,
  30. TradeDate = null,
  31. List = []
  32. };
  33. }
  34. var query = db.SriBond.AsNoTracking().Where(c => c.TradeDate == targetDate.Value);
  35. if (!string.IsNullOrWhiteSpace(request.SriBondType))
  36. {
  37. var sriBondType = request.SriBondType.Trim();
  38. query = query.Where(c => c.SriBondType == sriBondType);
  39. }
  40. if (!string.IsNullOrWhiteSpace(request.BondType))
  41. {
  42. var bondType = request.BondType.Trim();
  43. query = query.Where(c => c.BondType == bondType);
  44. }
  45. var total = await query.CountAsync(ct);
  46. var list = await query
  47. .OrderByDescending(c => c.IssueAmount).ThenBy(c => c.Code)
  48. .Skip((page - 1) * perPage)
  49. .Take(perPage)
  50. .Select(c => new Response.Row
  51. {
  52. Code = c.Code,
  53. IssuerName = c.IssuerName,
  54. SriBondType = c.SriBondType,
  55. Name = c.Name,
  56. ListDate = c.ListDate,
  57. IssueDate = c.IssueDate,
  58. RedemptionDate = c.RedemptionDate,
  59. CouponRate = c.CouponRate,
  60. IssueAmount = c.IssueAmount,
  61. ListAmount = c.ListAmount,
  62. BondType = c.BondType
  63. })
  64. .ToListAsync(ct);
  65. return new Response
  66. {
  67. Total = total,
  68. TradeDate = targetDate.Value,
  69. List = list
  70. };
  71. }
  72. }