aboutsummaryrefslogtreecommitdiff
path: root/src/Costasdev.Busurbano.Backend/SantiagoController.cs
diff options
context:
space:
mode:
authorAriel Costas Guerrero <ariel@costas.dev>2025-10-21 17:38:01 +0200
committerAriel Costas Guerrero <ariel@costas.dev>2025-10-21 17:38:01 +0200
commit12ecc97b07093f3cac6567c70ff75d57b429c674 (patch)
treecf4ec0abe4e1d20c01c62e0fc04af5eaa885e881 /src/Costasdev.Busurbano.Backend/SantiagoController.cs
parent67c1dd5cb0025235c29ebd1f1706e5c17392dbff (diff)
Implement new Santiago region (WIP)
Diffstat (limited to 'src/Costasdev.Busurbano.Backend/SantiagoController.cs')
-rw-r--r--src/Costasdev.Busurbano.Backend/SantiagoController.cs81
1 files changed, 81 insertions, 0 deletions
diff --git a/src/Costasdev.Busurbano.Backend/SantiagoController.cs b/src/Costasdev.Busurbano.Backend/SantiagoController.cs
new file mode 100644
index 0000000..51a76c6
--- /dev/null
+++ b/src/Costasdev.Busurbano.Backend/SantiagoController.cs
@@ -0,0 +1,81 @@
+using System.Text.Json;
+using Costasdev.VigoTransitApi.Types;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.Extensions.Caching.Memory;
+
+namespace Costasdev.Busurbano.Backend;
+
+[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 new OkObjectResult(new StopEstimateResponse
+ {
+ Stop = new StopEstimateResponse.StopInfo
+ {
+ Name = root.GetProperty("nombre").GetString() ?? string.Empty,
+ Id = root.GetProperty("id").GetInt32(),
+ Latitude = root.GetProperty("coordenadas").GetProperty("latitud").GetDecimal(),
+ Longitude = root.GetProperty("coordenadas").GetProperty("longitud").GetDecimal()
+ },
+ Estimates = estimates
+ });
+ }
+ catch (InvalidOperationException)
+ {
+ return new BadRequestObjectResult("Stop not found");
+ }
+ }
+
+ [HttpGet("GetStopTimetable")]
+ public async Task<IActionResult> GetStopTimetable()
+ {
+ throw new NotImplementedException();
+ }
+}