diff options
| author | Ariel Costas Guerrero <ariel@costas.dev> | 2025-11-06 16:48:43 +0100 |
|---|---|---|
| committer | Ariel Costas Guerrero <ariel@costas.dev> | 2025-11-06 16:48:43 +0100 |
| commit | 817b29603e5b7f79bfe6489eebf73961e6ca93f2 (patch) | |
| tree | 09057b38ca6ed23ce7852b62ae1d5c97a91cfe9b /src/Costasdev.Busurbano.Backend/Controllers/SantiagoController.cs | |
| parent | 94658119f2c0b555f83e870fbeb50150f18fc38d (diff) | |
Move controllers, add config, read stop schedules from local directory
Diffstat (limited to 'src/Costasdev.Busurbano.Backend/Controllers/SantiagoController.cs')
| -rw-r--r-- | src/Costasdev.Busurbano.Backend/Controllers/SantiagoController.cs | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/src/Costasdev.Busurbano.Backend/Controllers/SantiagoController.cs b/src/Costasdev.Busurbano.Backend/Controllers/SantiagoController.cs new file mode 100644 index 0000000..24ecab9 --- /dev/null +++ b/src/Costasdev.Busurbano.Backend/Controllers/SantiagoController.cs @@ -0,0 +1,72 @@ +using System.Text.Json; +using Costasdev.VigoTransitApi.Types; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Caching.Memory; + +namespace Costasdev.Busurbano.Backend.Controllers; + +[ApiController] +[Route("api/santiago")] +public class SantiagoController : ControllerBase +{ + private readonly IMemoryCache _cache; + private readonly HttpClient _httpClient; + + public SantiagoController(HttpClient http, IMemoryCache cache) + { + _cache = cache; + _httpClient = http; + } + + [HttpGet("GetStopEstimates")] + public async Task<IActionResult> Run() + { + var argumentAvailable = Request.Query.TryGetValue("id", out var requestedStopIdString); + if (!argumentAvailable) + { + return BadRequest("Please provide a stop id as a query parameter with the name 'id'."); + } + + var argumentNumber = int.TryParse(requestedStopIdString, out var requestedStopId); + if (!argumentNumber) + { + return BadRequest("The provided stop id is not a valid number."); + } + + try + { + var obj = await _httpClient.GetFromJsonAsync<JsonDocument>( + $"https://app.tussa.org/tussa/api/paradas/{requestedStopId}"); + + if (obj is null) + { + return BadRequest("No response returned from the API, or whatever"); + } + + var root = obj.RootElement; + + List<StopEstimate> estimates = root + .GetProperty("lineas") + .EnumerateArray() + .Select(el => new StopEstimate( + el.GetProperty("sinoptico").GetString() ?? string.Empty, + el.GetProperty("nombre").GetString() ?? string.Empty, + el.GetProperty("minutosProximoPaso").GetInt32(), + 0 + )).ToList(); + + // Return only the estimates array, not the stop metadata + return new OkObjectResult(estimates); + } + catch (InvalidOperationException) + { + return BadRequest("Stop not found"); + } + } + + [HttpGet("GetStopTimetable")] + public async Task<IActionResult> GetStopTimetable() + { + throw new NotImplementedException(); + } +} |
