| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- using Application.Abstractions.Data;
- using Application.Abstractions.Messaging;
- using Microsoft.EntityFrameworkCore;
- namespace Application.Features.Api.Stocks.GetInterestRates;
- /// <summary>
- /// 금리 목록 — 종류(대출/국제)별, 지정일(미지정 시 최신 TradeDate)의 항목별 금리를 항목명 오름차순으로 반환
- /// (수출입은행 AP02/AP03 수집, 익명).
- /// </summary>
- internal sealed class Handler(IAppDbContext db) : IQueryHandler<Query, Response>
- {
- public async Task<Response> Handle(Query request, CancellationToken ct)
- {
- var targetDate = request.Date;
- if (targetDate is null)
- {
- targetDate = await db.InterestRate.AsNoTracking()
- .Where(c => c.RateType == request.Type)
- .OrderByDescending(c => c.TradeDate)
- .Select(c => (DateOnly?)c.TradeDate)
- .FirstOrDefaultAsync(ct);
- }
- if (targetDate is null)
- {
- return new Response
- {
- Type = request.Type,
- Total = 0,
- TradeDate = null,
- List = []
- };
- }
- var list = await db.InterestRate.AsNoTracking()
- .Where(c => c.RateType == request.Type && c.TradeDate == targetDate.Value)
- .OrderBy(c => c.ItemName)
- .Select(c => new Response.Row
- {
- ItemName = c.ItemName,
- Rate = c.Rate
- })
- .ToListAsync(ct);
- return new Response
- {
- Type = request.Type,
- Total = list.Count,
- TradeDate = targetDate.Value,
- List = list
- };
- }
- }
|