Skip to content

Commit

Permalink
Patch layer with new api (#928)
Browse files Browse the repository at this point in the history
  • Loading branch information
tschumpr authored Jan 8, 2024
2 parents b7a408a + d7024c4 commit 3fbed38
Show file tree
Hide file tree
Showing 18 changed files with 248 additions and 243 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

### Changed

- Updated layer management to use the .NET API.

## v2.0.506 - 2023-12-21

### Added
Expand Down
54 changes: 52 additions & 2 deletions src/api/Controllers/LayerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,58 @@ public async Task<ActionResult<Layer>> GetByIdAsync(int id)

/// <inheritdoc />
[Authorize(Policy = PolicyNames.Viewer)]
public override Task<ActionResult<Layer>> EditAsync(Layer entity)
=> base.EditAsync(entity);
public async override Task<ActionResult<Layer>> EditAsync(Layer entity)
{
if (entity == null)
{
return BadRequest(ModelState);
}

var existingLayer = Context.Layers.Include(l => l.LayerCodelists).Include(c => c.Codelists).SingleOrDefault(l => l.Id == entity.Id);
var codelistIds = entity.CodelistIds?.ToList() ?? new List<int>();
if (existingLayer != default)
{
Context.Entry(existingLayer).CurrentValues.SetValues(entity);
}
else
{
return NotFound();
}

foreach (var layerCodelist in existingLayer.LayerCodelists)
{
if (!codelistIds.Contains(layerCodelist.CodelistId))
{
Context.Remove(layerCodelist);
}
}

foreach (var id in codelistIds)
{
if (!existingLayer.LayerCodelists.Any(lc => lc.CodelistId == id))
{
var codelist = await Context.Codelists.FindAsync(id).ConfigureAwait(false);
if (codelist != null)
{
existingLayer.LayerCodelists ??= new List<LayerCodelist>();

existingLayer.LayerCodelists.Add(new LayerCodelist { Codelist = codelist, CodelistId = codelist.Id, SchemaName = codelist.Schema! });
}
}
}

try
{
await Context.UpdateChangeInformationAndSaveChangesAsync(HttpContext).ConfigureAwait(false);
return await GetByIdAsync(entity.Id).ConfigureAwait(false);
}
catch (Exception ex)
{
var errorMessage = "An error occurred while saving the entity changes.";
Logger?.LogError(ex, errorMessage);
return Problem(errorMessage);
}
}

/// <inheritdoc />
[Authorize(Policy = PolicyNames.Viewer)]
Expand Down
2 changes: 1 addition & 1 deletion src/client/cypress/e2e/editor/chronostratigraphy.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe("Tests for the chronostratigraphy editor.", () => {
cy.get("@borehole_id").then(id =>
login(`editor/${id}/stratigraphy/chronostratigraphy`),
);
cy.wait("@layer-by-profileId");
cy.wait("@get-layers-by-profileId");

// start editing session
cy.contains("a", "Start editing").click();
Expand Down
5 changes: 3 additions & 2 deletions src/client/cypress/e2e/editor/layerform.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe("Tests for the layer form.", () => {
cy.wait("@layer");

cy.get('[data-cy="styled-layer-0"] [data-testid="ModeEditIcon"]').click();
cy.wait("@get-layer-by-id");

// fill all dropdowns with two values
cy.get('[aria-multiselectable="true"]')
Expand All @@ -26,7 +27,7 @@ describe("Tests for the layer form.", () => {
.last()
.scrollIntoView()
.click();
cy.wait("@stratigraphy_layer_edit_patch");
cy.wait("@update-layer");
});

cy.get('[aria-multiselectable="true"]')
Expand All @@ -38,7 +39,7 @@ describe("Tests for the layer form.", () => {
.find('[role="option"]')
.eq(1)
.click();
cy.wait("@stratigraphy_layer_edit_patch");
cy.wait("@update-layer");
});

const multipleDropdownValues = [];
Expand Down
22 changes: 13 additions & 9 deletions src/client/cypress/e2e/editor/lithologicalDescription.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,37 @@ describe("Tests for the lithological description column.", () => {
cy.get('[data-cy="add-layer-icon"]').click();
cy.wait("@layer");
cy.get('[data-cy="styled-layer-0"] [data-testid="ModeEditIcon"]').click();
cy.wait("@layer");
cy.wait("@get-layer-by-id");
cy.contains("Show all fields").children(".checkbox").click();
cy.get('[data-cy="depth_to"]').click().clear().type(50);
cy.wait("@stratigraphy_layer_edit_patch");
cy.get('[data-cy="toDepth"]').click().clear().type(50);
cy.wait("@update-layer");
cy.wait("@layer");
cy.get('[data-cy="styled-layer-0"] [data-testid="ClearIcon"]').click();

cy.get('[data-cy="add-layer-icon"]').click();
cy.wait("@layer");
cy.get('[data-cy="styled-layer-1"] [data-testid="ModeEditIcon"]').click();
cy.wait("@layer");
cy.wait("@get-layer-by-id");
cy.contains("Show all fields").children(".checkbox").click();
cy.get('[data-cy="depth_to"]').click().clear().type(62.5);
cy.wait("@stratigraphy_layer_edit_patch");
cy.get('[data-cy="toDepth"]').click().clear().type(62.5);
cy.wait("@update-layer");
cy.wait("@layer");
cy.get('[data-cy="styled-layer-1"] [data-testid="ClearIcon"]').click();

cy.get('[data-cy="add-layer-icon"]').click();
cy.wait("@layer");
cy.get('[data-cy="styled-layer-2"] [data-testid="ModeEditIcon"]').click();
cy.wait("@layer");
cy.wait("@get-layer-by-id");
cy.contains("Show all fields").children(".checkbox").click();
cy.get('[data-cy="depth_to"]').click().clear().type(120);
cy.wait("@stratigraphy_layer_edit_patch");
cy.get('[data-cy="toDepth"]').click().clear().type(120);
cy.wait("@update-layer");
cy.wait("@layer");
cy.get('[data-cy="styled-layer-2"] [data-testid="ClearIcon"]').click();

// workaround because close button of profile attributes is sometimes not clickable
cy.get('[data-cy="borehole-menu-item"]').click();
cy.get('[data-cy="lithology-menu-item"]').click();

// add lithological description
cy.wait("@layer");
cy.get('[data-cy="add-litho-desc-icon"]').click();
Expand Down
2 changes: 1 addition & 1 deletion src/client/cypress/e2e/editor/lithostratigraphy.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe("Tests for the lithostratigraphy editor.", () => {
cy.get("@borehole_id").then(id =>
login(`editor/${id}/stratigraphy/lithostratigraphy`),
);
cy.wait("@layer-by-profileId");
cy.wait("@get-layers-by-profileId");

// start editing session
cy.contains("a", "Start editing").click();
Expand Down
7 changes: 3 additions & 4 deletions src/client/cypress/e2e/testHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@ export const interceptApiCalls = () => {
cy.intercept("/api/v1/workflow/edit", req => {
return (req.alias = `workflow_edit_${req.body.action.toLowerCase()}`);
});
cy.intercept("/api/v1/borehole/stratigraphy/layer/edit", req => {
return (req.alias = `stratigraphy_layer_edit_${req.body.action.toLowerCase()}`);
});
cy.intercept("/api/v1/setting").as("setting");
cy.intercept("api/v1/borehole/codes").as("codes");

// Api V2
cy.intercept("/api/v2/layer?profileId=**").as("layer-by-profileId");
cy.intercept("/api/v2/layer?profileId=**").as("get-layers-by-profileId");
cy.intercept("GET", "/api/v2/layer/**").as("get-layer-by-id");
cy.intercept("PUT", "/api/v2/layer").as("update-layer");
cy.intercept("/api/v2/location/identify**").as("location");
cy.intercept("/api/v2/borehole/copy*").as("borehole_copy");
cy.intercept("/api/v2/lithologicaldescription*").as(
Expand Down
10 changes: 5 additions & 5 deletions src/client/cypress/e2e/viewer/displayLayer.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ describe("Test for the borehole form.", () => {
.find('[role="option"]')
.eq(1)
.click();
cy.wait("@stratigraphy_layer_edit_patch");
cy.wait("@update-layer");
});

// fill text fields
cy.get('[data-cy="depth_from"]').click().clear().type(0);
cy.get('[data-cy="depth_to"]').click().clear().type(50);
cy.get('[data-cy="uscs_original"]')
cy.get('[data-cy="fromDepth"]').click().clear().type(0);
cy.get('[data-cy="toDepth"]').click().clear().type(50);
cy.get('[data-cy="originalUscs"]')
.find("input")
.click()
.clear()
Expand All @@ -80,7 +80,7 @@ describe("Test for the borehole form.", () => {
);
})
.type("S");
cy.get('[data-cy="original_lithology"]')
cy.get('[data-cy="originalLithology"]')
.find("input")
.click()
.clear()
Expand Down
8 changes: 0 additions & 8 deletions src/client/src/api-lib/actions/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,6 @@ export function getProfileLayers(id, withValidation = false) {
});
}

// Fetch the attributes of a single layer by its id
export function getLayerAttributes(id) {
return fetch("/borehole/profile/layer", {
action: "GET",
id: id,
});
}

// Update the attributes of a single layer by its id, field name and value
export function patchProfile(id, field, value) {
return fetch("/borehole/profile/edit", {
Expand Down
13 changes: 0 additions & 13 deletions src/client/src/api-lib/actions/stratigraphy.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,3 @@ export function gapLayer(id, process = 0, value = null) {
value: value,
});
}

export function patchLayer(id, field, value) {
if (field === "layer_lithology_top_bedrock") {
// Fixes discrepancy between field name in client and API.
field = "lithology_top_bedrock";
}
return fetch("/borehole/stratigraphy/layer/edit", {
action: "PATCH",
id: id,
field: field,
value: value,
});
}
4 changes: 0 additions & 4 deletions src/client/src/api-lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,13 @@ import {
deleteLayer,
gapLayer,
getLayers,
patchLayer,
} from "./actions/stratigraphy";

import {
getProfile,
getProfiles,
getProfileLayers,
patchProfile,
getLayerAttributes,
} from "./actions/profile";

import { loadDomains, patchCodeConfig } from "./actions/domains";
Expand Down Expand Up @@ -166,12 +164,10 @@ export {
deleteLayer,
gapLayer,
getLayers,
patchLayer,
getProfile,
getProfiles,
getProfileLayers,
patchProfile,
getLayerAttributes,
loadDomains,
patchCodeConfig,
getWms,
Expand Down

This file was deleted.

Loading

0 comments on commit 3fbed38

Please sign in to comment.