Skip to content

Commit

Permalink
Merge branch 'main' into adapt-csv-import
Browse files Browse the repository at this point in the history
  • Loading branch information
MiraGeowerkstatt authored Dec 19, 2024
2 parents f4ac170 + 9b2b2e0 commit 1630079
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 11 deletions.
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))
.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 static IEnumerable<BoreholeCodelist> GetBoreholeCodelists(Borehole borehole)
{
return borehole.BoreholeCodelists ?? Enumerable.Empty<BoreholeCodelist>();
}
}
40 changes: 40 additions & 0 deletions src/client/cypress/e2e/detailPage/coordinates.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,44 @@ describe("Tests for editing coordinates of a borehole.", () => {
checkDecimalPlaces("@LV03X-input", 5);
checkDecimalPlaces("@LV03Y-input", 5);
});

it("updates canton and municipality when changing coordinates", () => {
// Type coordinates for Samaden in LV95
cy.get("@LV95X-input").type("2789000");
cy.get("@LV95Y-input").type("1155000");
cy.wait("@location");
cy.wait(4000);

cy.get("@country").should("have.value", "Schweiz");
cy.get("@canton").should("have.value", "Graubünden");
cy.get("@municipality").should("have.value", "Samaden");

// Type coordinates for Unterentfelden in LV95
cy.get("@LV95X-input").clear().type("2646000");
cy.get("@LV95Y-input").clear().type("1247000");

cy.get("@country").should("have.value", "Schweiz");
cy.get("@canton").should("have.value", "Aargau");
cy.get("@municipality").should("have.value", "Unterentfelden");

// switch reference system to LV03
setSelect("originalReferenceSystem", 1);
handlePrompt("Changing the coordinate system will reset the coordinates. Do you want to continue?", "Confirm");

// Type coordinates for Samaden in LV03
cy.get("@LV03X-input").clear().type("789000");
cy.get("@LV03Y-input").clear().type("155000");

cy.get("@country").should("have.value", "Schweiz");
cy.get("@canton").should("have.value", "Graubünden");
cy.get("@municipality").should("have.value", "Samaden");

// Type coordinates for Unterentfelden in LV03
cy.get("@LV03X-input").clear().type("646000");
cy.get("@LV03Y-input").clear().type("247000");

cy.get("@country").should("have.value", "Schweiz");
cy.get("@canton").should("have.value", "Aargau");
cy.get("@municipality").should("have.value", "Unterentfelden");
});
});
22 changes: 15 additions & 7 deletions src/client/src/pages/detail/form/location/locationSegment.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback } from "react";
import { useCallback, useState } from "react";
import { UseFormReturn } from "react-hook-form";
import { Card, Grid, Stack } from "@mui/material";
import { fetchApiV2 } from "../../../../api/fetchApiV2";
Expand All @@ -17,6 +17,8 @@ interface LocationSegmentProps extends LocationBaseProps {
}

const LocationSegment = ({ borehole, editingEnabled, labelingPanelOpen, formMethods }: LocationSegmentProps) => {
const [currentLV95X, setCurrentLV95X] = useState(borehole.locationX ? Number(borehole.locationX) : null);
const [currentLV95Y, setCurrentLV95Y] = useState(borehole.locationY ? Number(borehole.locationY) : null);
const transformCoordinates = useCallback(async (referenceSystem: string, x: number, y: number) => {
let apiUrl;
if (referenceSystem === referenceSystems.LV95.name) {
Expand Down Expand Up @@ -70,12 +72,18 @@ const LocationSegment = ({ borehole, editingEnabled, labelingPanelOpen, formMeth
if (!response) return; // Ensure response is valid

const maxPrecision = Math.max(XPrecision, YPrecision);
const transformedX = parseFloat(response.easting).toFixed(maxPrecision);
const transformedY = parseFloat(response.northing).toFixed(maxPrecision);
const transformedX = parseFloat(response.easting);
const transformedY = parseFloat(response.northing);

const location = await fetchApiV2(`location/identify?east=${X}&north=${Y}`, "GET");
const XLV95 = sourceSystem === ReferenceSystemKey.LV95 ? X : transformedX;
const YLV95 = sourceSystem === ReferenceSystemKey.LV95 ? Y : transformedY;

setCurrentLV95X(XLV95);
setCurrentLV95Y(YLV95);

const location = await fetchApiV2(`location/identify?east=${XLV95}&north=${YLV95}`, "GET");
setValuesForCountryCantonMunicipality(location);
setValuesForReferenceSystem(targetSystem, transformedX, transformedY);
setValuesForReferenceSystem(targetSystem, transformedX.toFixed(maxPrecision), transformedY.toFixed(maxPrecision));
},
[setValuesForCountryCantonMunicipality, setValuesForReferenceSystem, transformCoordinates],
);
Expand Down Expand Up @@ -130,8 +138,8 @@ const LocationSegment = ({ borehole, editingEnabled, labelingPanelOpen, formMeth
}}
id={borehole.id}
isEditable={editingEnabled}
x={borehole.locationX ? Number(borehole.locationX) : null}
y={borehole.locationY ? Number(borehole.locationY) : null}
x={currentLV95X}
y={currentLV95Y}
/>
</FormSegmentBox>
</Grid>
Expand Down

0 comments on commit 1630079

Please sign in to comment.