aboutsummaryrefslogtreecommitdiff
path: root/src/Enmarcha.Backend/Services/Providers/XuntaFareProvider.cs
diff options
context:
space:
mode:
authorAriel Costas Guerrero <ariel@costas.dev>2025-12-29 00:41:52 +0100
committerAriel Costas Guerrero <ariel@costas.dev>2025-12-29 00:41:52 +0100
commita304c24b32c0327436bbd8c2853e60668e161b42 (patch)
tree08f65c05daca134cf4d2e4f779bd15d98fd66370 /src/Enmarcha.Backend/Services/Providers/XuntaFareProvider.cs
parent120a3c6bddd0fb8d9fa05df4763596956554c025 (diff)
Rename a lot of stuff, add Santiago real time
Diffstat (limited to 'src/Enmarcha.Backend/Services/Providers/XuntaFareProvider.cs')
-rw-r--r--src/Enmarcha.Backend/Services/Providers/XuntaFareProvider.cs57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/Enmarcha.Backend/Services/Providers/XuntaFareProvider.cs b/src/Enmarcha.Backend/Services/Providers/XuntaFareProvider.cs
new file mode 100644
index 0000000..4bb60e2
--- /dev/null
+++ b/src/Enmarcha.Backend/Services/Providers/XuntaFareProvider.cs
@@ -0,0 +1,57 @@
+using System.Collections.Frozen;
+using System.Globalization;
+using CsvHelper;
+using CsvHelper.Configuration.Attributes;
+
+namespace Enmarcha.Backend.Services.Providers;
+
+public class PriceRecord
+{
+ [Name("conc_inicio")] public string Origin { get; set; }
+ [Name("conc_fin")] public string Destination { get; set; }
+ [Name("bonificacion")] public string? MetroArea { get; set; }
+ [Name("efectivo")] public decimal PriceCash { get; set; }
+ [Name("tpg")] public decimal PriceCard { get; set; }
+}
+
+public class XuntaFareProvider
+{
+ private readonly FrozenDictionary<(string, string), PriceRecord> _priceMatrix;
+
+ public XuntaFareProvider(IWebHostEnvironment env)
+ {
+ var filePath = Path.Combine(env.ContentRootPath, "Data", "xunta_fares.csv");
+
+ using var reader = new StreamReader(filePath);
+ using var csv = new CsvReader(reader, CultureInfo.InvariantCulture);
+
+ // We do GroupBy first to prevent duplicates from throwing an exception
+ _priceMatrix = csv.GetRecords<PriceRecord>()
+ .GroupBy(record => (record.Origin, record.Destination))
+ .ToFrozenDictionary(
+ group => group.Key,
+ group => group.First()
+ );
+ }
+
+ public PriceRecord? GetPrice(string origin, string destination)
+ {
+ var originMunicipality = origin[..5];
+ var destinationMunicipality = destination[..5];
+
+ var valueOrDefault = _priceMatrix.GetValueOrDefault((originMunicipality, destinationMunicipality));
+
+ /* This happens in cases where traffic is forbidden (like inside municipalities with urban transit */
+ if (valueOrDefault?.PriceCash == 0.0M)
+ {
+ valueOrDefault.PriceCash = 100;
+ }
+
+ if (valueOrDefault?.PriceCard == 0.0M)
+ {
+ valueOrDefault.PriceCard = 100;
+ }
+
+ return valueOrDefault;
+ }
+}