From 87417c313b455ba0dee19708528cc8d0b830a276 Mon Sep 17 00:00:00 2001 From: Ariel Costas Guerrero Date: Tue, 23 Dec 2025 12:59:52 +0100 Subject: Reimplement real time for Vitrasa --- .../Controllers/TileController.cs | 62 +++++----------------- 1 file changed, 12 insertions(+), 50 deletions(-) (limited to 'src/Costasdev.Busurbano.Backend/Controllers/TileController.cs') diff --git a/src/Costasdev.Busurbano.Backend/Controllers/TileController.cs b/src/Costasdev.Busurbano.Backend/Controllers/TileController.cs index 6354d67..0e9d21b 100644 --- a/src/Costasdev.Busurbano.Backend/Controllers/TileController.cs +++ b/src/Costasdev.Busurbano.Backend/Controllers/TileController.cs @@ -10,6 +10,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Caching.Memory; using System.Text.Json; using Costasdev.Busurbano.Backend.Helpers; +using Costasdev.Busurbano.Backend.Services; namespace Costasdev.Busurbano.Backend.Controllers; @@ -20,29 +21,21 @@ public class TileController : ControllerBase private readonly ILogger _logger; private readonly IMemoryCache _cache; private readonly HttpClient _httpClient; + private readonly FeedService _feedService; public TileController( ILogger logger, IMemoryCache cache, - HttpClient httpClient + HttpClient httpClient, + FeedService feedService ) { _logger = logger; _cache = cache; _httpClient = httpClient; + _feedService = feedService; } - private static readonly string[] HiddenStops = - [ - "vitrasa:20223", // Castrelos (Pavillón - U1) - "vitrasa:20146", // García Barbón, 7 (A, 18A) - "vitrasa:20220", // COIA-SAMIL (15) - "vitrasa:20001", // Samil por Beiramar (15B) - "vitrasa:20002", // Samil por Torrecedeira (15C) - "vitrasa:20144", // Samil por Coia (C3d, C3i) - "vitrasa:20145" // Samil por Bouzs (C3d, C3i) - ]; - [HttpGet("stops/{z:int}/{x:int}/{y:int}")] public async Task Stops(int z, int x, int y) { @@ -97,24 +90,14 @@ public class TileController : ControllerBase { var idParts = stop.GtfsId.Split(':', 2); string feedId = idParts[0]; - string codeWithinFeed = stop.Code ?? string.Empty; + string codeWithinFeed = _feedService.NormalizeStopCode(feedId, stop.Code ?? string.Empty); - // TODO: Refactor this, maybe do it client-side or smth - if (feedId == "vitrasa") - { - var digits = new string(codeWithinFeed.Where(char.IsDigit).ToArray()); - if (int.TryParse(digits, out int code)) - { - codeWithinFeed = code.ToString(); - } - } - - if (HiddenStops.Contains($"{feedId}:{codeWithinFeed}")) + if (_feedService.IsStopHidden($"{feedId}:{codeWithinFeed}")) { return; } - var (Color, TextColor) = GetFallbackColourForFeed(idParts[0]); + var (Color, TextColor) = _feedService.GetFallbackColourForFeed(idParts[0]); var distinctRoutes = GetDistinctRoutes(feedId, stop.Routes ?? []); Feature feature = new() @@ -129,7 +112,7 @@ public class TileController : ControllerBase // The public identifier, usually feed:code or feed:id, recognisable by users and in other systems { "code", $"{idParts[0]}:{codeWithinFeed}" }, // The name of the stop - { "name", stop.Name }, + { "name", _feedService.NormalizeStopName(feedId, stop.Name) }, // Routes { "routes", JsonSerializer .Serialize( @@ -176,21 +159,15 @@ public class TileController : ControllerBase return File(ms.ToArray(), "application/x-protobuf"); } - private static List GetDistinctRoutes(string feedId, List routes) + private List GetDistinctRoutes(string feedId, List routes) { List distinctRoutes = []; HashSet seen = new(); foreach (var route in routes) { - var seenId = route.ShortName; - if (feedId == "xunta") - { - // For Xunta routes we take only the contract number (XG123, for example) - seenId = seenId.Substring(0, 5); - - route.ShortName = seenId; - } + var seenId = _feedService.NormalizeRouteShortName(feedId, route.ShortName ?? string.Empty); + route.ShortName = seenId; if (seen.Contains(seenId)) { @@ -206,19 +183,4 @@ public class TileController : ControllerBase Comparer.Create(SortingHelper.SortRouteShortNames) )]; } - - private static (string Color, string TextColor) GetFallbackColourForFeed(string feed) - { - return feed switch - { - "vitrasa" => ("#95D516", "#000000"), - "santiago" => ("#508096", "#FFFFFF"), - "coruna" => ("#E61C29", "#FFFFFF"), - "xunta" => ("#007BC4", "#FFFFFF"), - "renfe" => ("#870164", "#FFFFFF"), - "feve" => ("#EE3D32", "#FFFFFF"), - _ => ("#000000", "#FFFFFF"), - - }; - } } -- cgit v1.3