Skip to content

Commit

Permalink
Use .NET API for adding bedrock layer to a stratigraphy (#927)
Browse files Browse the repository at this point in the history
  • Loading branch information
danjov authored Dec 20, 2023
2 parents c72b639 + 34441bb commit 34c37b5
Show file tree
Hide file tree
Showing 13 changed files with 158 additions and 148 deletions.
3 changes: 0 additions & 3 deletions src/api-legacy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@
from bms.v1.borehole.identifier import IdentifierAdminHandler
from bms.v1.borehole.identifier import IdentifierViewerHandler

# Stratigraphy's ACTION Handlers
from bms.v1.borehole.stratigraphy.producer import StratigraphyProducerHandler

# Profiles's ACTION Handlers
from bms.v1.borehole.profile.viewer import ProfileViewerHandler
from bms.v1.borehole.profile.producer import ProfileProducerHandler
Expand Down
6 changes: 0 additions & 6 deletions src/api-legacy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,6 @@ async def close(application):
IdentifierProducerHandler,
IdentifierViewerHandler,

# Stratigraphy handlers
StratigraphyProducerHandler,

# Layer handlers
LayerViewerHandler,
LayerProducerHandler,
Expand Down Expand Up @@ -218,9 +215,6 @@ async def close(application):
# FEEDBACK handlers
(r'/api/v1/feedback', FeedbackHandler),

# Stratigraphy handlers (will be deprecated)
(r'/api/v1/borehole/stratigraphy/edit', StratigraphyProducerHandler),

# Layer handlers (will be deprecated)
(r'/api/v1/borehole/stratigraphy/layer', LayerViewerHandler),
(r'/api/v1/borehole/stratigraphy/layer/edit', LayerProducerHandler),
Expand Down
9 changes: 1 addition & 8 deletions src/api-legacy/v1/borehole/profile/producer.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
# -*- coding: utf-8 -*-
from bms.v1.borehole.profile.patch import PatchProfile
from bms.v1.handlers import Producer
from bms.v1.borehole.stratigraphy import AddBedrock


class ProfileProducerHandler(Producer):
async def execute(self, request):
action = request.pop('action', None)

if action in [
'ADDBEDROCK',
'PATCH'
]:

Expand All @@ -20,7 +18,6 @@ async def execute(self, request):
id_bho = None

if action in [
'ADDBEDROCK',
'PATCH',
]:
# Get Borehole id
Expand All @@ -38,11 +35,7 @@ async def execute(self, request):
id_bho, self.user, conn
)

if action == 'ADDBEDROCK':
exe = AddBedrock(conn)
request['user_id'] = self.user['id']

elif action == 'PATCH':
if action == 'PATCH':
exe = PatchProfile(conn)
request['user_id'] = self.user['id']

Expand Down
5 changes: 0 additions & 5 deletions src/api-legacy/v1/borehole/stratigraphy/__init__.py

This file was deleted.

52 changes: 0 additions & 52 deletions src/api-legacy/v1/borehole/stratigraphy/addbedrock.py

This file was deleted.

49 changes: 0 additions & 49 deletions src/api-legacy/v1/borehole/stratigraphy/producer.py

This file was deleted.

58 changes: 58 additions & 0 deletions src/api/Controllers/StratigraphyController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,62 @@ public override async Task<ActionResult<Stratigraphy>> CreateAsync(Stratigraphy

return await base.CreateAsync(entity).ConfigureAwait(false);
}

/// <summary>
/// Asynchronously adds a bedrock <see cref="Layer"/> to a <see cref="Stratigraphy"/>.
/// </summary>
/// <param name="id">The <see cref="Stratigraphy"/> id.</param>
/// <returns>The id of the created bedrock <see cref="Layer"/>.</returns>
[HttpPost("addbedrock")]
[Authorize(Policy = PolicyNames.Viewer)]
public async Task<ActionResult<int>> AddBedrockLayerAsync([Required] int id)
{
var stratigraphy = await Context.Stratigraphies.FindAsync(id).ConfigureAwait(false);
if (stratigraphy == null)
{
return NotFound();
}

try
{
// Check if associated borehole is locked
var userName = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;
if (await boreholeLockService.IsBoreholeLockedAsync(stratigraphy.BoreholeId, userName).ConfigureAwait(false))
{
return Problem("The borehole is locked by another user.");
}
}
catch (UnauthorizedAccessException)
{
return Unauthorized("You are not authorized to add a bedrock layer to this stratigraphy.");
}
catch (Exception ex)
{
var message = "An error ocurred while adding a bedrock layer to the stratigraphy.";
Logger.LogError(ex, message);
return Problem(message);
}

// Check if associated borehole has a TopBedrock value
var borehole = await Context.Boreholes.FindAsync(stratigraphy.BoreholeId).ConfigureAwait(false);
if (!borehole.TopBedrock.HasValue)
{
return Problem("Bedrock not yet defined.");
}

// Add bedrock layer
var bedrockLayer = new Layer
{
StratigraphyId = stratigraphy.Id,
FromDepth = borehole.TopBedrock.Value,
LithologyTopBedrockId = borehole.LithologyTopBedrockId,
LithostratigraphyId = borehole.LithostratigraphyId,
IsLast = false,
};

await Context.Layers.AddAsync(bedrockLayer).ConfigureAwait(false);
await Context.UpdateChangeInformationAndSaveChangesAsync(HttpContext).ConfigureAwait(false);

return Ok(bedrockLayer.Id);
}
}
8 changes: 0 additions & 8 deletions src/client/src/api-lib/actions/stratigraphy.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
import { fetch } from "./index";

// Create a new stratigraphy for the given borehole id
export function addBedrock(id) {
return fetch("/borehole/stratigraphy/edit", {
action: "ADDBEDROCK",
id: id,
});
}

export function getLayers(id) {
return fetch("/borehole/stratigraphy/layer", {
action: "LIST",
Expand Down
2 changes: 0 additions & 2 deletions src/client/src/api-lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ import {
} from "./actions/workflow";

import {
addBedrock,
createLayer,
createInstrument,
deleteLayer,
Expand Down Expand Up @@ -162,7 +161,6 @@ export {
submitWorkflow,
rejectWorkflow,
resetWorkflow,
addBedrock,
createLayer,
createInstrument,
deleteLayer,
Expand Down
4 changes: 4 additions & 0 deletions src/client/src/api/fetchApiV2.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ export const createStratigraphy = async (boreholeId, kindId) => {
});
};

export const addBedrock = async id => {
return await fetchApiV2(`stratigraphy/addbedrock?id=${id}`, "POST");
};

// Enable using react-query outputs across the application.

// eslint-disable-next-line react-hooks/rules-of-hooks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import React, { useState, useEffect, useContext } from "react";
import * as Styled from "./styles";
import { Icon, Radio } from "semantic-ui-react";
import TranslationText from "../../../../../translationText";
import { gapLayer, deleteLayer } from "../../../../../../../api-lib/index";
import {
gapLayer,
fetchLayerById,
addBedrock,
deleteLayer,
} from "../../../../../../../api-lib/index";
import { fetchLayerById } from "../../../../../../../api/fetchApiV2";
} from "../../../../../../../api/fetchApiV2";
import ErrorTypes from "./errorTypes";
import { AlertContext } from "../../../../../../alert/alertContext";

Expand Down Expand Up @@ -109,10 +108,8 @@ const ProfileLayersError = props => {
} else if (title === "missingBedrock") {
addBedrock(id)
.then(response => {
if (response.data.success) {
if (response) {
onUpdated("fixErrors");
} else {
alertContext.error(response.data.message);
}
})
.catch(error => {
Expand Down
15 changes: 15 additions & 0 deletions tests/ActionResultAssert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ internal static void IsNotFound(IActionResult? actionResult)
internal static void IsInternalServerError(IActionResult? actionResult)
=> AssertActionResult(actionResult, StatusCodes.Status500InternalServerError);

/// <summary>
/// Asserts that the <see cref="IActionResult"/> is InternalServerError (500).
/// </summary>
internal static void IsInternalServerError(IActionResult? actionResult, string expectedErrorMessageSubstring)
{
AssertActionResult(actionResult, StatusCodes.Status500InternalServerError);

var problemDetails = (ProblemDetails)((ObjectResult)actionResult!).Value!;
StringAssert.Contains(
problemDetails.Detail,
expectedErrorMessageSubstring,
$"The error message does not contain the expected message '{expectedErrorMessageSubstring}'.",
StringComparison.OrdinalIgnoreCase);
}

private static void AssertActionResult(IActionResult? currentActionResult, int expectedStatusCode)
{
var statusCodeResult = currentActionResult as IStatusCodeActionResult;
Expand Down
Loading

0 comments on commit 34c37b5

Please sign in to comment.