| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- using Application.Abstractions.Data;
- using Application.Abstractions.Messaging;
- using Microsoft.EntityFrameworkCore;
- namespace Application.Features.Api.Stocks.GetOptions;
- /// <summary>
- /// 옵션(일반/주식유가/주식코스닥) 목록 — 상품군·세션(기본 정규)별 지정일(미지정 시 최신 TradeDate) 행을 거래대금 내림차순으로 페이징 (KRX 파생상품 수집, 익명).
- /// </summary>
- internal sealed class Handler(IAppDbContext db) : IQueryHandler<Query, Response>
- {
- private const ushort MaxPerPage = 100;
- public async Task<Response> Handle(Query request, CancellationToken ct)
- {
- var page = request.Page < 1 ? 1 : request.Page;
- var perPage = request.PerPage is 0 or > MaxPerPage ? (ushort)20 : request.PerPage;
- var targetDate = request.Date;
- if (targetDate is null)
- {
- targetDate = await db.OptionsDailyTrade.AsNoTracking()
- .Where(c => c.OptionsKind == request.Kind && c.Session == request.Session)
- .OrderByDescending(c => c.TradeDate)
- .Select(c => (DateOnly?)c.TradeDate)
- .FirstOrDefaultAsync(ct);
- }
- if (targetDate is null)
- {
- return new Response
- {
- Total = 0,
- TradeDate = null,
- List = []
- };
- }
- var query = db.OptionsDailyTrade.AsNoTracking().Where(c => c.OptionsKind == request.Kind && c.TradeDate == targetDate.Value && c.Session == request.Session);
- var total = await query.CountAsync(ct);
- var list = await query
- .OrderByDescending(c => c.TradeValue).ThenBy(c => c.IsuCode)
- .Skip((page - 1) * perPage)
- .Take(perPage)
- .Select(c => new Response.Row
- {
- Kind = c.OptionsKind,
- Session = c.Session,
- IsuCode = c.IsuCode,
- IsuName = c.IsuName,
- RightType = c.RightType,
- StrikePrice = c.StrikePrice,
- Close = c.Close,
- ChangeAmount = c.ChangeAmount,
- ImpliedVolatility = c.ImpliedVolatility,
- Volume = c.Volume,
- TradeValue = c.TradeValue,
- OpenInterest = c.OpenInterest,
- ProductName = c.ProductName
- })
- .ToListAsync(ct);
- return new Response
- {
- Total = total,
- TradeDate = targetDate.Value,
- List = list
- };
- }
- }
|