Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: return multiple error messages at once for propose for municipal… #419

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public async Task<IActionResult> ProposeForMunicipalityMerger(

try
{
var errorMessages = new List<string>();
var records = new List<CsvRecord>();
using (var stream = new MemoryStream())
{
Expand All @@ -84,51 +85,62 @@ public async Task<IActionResult> ProposeForMunicipalityMerger(
{
recordNr++;

var recordErrorMessages = new List<string>();

var oldNisCode = csv.GetField<string>("OUD NIS code");
var oldStreetNamePersistentLocalIdAsString = csv.GetField<string>("OUD straatnaamid");
var newNisCode = csv.GetField<string>("NIEUW NIS code");
var streetName = csv.GetField<string>("NIEUW straatnaam");
var homonymAddition = csv.GetField<string>("NIEUW homoniemtoevoeging");

if (string.IsNullOrWhiteSpace(oldNisCode))
return BadRequest($"OldNisCode is required at record number {recordNr}");
{
recordErrorMessages.Add($"OldNisCode is required at record number {recordNr}");
}

if (string.IsNullOrWhiteSpace(oldStreetNamePersistentLocalIdAsString))
return BadRequest($"OldStreetNamePersistentLocalId is required at record number {recordNr}");
{
recordErrorMessages.Add($"OldStreetNamePersistentLocalId is required at record number {recordNr}");
}

if (!int.TryParse(oldStreetNamePersistentLocalIdAsString, out var oldStreetNamePersistentLocalId))
return BadRequest($"OldStreetNamePersistentLocalId is NaN at record number {recordNr}");
if (!string.IsNullOrWhiteSpace(oldStreetNamePersistentLocalIdAsString)
& !int.TryParse(oldStreetNamePersistentLocalIdAsString, out var oldStreetNamePersistentLocalId))
{
recordErrorMessages.Add($"OldStreetNamePersistentLocalId is NaN at record number {recordNr}");
}

if (string.IsNullOrWhiteSpace(newNisCode))
return BadRequest($"NisCode is required at record number {recordNr}");

if (newNisCode.Trim() != nisCode)
return BadRequest(
$"NisCode {newNisCode} does not match the provided NisCode {nisCode} at record number {recordNr}");
{
recordErrorMessages.Add($"NisCode is required at record number {recordNr}");
}
else if (newNisCode.Trim() != nisCode)
{
recordErrorMessages.Add($"NisCode {newNisCode} does not match the provided NisCode {nisCode} at record number {recordNr}");
}

if (string.IsNullOrWhiteSpace(streetName))
return BadRequest($"StreetName is required at record number {recordNr}");
{
recordErrorMessages.Add($"StreetName is required at record number {recordNr}");
}

if (recordErrorMessages.Any())
{
errorMessages.AddRange(recordErrorMessages);
continue;
}

records.Add(new CsvRecord
{
OldNisCode = oldNisCode.Trim(),
OldNisCode = oldNisCode!.Trim(),
OldStreetNamePersistentLocalId = oldStreetNamePersistentLocalId,
NisCode = nisCode.Trim(),
StreetName = streetName.Trim(),
StreetName = streetName!.Trim(),
HomonymAddition = string.IsNullOrWhiteSpace(homonymAddition) ? null : homonymAddition.Trim()
});
}
}
}

var streetNamesByNisCodeRecords = records
.GroupBy(x => x.NisCode)
.ToDictionary(
x => x.Key,
y => y.ToList())
.Single()
.Value;

var oldMunicipalities = new List<MunicipalityConsumerItem>();
foreach (var oldMunicipalityNisCode in records.Select(x => x.OldNisCode).Distinct())
{
Expand All @@ -137,12 +149,26 @@ public async Task<IActionResult> ProposeForMunicipalityMerger(

if (oldMunicipality is null)
{
return BadRequest($"No municipality found for {oldMunicipalityNisCode}");
errorMessages.Add($"No municipality found for NisCode '{oldMunicipalityNisCode}'");
continue;
}

oldMunicipalities.Add(oldMunicipality);
}

if (errorMessages.Any())
{
return BadRequest(errorMessages);
}

var streetNamesByNisCodeRecords = records
.GroupBy(x => x.NisCode)
.ToDictionary(
x => x.Key,
y => y.ToList())
.Single()
.Value;

// group by streetname and homonym addition
var streetNamesByNisCode = streetNamesByNisCodeRecords
.GroupBy(x => (x.StreetName, x.HomonymAddition))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void WithNoNisCode_ThenReturnsBadRequest()
CancellationToken.None).GetAwaiter().GetResult();

result.Should().BeOfType<BadRequestObjectResult>();
((BadRequestObjectResult)result).Value.Should().BeEquivalentTo("NisCode is required at record number 1");
((BadRequestObjectResult)result).Value.Should().BeEquivalentTo(new[] { "NisCode is required at record number 1" });
}

[Fact]
Expand All @@ -86,7 +86,7 @@ public void WithDifferentNisCodeThanRoute_ThenReturnsBadRequest()
CancellationToken.None).GetAwaiter().GetResult();

result.Should().BeOfType<BadRequestObjectResult>();
((BadRequestObjectResult)result).Value.Should().BeEquivalentTo($"NisCode 11001 does not match the provided NisCode {nisCode} at record number 1");
((BadRequestObjectResult)result).Value.Should().BeEquivalentTo(new[] { $"NisCode 11001 does not match the provided NisCode {nisCode} at record number 1" });
}

[Fact]
Expand All @@ -102,7 +102,7 @@ public void WithNoStreetName_ThenReturnsBadRequest()
CancellationToken.None).GetAwaiter().GetResult();

result.Should().BeOfType<BadRequestObjectResult>();
((BadRequestObjectResult)result).Value.Should().BeEquivalentTo("StreetName is required at record number 1");
((BadRequestObjectResult)result).Value.Should().BeEquivalentTo(new[] { "StreetName is required at record number 1" });
}

[Fact]
Expand Down
Loading