using Backend.ServiceHandler; using Backend.MarketDataRequest; using Backend.Interface; using Microsoft.AspNetCore.Mvc; using System.Globalization; namespace Backend.Controllers { [ApiController] [Route("api/market")] public class MarketController : ControllerBase { private readonly MarketDataService _marketDataService; private readonly IHistoricalMarketDataCache _cache; public MarketController(MarketDataService marketDataService, IHistoricalMarketDataCache cache) { _marketDataService = marketDataService; _cache = cache; } [HttpGet("last-trading-day")] public async Task GetLastTradingDay( [FromQuery] string symbol = "SPY", CancellationToken cancellationToken = default) { var lastDay = await _cache.GetLastTradingDayAsync( symbol.Trim().ToUpperInvariant(), cancellationToken); if (lastDay is null) return Ok(new { found = false, lastTradingDay = (string?)null }); return Ok(new { found = true, lastTradingDay = lastDay.Value.ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture) }); } [HttpGet("symbol-check/{symbol}")] public IActionResult CheckSymbol(string symbol) { var (found, message) = _marketDataService.CheckSymbol(symbol.ToUpperInvariant()); return Ok(new { found, message }); } [HttpGet("live/{symbol}")] public IActionResult GetLive(string symbol) { var data = _marketDataService.GetLiveQuote(symbol.ToUpperInvariant()); return Ok(data); } [HttpGet("historical")] public async Task GetHistorical( [FromQuery] string symbol, [FromQuery] string endDateTime, [FromQuery] string duration, [FromQuery] HistoricalBarSize barSize, [FromQuery] HistoricalWhatToShow whatToShow = HistoricalWhatToShow.Trades, [FromQuery] bool useRth = true) { var data = await _marketDataService.GetHistoricalAsync( symbol.ToUpperInvariant(), endDateTime, duration, barSize, whatToShow, useRth ); return Ok(data); } [HttpGet("historical-line")] public async Task GetHistoricalLine( [FromQuery] string symbol, [FromQuery] string endDateTime, [FromQuery] string duration, [FromQuery] HistoricalBarSize barSize, [FromQuery] HistoricalWhatToShow whatToShow = HistoricalWhatToShow.Trades, [FromQuery] bool useRth = true) { var data = await _marketDataService.GetHistoricalLineAsync( symbol.ToUpperInvariant(), endDateTime, duration, barSize, whatToShow, useRth ); return Ok(data); } } }