using Application.Abstractions.Data;
using Application.Abstractions.Messaging;
using Domain.Entities.Stocks.ValueObject;
using Microsoft.EntityFrameworkCore;
namespace Application.Features.Api.Stocks.GetInterestRates;
///
/// 금리 목록 — 국제금리는 Yahoo 수집분(MarketQuoteSnapshot, Category=BondYield·GroupCode="macro")의 미국채 수익률(%)을 반환 (익명).
/// koreaexim AP02/AP03 은 봇차단 WAF 로 이 서버망에서 수집 불가 → 이미 수집 중인 Yahoo 매크로 시세로 소스 전환 (2026-07-14).
/// 대출금리(Loan)는 Yahoo 대응이 없어 빈 목록으로 반환한다(개요 화면은 International 만 사용). Date 파라미터는 스냅샷 특성상 무시(항상 최신).
///
internal sealed class Handler(IAppDbContext db) : IQueryHandler
{
public async Task Handle(Query request, CancellationToken ct)
{
// Yahoo 는 국제금리(미국채 수익률)만 제공 — 대출금리(Loan)는 미수집
if (request.Type != RateType.International)
{
return new Response
{
Type = request.Type,
Total = 0,
TradeDate = null,
List = []
};
}
var rows = await db.MarketQuoteSnapshot.AsNoTracking()
.Where(c => c.Category == QuoteCategory.BondYield && c.GroupCode == "macro")
.OrderBy(c => c.Symbol)
.Select(c => new { c.Name, c.Close, c.TradeDate })
.ToListAsync(ct);
if (rows.Count == 0)
{
return new Response
{
Type = request.Type,
Total = 0,
TradeDate = null,
List = []
};
}
var list = rows
.Select(c => new Response.Row
{
ItemName = c.Name,
Rate = c.Close
})
.ToList();
return new Response
{
Type = request.Type,
Total = list.Count,
TradeDate = rows.Max(c => c.TradeDate),
List = list
};
}
}