From 6192730d1b7b0d08095d7da88caba73fd07fe99e Mon Sep 17 00:00:00 2001 From: Ariel Costas Guerrero Date: Sun, 25 Jan 2026 21:35:36 +0100 Subject: Bring back basic stop search --- src/Enmarcha.Backend/Services/OtpService.cs | 37 +++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'src/Enmarcha.Backend/Services') diff --git a/src/Enmarcha.Backend/Services/OtpService.cs b/src/Enmarcha.Backend/Services/OtpService.cs index e4b4846..1484583 100644 --- a/src/Enmarcha.Backend/Services/OtpService.cs +++ b/src/Enmarcha.Backend/Services/OtpService.cs @@ -5,6 +5,7 @@ using Enmarcha.Backend.Helpers; using Enmarcha.Backend.Types.Otp; using Enmarcha.Backend.Types.Planner; using Enmarcha.Backend.Types.Transit; +using Enmarcha.Sources.OpenTripPlannerGql; using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Options; @@ -89,6 +90,42 @@ public class OtpService }; } + public async Task> GetStopsByBboxAsync(double minLon, double minLat, double maxLon, double maxLat) + { + const string cacheKey = "otp_all_stops_detailed"; + if (_cache.TryGetValue(cacheKey, out List? cachedStops) && cachedStops != null) + { + return cachedStops; + } + + try + { + var bbox = new StopTileRequestContent.Bbox(minLon, minLat, maxLon, maxLat); + var query = StopTileRequestContent.Query(bbox); + + var request = new HttpRequestMessage(HttpMethod.Post, $"{_config.OpenTripPlannerBaseUrl}/gtfs/v1"); + request.Content = JsonContent.Create(new GraphClientRequest { Query = query }); + + var response = await _httpClient.SendAsync(request); + var responseBody = await response.Content.ReadFromJsonAsync>(); + + if (responseBody is not { IsSuccess: true } || responseBody.Data?.StopsByBbox == null) + { + _logger.LogError("Error fetching stops from OTP for caching"); + return new List(); + } + + var stops = responseBody.Data.StopsByBbox; + _cache.Set(cacheKey, stops, TimeSpan.FromHours(18)); + return stops; + } + catch (Exception ex) + { + _logger.LogError(ex, "Exception fetching stops from OTP for caching"); + return new List(); + } + } + private Leg MapLeg(OtpLeg otpLeg) { return new Leg -- cgit v1.3