using Application.Abstractions.Data;
using Application.Abstractions.Messaging;
using Microsoft.EntityFrameworkCore;
namespace Application.Features.Api.Stocks.GetInterestRates;
///
/// 금리 목록 — 종류(대출/국제)별, 지정일(미지정 시 최신 TradeDate)의 항목별 금리를 항목명 오름차순으로 반환
/// (수출입은행 AP02/AP03 수집, 익명).
///
internal sealed class Handler(IAppDbContext db) : IQueryHandler
{
public async Task 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
};
}
}