aboutsummaryrefslogtreecommitdiff
path: root/src/Costasdev.Busurbano.Backend
diff options
context:
space:
mode:
Diffstat (limited to 'src/Costasdev.Busurbano.Backend')
-rw-r--r--src/Costasdev.Busurbano.Backend/Controllers/VigoController.Legacy.cs78
-rw-r--r--src/Costasdev.Busurbano.Backend/Controllers/VigoController.cs79
-rw-r--r--src/Costasdev.Busurbano.Backend/Services/LineFormatterService.cs51
3 files changed, 131 insertions, 77 deletions
diff --git a/src/Costasdev.Busurbano.Backend/Controllers/VigoController.Legacy.cs b/src/Costasdev.Busurbano.Backend/Controllers/VigoController.Legacy.cs
new file mode 100644
index 0000000..3bb9930
--- /dev/null
+++ b/src/Costasdev.Busurbano.Backend/Controllers/VigoController.Legacy.cs
@@ -0,0 +1,78 @@
+using System.Globalization;
+using System.Text.Json;
+using Costasdev.Busurbano.Backend.Types;
+using Microsoft.AspNetCore.Mvc;
+using SysFile = System.IO.File;
+
+namespace Costasdev.Busurbano.Backend.Controllers;
+
+public partial class VigoController : ControllerBase
+{
+ [HttpGet("GetStopEstimates")]
+ public async Task<IActionResult> Run(
+ [FromQuery] int id
+ )
+ {
+ try
+ {
+ var response = await _api.GetStopEstimates(id);
+ // Return only the estimates array, not the stop metadata
+ return new OkObjectResult(response.Estimates);
+ }
+ catch (InvalidOperationException)
+ {
+ return BadRequest("Stop not found");
+ }
+ }
+
+ [HttpGet("GetStopTimetable")]
+ public async Task<IActionResult> GetStopTimetable(
+ [FromQuery] int stopId,
+ [FromQuery] string? date = null
+)
+ {
+ // Use Europe/Madrid timezone to determine the correct date
+ var tz = TimeZoneInfo.FindSystemTimeZoneById("Europe/Madrid");
+ var nowLocal = TimeZoneInfo.ConvertTime(DateTime.UtcNow, tz);
+
+ // If no date provided or date is "today", use Madrid timezone's current date
+ string effectiveDate;
+ if (string.IsNullOrEmpty(date) || date == "today")
+ {
+ effectiveDate = nowLocal.Date.ToString("yyyy-MM-dd");
+ }
+ else
+ {
+ // Validate provided date format
+ if (!DateTime.TryParseExact(date, "yyyy-MM-dd", null, DateTimeStyles.None, out _))
+ {
+ return BadRequest("Invalid date format. Please use yyyy-MM-dd format.");
+ }
+ effectiveDate = date;
+ }
+
+ try
+ {
+ var file = Path.Combine(_configuration.ScheduleBasePath, effectiveDate, stopId + ".json");
+ if (!SysFile.Exists(file))
+ {
+ throw new FileNotFoundException();
+ }
+
+ var contents = await SysFile.ReadAllTextAsync(file);
+
+ return new OkObjectResult(JsonSerializer.Deserialize<List<ScheduledStop>>(contents)!);
+ }
+ catch (FileNotFoundException ex)
+ {
+ _logger.LogError(ex, "Stop data not found for stop {StopId} on date {Date}", stopId, effectiveDate);
+ return StatusCode(404, $"Stop data not found for stop {stopId} on date {effectiveDate}");
+ }
+ catch (Exception ex)
+ {
+ _logger.LogError(ex, "Error loading stop data");
+ return StatusCode(500, "Error loading timetable");
+ }
+ }
+
+}
diff --git a/src/Costasdev.Busurbano.Backend/Controllers/VigoController.cs b/src/Costasdev.Busurbano.Backend/Controllers/VigoController.cs
index 1d9e519..a6aafe8 100644
--- a/src/Costasdev.Busurbano.Backend/Controllers/VigoController.cs
+++ b/src/Costasdev.Busurbano.Backend/Controllers/VigoController.cs
@@ -1,6 +1,5 @@
using System.Globalization;
using System.Text;
-using System.Text.Json;
using Costasdev.Busurbano.Backend.Configuration;
using Costasdev.Busurbano.Backend.Services;
using Costasdev.Busurbano.Backend.Types;
@@ -14,7 +13,7 @@ namespace Costasdev.Busurbano.Backend.Controllers;
[ApiController]
[Route("api/vigo")]
-public class VigoController : ControllerBase
+public partial class VigoController : ControllerBase
{
private readonly ILogger<VigoController> _logger;
private readonly VigoTransitApiClient _api;
@@ -29,23 +28,6 @@ public class VigoController : ControllerBase
_shapeService = shapeService;
}
- [HttpGet("GetStopEstimates")]
- public async Task<IActionResult> Run(
- [FromQuery] int id
- )
- {
- try
- {
- var response = await _api.GetStopEstimates(id);
- // Return only the estimates array, not the stop metadata
- return new OkObjectResult(response.Estimates);
- }
- catch (InvalidOperationException)
- {
- return BadRequest("Stop not found");
- }
- }
-
[HttpGet("GetShape")]
public async Task<IActionResult> GetShape(
[FromQuery] string shapeId,
@@ -124,56 +106,6 @@ public class VigoController : ControllerBase
return Ok(geoJson);
}
- [HttpGet("GetStopTimetable")]
- public async Task<IActionResult> GetStopTimetable(
- [FromQuery] int stopId,
- [FromQuery] string? date = null
- )
- {
- // Use Europe/Madrid timezone to determine the correct date
- var tz = TimeZoneInfo.FindSystemTimeZoneById("Europe/Madrid");
- var nowLocal = TimeZoneInfo.ConvertTime(DateTime.UtcNow, tz);
-
- // If no date provided or date is "today", use Madrid timezone's current date
- string effectiveDate;
- if (string.IsNullOrEmpty(date) || date == "today")
- {
- effectiveDate = nowLocal.Date.ToString("yyyy-MM-dd");
- }
- else
- {
- // Validate provided date format
- if (!DateTime.TryParseExact(date, "yyyy-MM-dd", null, DateTimeStyles.None, out _))
- {
- return BadRequest("Invalid date format. Please use yyyy-MM-dd format.");
- }
- effectiveDate = date;
- }
-
- try
- {
- var file = Path.Combine(_configuration.ScheduleBasePath, effectiveDate, stopId + ".json");
- if (!SysFile.Exists(file))
- {
- throw new FileNotFoundException();
- }
-
- var contents = await SysFile.ReadAllTextAsync(file);
-
- return new OkObjectResult(JsonSerializer.Deserialize<List<ScheduledStop>>(contents)!);
- }
- catch (FileNotFoundException ex)
- {
- _logger.LogError(ex, "Stop data not found for stop {StopId} on date {Date}", stopId, effectiveDate);
- return StatusCode(404, $"Stop data not found for stop {stopId} on date {effectiveDate}");
- }
- catch (Exception ex)
- {
- _logger.LogError(ex, "Error loading stop data");
- return StatusCode(500, "Error loading timetable");
- }
- }
-
[HttpGet("GetConsolidatedCirculations")]
public async Task<IActionResult> GetConsolidatedCirculations(
[FromQuery] int stopId
@@ -415,6 +347,7 @@ public class VigoController : ControllerBase
// Sort by ETA (RealTime minutes if present; otherwise Schedule minutes)
var sorted = consolidatedCirculations
.OrderBy(c => c.RealTime?.Minutes ?? c.Schedule!.Minutes)
+ .Select(LineFormatterService.Format)
.ToList();
return Ok(sorted);
@@ -452,7 +385,6 @@ public class VigoController : ControllerBase
var normalized = route.Trim().ToLowerInvariant();
// Remove diacritics/accents first, then filter to alphanumeric
normalized = RemoveDiacritics(normalized);
- normalized = RenameCustom(normalized);
return new string(normalized.Where(char.IsLetterOrDigit).ToArray());
}
@@ -472,13 +404,6 @@ public class VigoController : ControllerBase
return stringBuilder.ToString().Normalize(NormalizationForm.FormC);
}
-
- private static string RenameCustom(string text)
- {
- // Custom replacements for known problematic route names
- return text
- .Replace("praza", "p");
- }
}
public static class StopScheduleExtensions
diff --git a/src/Costasdev.Busurbano.Backend/Services/LineFormatterService.cs b/src/Costasdev.Busurbano.Backend/Services/LineFormatterService.cs
new file mode 100644
index 0000000..788634d
--- /dev/null
+++ b/src/Costasdev.Busurbano.Backend/Services/LineFormatterService.cs
@@ -0,0 +1,51 @@
+using Costasdev.Busurbano.Backend.Types;
+
+namespace Costasdev.Busurbano.Backend.Services;
+
+public class LineFormatterService
+{
+ public static ConsolidatedCirculation Format(ConsolidatedCirculation circulation)
+ {
+ circulation.Route = circulation.Route.Replace("*", "");
+
+ if (circulation.Line == "18A")
+ {
+ circulation.Route = circulation.Route
+ .Replace("\"A\" ", "")
+ .Trim()
+ .Replace("SARDOMA por MANTELAS", "Praza de Miraflores");
+ }
+
+ if (circulation.Line == "5A")
+ {
+ circulation.Route = circulation.Route
+ .Replace("Rúa da Travesía de Vigo, 220", "URZAIZ - TVA DE VIGO");
+ }
+
+ if (circulation.Line == "5B")
+ {
+ circulation.Route = circulation.Route
+ .Replace("Rúa de Sanjurjo Badía, 252", "S. BADIA - TVA DE VIGO");
+ }
+
+ if (circulation.Line == "11")
+ {
+ circulation.Route = circulation.Route
+ .Replace("Avda. de Cesáreo Vázquez, 61", "SAN MIGUEL por FLORIDA");
+ }
+
+ if (circulation.Line == "4C")
+ {
+ circulation.Route = circulation.Route
+ .Replace("Rúa do Porriño (fronte 9)", "COIA POR CASTELAO");
+ }
+
+ if (circulation.Line == "6")
+ {
+ circulation.Route = circulation.Route
+ .Replace("\"", "");
+ }
+
+ return circulation;
+ }
+}