Skip to content

Commit

Permalink
Add GetById functionality for stratigraphies in .NET API
Browse files Browse the repository at this point in the history
  • Loading branch information
danjov committed Jan 10, 2024
1 parent 9acb872 commit dc3bfdc
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
30 changes: 25 additions & 5 deletions src/api/Controllers/StratigraphyController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@ public StratigraphyController(BdmsContext context, ILogger<Stratigraphy> logger,
this.boreholeLockService = boreholeLockService;
}

/// <summary>
/// Asynchronously gets the <see cref="Stratigraphy"/> with the specified <paramref name="id"/>.
/// </summary>
/// <param name="id">The id of the <see cref="Stratigraphy"/> to get.</param>
[HttpGet("{id}")]
[Authorize(Policy = PolicyNames.Viewer)]
public async Task<ActionResult<Stratigraphy>> GetByIdAsync([Required] int id)
{
var stratigraphy = await Context.Stratigraphies
.SingleOrDefaultAsync(x => x.Id == id)
.ConfigureAwait(false);

if (stratigraphy == null)
{
return NotFound();
}

return Ok(stratigraphy);
}

/// <summary>
/// Asynchronously gets the <see cref="Stratigraphy"/>s, optionally filtered by <paramref name="boreholeId"/> and <paramref name="kindId"/>.
/// </summary>
Expand Down Expand Up @@ -261,11 +281,11 @@ public override async Task<ActionResult<Stratigraphy>> 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);
}
Expand All @@ -282,10 +302,10 @@ public override async Task<ActionResult<Stratigraphy>> 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);
Expand Down
18 changes: 18 additions & 0 deletions tests/Controllers/StratigraphyControllerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Stratigraphy>(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()
{
Expand Down

0 comments on commit dc3bfdc

Please sign in to comment.