Skip to content

Commit

Permalink
feat: add NUTS3 codes
Browse files Browse the repository at this point in the history
  • Loading branch information
ArneD authored and jvandaal committed Oct 6, 2023
1 parent 11f6efb commit 3b96d35
Show file tree
Hide file tree
Showing 8 changed files with 1,259 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace PostalRegistry.Api.Oslo.Infrastructure.Modules
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Nuts;
using Projections.Legacy;
using Projections.Syndication.Modules;

Expand Down Expand Up @@ -39,6 +40,9 @@ protected override void Load(ContainerBuilder builder)
.RegisterType<ProblemDetailsHelper>()
.AsSelf();

builder.RegisterType<Nuts3Service>()
.SingleInstance();

builder.Populate(_services);
}
}
Expand Down
59 changes: 59 additions & 0 deletions src/PostalRegistry.Api.Oslo/Nuts/Nuts3Service.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
namespace PostalRegistry.Api.Oslo.Nuts
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using CsvHelper;
using CsvHelper.Configuration;

public sealed class Nuts3Service
{
private const string Nuts3FileName = "nuts3_postal_v1.csv";
private readonly List<Nuts3Record> _nuts3Records = new List<Nuts3Record>();

public Nuts3Service()
{
var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Nuts", Nuts3FileName);

if (!File.Exists(filePath))
{
// Handle the case where the file does not exist
throw new FileNotFoundException("NUTS3 CSV file not found.");
}

var csvConfiguration = new CsvConfiguration(CultureInfo.InvariantCulture){
Delimiter = ";",
HasHeaderRecord = true,
Encoding = Encoding.UTF8,
Quote = '\''
};

using var reader = new StreamReader(filePath);
using var csvReader = new CsvReader(reader, csvConfiguration);
csvReader.Read(); // skip first record Header as ReadHeader doesn't work

while (csvReader.Read())
{
var nuts3Code = csvReader.GetField<string>(0);
var postalCode = csvReader.GetField<string>(1);

_nuts3Records.Add(new Nuts3Record(nuts3Code, postalCode));
}
}

public Nuts3Record? GetNuts3ByPostalCode(string postalCode)
{
return _nuts3Records.FirstOrDefault(x => x.PostalCode == postalCode);
}

public IEnumerable<Nuts3Record> GetPostalCodesByNuts3(string nuts3Code)
{
return _nuts3Records.Where(x => string.Equals(x.Nuts3Code, nuts3Code, StringComparison.OrdinalIgnoreCase));
}
}

public record Nuts3Record(string Nuts3Code, string PostalCode);
}
Loading

0 comments on commit 3b96d35

Please sign in to comment.