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

Add tvd and custom ids #1769

Merged
merged 14 commits into from
Dec 19, 2024
12 changes: 8 additions & 4 deletions src/api/Controllers/ExportController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ public async Task<IActionResult> ExportCsvAsync([FromQuery][MaxLength(MaxPageSiz
if (idList.Count < 1) return BadRequest("The list of IDs must not be empty.");

var boreholes = await context.Boreholes
.Include(b => b.BoreholeCodelists)
.ThenInclude(bc => bc.Codelist)
.Include(b => b.BoreholeCodelists).ThenInclude(bc => bc.Codelist)
.Where(borehole => idList.Contains(borehole.Id))
.OrderBy(b => idList.IndexOf(b.Id))
.ToListAsync()
Expand Down Expand Up @@ -85,7 +84,7 @@ public async Task<IActionResult> ExportCsvAsync([FromQuery][MaxLength(MaxPageSiz

// Write dynamic headers for each distinct custom Id
var customIdHeaders = boreholes
.SelectMany(b => b.BoreholeCodelists ?? Enumerable.Empty<BoreholeCodelist>())
.SelectMany(b => GetBoreholeCodelists(b))
MiraGeowerkstatt marked this conversation as resolved.
Show resolved Hide resolved
.Select(bc => new { bc.CodelistId, bc.Codelist?.En })
.Distinct()
.OrderBy(x => x.CodelistId)
Expand Down Expand Up @@ -149,7 +148,7 @@ public async Task<IActionResult> ExportCsvAsync([FromQuery][MaxLength(MaxPageSiz
// Write dynamic fields for custom Ids
foreach (var header in customIdHeaders)
{
var codelistValue = (b.BoreholeCodelists ?? Enumerable.Empty<BoreholeCodelist>()).FirstOrDefault(bc => bc.CodelistId == header.CodelistId)?.Value;
var codelistValue = GetBoreholeCodelists(b).FirstOrDefault(bc => bc.CodelistId == header.CodelistId)?.Value;
csvWriter.WriteField(codelistValue ?? "");
}

Expand All @@ -160,4 +159,9 @@ public async Task<IActionResult> ExportCsvAsync([FromQuery][MaxLength(MaxPageSiz
await csvWriter.FlushAsync().ConfigureAwait(false);
return File(Encoding.UTF8.GetBytes(stringWriter.ToString()), "text/csv", "boreholes_export.csv");
}

private IEnumerable<BoreholeCodelist> GetBoreholeCodelists(Borehole borehole)
{
return borehole.BoreholeCodelists ?? Enumerable.Empty<BoreholeCodelist>();
}
}
Loading