From dc3bfdc4bdfe95ba24f0b42208e79d102fd457ac Mon Sep 17 00:00:00 2001 From: danjov Date: Wed, 10 Jan 2024 11:39:27 +0100 Subject: [PATCH] Add GetById functionality for stratigraphies in .NET API --- src/api/Controllers/StratigraphyController.cs | 30 +++++++++++++++---- .../Controllers/StratigraphyControllerTest.cs | 18 +++++++++++ 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/src/api/Controllers/StratigraphyController.cs b/src/api/Controllers/StratigraphyController.cs index 8a6830fc6..c97f72c50 100644 --- a/src/api/Controllers/StratigraphyController.cs +++ b/src/api/Controllers/StratigraphyController.cs @@ -21,6 +21,26 @@ public StratigraphyController(BdmsContext context, ILogger logger, this.boreholeLockService = boreholeLockService; } + /// + /// Asynchronously gets the with the specified . + /// + /// The id of the to get. + [HttpGet("{id}")] + [Authorize(Policy = PolicyNames.Viewer)] + public async Task> GetByIdAsync([Required] int id) + { + var stratigraphy = await Context.Stratigraphies + .SingleOrDefaultAsync(x => x.Id == id) + .ConfigureAwait(false); + + if (stratigraphy == null) + { + return NotFound(); + } + + return Ok(stratigraphy); + } + /// /// Asynchronously gets the s, optionally filtered by and . /// @@ -261,11 +281,11 @@ public override async Task> EditAsync(Stratigraphy en } catch (UnauthorizedAccessException) { - return Unauthorized("You are not authorized to add a bedrock layer to this stratigraphy."); + return Unauthorized("You are not authorized to edit to this stratigraphy."); } catch (Exception ex) { - var message = "An error ocurred while adding a bedrock layer to the stratigraphy."; + var message = "An error ocurred while editing the stratigraphy."; Logger.LogError(ex, message); return Problem(message); } @@ -282,10 +302,10 @@ public override async Task> EditAsync(Stratigraphy en .ToListAsync() .ConfigureAwait(false); - foreach (var stratigraphy in otherPrimaryStratigraphies) + foreach (var other in otherPrimaryStratigraphies) { - stratigraphy.IsPrimary = false; - Context.Update(stratigraphy); + other.IsPrimary = false; + Context.Update(other); } await Context.UpdateChangeInformationAndSaveChangesAsync(HttpContext).ConfigureAwait(false); diff --git a/tests/Controllers/StratigraphyControllerTest.cs b/tests/Controllers/StratigraphyControllerTest.cs index e7131b813..caa70bdf0 100644 --- a/tests/Controllers/StratigraphyControllerTest.cs +++ b/tests/Controllers/StratigraphyControllerTest.cs @@ -79,6 +79,24 @@ public async Task GetCasingsByBoreholeId() Assert.AreEqual(stratigraphy.UpdatedById, 2); } + [TestMethod] + public async Task GetById() + { + var stratigraphyResult = await controller.GetByIdAsync(StratigraphyId); + + var stratigraphy = ActionResultAssert.IsOkObjectResult(stratigraphyResult.Result); + Assert.AreEqual(stratigraphy.BoreholeId, 1008078); + Assert.AreEqual(stratigraphy.Name, "Earnest Little"); + Assert.AreEqual(stratigraphy.Notes, "My co-worker Tyron has one of these. He says it looks stout."); + } + + [TestMethod] + public async Task GetByUnknownId() + { + var stratigraphyResult = await controller.GetByIdAsync(int.MinValue); + ActionResultAssert.IsNotFound(stratigraphyResult.Result); + } + [TestMethod] public async Task Copy() {