Skip to content

Commit

Permalink
Add borehole put (#1573)
Browse files Browse the repository at this point in the history
  • Loading branch information
MiraGeowerkstatt authored Oct 8, 2024
2 parents 4a0161a + b807c4a commit 2059040
Show file tree
Hide file tree
Showing 33 changed files with 290 additions and 63 deletions.
2 changes: 1 addition & 1 deletion src/api/Controllers/BackfillController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace BDMS.Controllers;
[Route("api/v{version:apiVersion}/[controller]")]
public class BackfillController : BoreholeControllerBase<Backfill>
{
public BackfillController(BdmsContext context, ILogger<Backfill> logger, IBoreholeLockService boreholeLockService)
public BackfillController(BdmsContext context, ILogger<BackfillController> logger, IBoreholeLockService boreholeLockService)
: base(context, logger, boreholeLockService)
{
}
Expand Down
117 changes: 90 additions & 27 deletions src/api/Controllers/BoreholeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,74 @@

namespace BDMS.Controllers;

// The api version is temporarily hardcoded as "v2" until the legacy API for borehole is removed.
// This is necessary to avoid a rerouting issue with the reverse proxy, when matching routes exist for both the .net and the python API.
[ApiController]
[Route("api/v{version:apiVersion}/[controller]")]
public class BoreholeController : ControllerBase
[Route("api/v2/[controller]")]
public class BoreholeController : BoreholeControllerBase<Borehole>
{
private readonly BdmsContext context;
private readonly ILogger logger;
public BoreholeController(BdmsContext context, ILogger<BoreholeController> logger, IBoreholeLockService boreholeLockService)
: base(context, logger, boreholeLockService)
{
}

public BoreholeController(BdmsContext context, ILogger<BoreholeController> logger)
/// <inheritdoc />
[Authorize(Policy = PolicyNames.Viewer)]
public async override Task<ActionResult<Borehole>> EditAsync(Borehole entity)
{
this.context = context;
this.logger = logger;
if (entity == null)
{
return BadRequest(ModelState);
}

var existingBorehole = await Context.Boreholes
.SingleOrDefaultAsync(l => l.Id == entity.Id)
.ConfigureAwait(false);

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

Context.Entry(existingBorehole).CurrentValues.SetValues(entity);

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);
}
}

/// <summary>
/// Asynchronously copies a <see cref="Borehole"/>.
/// Asynchronously gets the <see cref="Borehole"/> with the specified <paramref name="id"/>.
/// </summary>
/// <param name="id">The <see cref="Borehole.Id"/> of the borehole to copy.</param>
/// <param name="workgroupId">The <see cref="Workgroup.Id"/> of the new <see cref="Borehole"/>.</param>
/// <returns>The id of the newly created <see cref="Borehole"/>.</returns>
[HttpPost("copy")]
/// <param name="id">The id of borehole to get.</param>
[HttpGet("{id}")]
[Authorize(Policy = PolicyNames.Viewer)]
public async Task<ActionResult<int>> CopyAsync([Required] int id, [Required] int workgroupId)
public async Task<ActionResult<Borehole>> GetByIdAsync(int id)
{
logger.LogInformation("Copy borehole with id <{BoreholeId}> to workgroup with id <{WorkgroupId}>", id, workgroupId);

var user = await context.Users
.Include(u => u.WorkgroupRoles)
var borehole = await GetBoreholesWithIncludes()
.AsNoTracking()
.SingleOrDefaultAsync(u => u.SubjectId == HttpContext.GetUserSubjectId())
.SingleOrDefaultAsync(l => l.Id == id)
.ConfigureAwait(false);

if (user == null || user.WorkgroupRoles == null || !user.WorkgroupRoles.Any(w => w.WorkgroupId == workgroupId && w.Role == Role.Editor))
if (borehole == null)
{
return Unauthorized();
return NotFound();
}

var borehole = await context.Boreholes
.Include(b => b.Stratigraphies).ThenInclude(s => s.Layers).ThenInclude(l => l.LayerColorCodes)
return Ok(borehole);
}

private IQueryable<Borehole> GetBoreholesWithIncludes()
{
return Context.Boreholes.Include(b => b.Stratigraphies).ThenInclude(s => s.Layers).ThenInclude(l => l.LayerColorCodes)
.Include(b => b.Stratigraphies).ThenInclude(s => s.Layers).ThenInclude(l => l.LayerDebrisCodes)
.Include(b => b.Stratigraphies).ThenInclude(s => s.Layers).ThenInclude(l => l.LayerGrainAngularityCodes)
.Include(b => b.Stratigraphies).ThenInclude(s => s.Layers).ThenInclude(l => l.LayerGrainShapeCodes)
Expand All @@ -62,7 +92,33 @@ public async Task<ActionResult<int>> CopyAsync([Required] int id, [Required] int
.Include(b => b.BoreholeCodelists)
.Include(b => b.Workflows)
.Include(b => b.BoreholeFiles)
.Include(b => b.BoreholeGeometry)
.Include(b => b.BoreholeGeometry);
}

/// <summary>
/// Asynchronously copies a <see cref="Borehole"/>.
/// </summary>
/// <param name="id">The <see cref="Borehole.Id"/> of the borehole to copy.</param>
/// <param name="workgroupId">The <see cref="Workgroup.Id"/> of the new <see cref="Borehole"/>.</param>
/// <returns>The id of the newly created <see cref="Borehole"/>.</returns>
[HttpPost("copy")]
[Authorize(Policy = PolicyNames.Viewer)]
public async Task<ActionResult<int>> CopyAsync([Required] int id, [Required] int workgroupId)
{
Logger.LogInformation("Copy borehole with id <{BoreholeId}> to workgroup with id <{WorkgroupId}>", id, workgroupId);

var user = await Context.Users
.Include(u => u.WorkgroupRoles)
.AsNoTracking()
.SingleOrDefaultAsync(u => u.SubjectId == HttpContext.GetUserSubjectId())
.ConfigureAwait(false);

if (user == null || user.WorkgroupRoles == null || !user.WorkgroupRoles.Any(w => w.WorkgroupId == workgroupId && w.Role == Role.Editor))
{
return Unauthorized();
}

var borehole = await GetBoreholesWithIncludes()
.AsNoTracking()
.SingleOrDefaultAsync(b => b.Id == id)
.ConfigureAwait(false);
Expand All @@ -79,7 +135,7 @@ public async Task<ActionResult<int>> CopyAsync([Required] int id, [Required] int
// if there are no fieldMeasurementResults of hydrotestResults the LoadAsync method will be called but have no effect
foreach (var fieldMeasurement in fieldMeasurements)
{
await context.Entry(fieldMeasurement)
await Context.Entry(fieldMeasurement)
.Collection(f => f.FieldMeasurementResults)
.LoadAsync()
.ConfigureAwait(false);
Expand All @@ -88,7 +144,7 @@ await context.Entry(fieldMeasurement)
var hydrotests = borehole.Observations.OfType<Hydrotest>().ToList();
foreach (var hydrotest in hydrotests)
{
await context.Entry(hydrotest)
await Context.Entry(hydrotest)
.Collection(h => h.HydrotestResults)
.LoadAsync()
.ConfigureAwait(false);
Expand Down Expand Up @@ -207,9 +263,16 @@ await context.Entry(hydrotest)

borehole.OriginalName += " (Copy)";

var entityEntry = await context.AddAsync(borehole).ConfigureAwait(false);
await context.SaveChangesAsync().ConfigureAwait(false);
var entityEntry = await Context.AddAsync(borehole).ConfigureAwait(false);
await Context.SaveChangesAsync().ConfigureAwait(false);

return Ok(entityEntry.Entity.Id);
}

/// <inheritdoc />
protected override async Task<int?> GetBoreholeId(Borehole entity)
{
if (entity == null) return default;
return await Task.FromResult<int?>(entity.Id).ConfigureAwait(false);
}
}
6 changes: 3 additions & 3 deletions src/api/Controllers/BoreholeControllerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public abstract class BoreholeControllerBase<TEntity> : ControllerBase
where TEntity : IIdentifyable, IChangeTracking, new()
{
private readonly BdmsContext context;
private readonly ILogger<TEntity> logger;
private readonly ILogger<BoreholeControllerBase<TEntity>> logger;
private readonly IBoreholeLockService boreholeLockService;

/// <summary>
Expand All @@ -24,14 +24,14 @@ public abstract class BoreholeControllerBase<TEntity> : ControllerBase
/// <summary>
/// Gets the <see cref="ILogger{TEntity}"/> used by the controller.
/// </summary>
protected ILogger<TEntity> Logger => logger;
protected ILogger<BoreholeControllerBase<TEntity>> Logger => logger;

/// <summary>
/// Gets the <see cref="IBoreholeLockService"/> used by the controller.
/// </summary>
protected IBoreholeLockService BoreholeLockService => boreholeLockService;

protected BoreholeControllerBase(BdmsContext context, ILogger<TEntity> logger, IBoreholeLockService boreholeLockService)
protected BoreholeControllerBase(BdmsContext context, ILogger<BoreholeControllerBase<TEntity>> logger, IBoreholeLockService boreholeLockService)
{
this.context = context;
this.logger = logger;
Expand Down
2 changes: 1 addition & 1 deletion src/api/Controllers/CasingController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace BDMS.Controllers;
[Route("api/v{version:apiVersion}/[controller]")]
public class CasingController : BoreholeControllerBase<Casing>
{
public CasingController(BdmsContext context, ILogger<Casing> logger, IBoreholeLockService boreholeLockService)
public CasingController(BdmsContext context, ILogger<CasingController> logger, IBoreholeLockService boreholeLockService)
: base(context, logger, boreholeLockService)
{
}
Expand Down
2 changes: 1 addition & 1 deletion src/api/Controllers/ChronostratigraphyController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace BDMS.Controllers;
[Route("api/v{version:apiVersion}/[controller]")]
public class ChronostratigraphyController : BoreholeControllerBase<ChronostratigraphyLayer>
{
public ChronostratigraphyController(BdmsContext context, ILogger<ChronostratigraphyLayer> logger, IBoreholeLockService boreholeLockService)
public ChronostratigraphyController(BdmsContext context, ILogger<ChronostratigraphyController> logger, IBoreholeLockService boreholeLockService)
: base(context, logger, boreholeLockService)
{
}
Expand Down
2 changes: 1 addition & 1 deletion src/api/Controllers/CompletionController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace BDMS.Controllers;
[Route("api/v{version:apiVersion}/[controller]")]
public class CompletionController : BoreholeControllerBase<Completion>
{
public CompletionController(BdmsContext context, ILogger<Completion> logger, IBoreholeLockService boreholeLockService)
public CompletionController(BdmsContext context, ILogger<CompletionController> logger, IBoreholeLockService boreholeLockService)
: base(context, logger, boreholeLockService)
{
}
Expand Down
2 changes: 1 addition & 1 deletion src/api/Controllers/FaciesDescriptionController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace BDMS.Controllers;
[Route("api/v{version:apiVersion}/[controller]")]
public class FaciesDescriptionController : BoreholeControllerBase<FaciesDescription>
{
public FaciesDescriptionController(BdmsContext context, ILogger<FaciesDescription> logger, IBoreholeLockService boreholeLockService)
public FaciesDescriptionController(BdmsContext context, ILogger<FaciesDescriptionController> logger, IBoreholeLockService boreholeLockService)
: base(context, logger, boreholeLockService)
{
}
Expand Down
2 changes: 1 addition & 1 deletion src/api/Controllers/FieldMeasurementController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace BDMS.Controllers;
[Route("api/v{version:apiVersion}/[controller]")]
public class FieldMeasurementController : BoreholeControllerBase<FieldMeasurement>
{
public FieldMeasurementController(BdmsContext context, ILogger<FieldMeasurement> logger, IBoreholeLockService boreholeLockService)
public FieldMeasurementController(BdmsContext context, ILogger<FieldMeasurementController> logger, IBoreholeLockService boreholeLockService)
: base(context, logger, boreholeLockService)
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace BDMS.Controllers;
[Route("api/v{version:apiVersion}/[controller]")]
public class GroundwaterLevelMeasurementController : BoreholeControllerBase<GroundwaterLevelMeasurement>
{
public GroundwaterLevelMeasurementController(BdmsContext context, ILogger<GroundwaterLevelMeasurement> logger, IBoreholeLockService boreholeLockService)
public GroundwaterLevelMeasurementController(BdmsContext context, ILogger<GroundwaterLevelMeasurementController> logger, IBoreholeLockService boreholeLockService)
: base(context, logger, boreholeLockService)
{
}
Expand Down
2 changes: 1 addition & 1 deletion src/api/Controllers/HydrotestController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace BDMS.Controllers;
[Route("api/v{version:apiVersion}/[controller]")]
public class HydrotestController : BoreholeControllerBase<Hydrotest>
{
public HydrotestController(BdmsContext context, ILogger<Hydrotest> logger, IBoreholeLockService boreholeLockService)
public HydrotestController(BdmsContext context, ILogger<HydrotestController> logger, IBoreholeLockService boreholeLockService)
: base(context, logger, boreholeLockService)
{
}
Expand Down
2 changes: 1 addition & 1 deletion src/api/Controllers/InstrumentationController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace BDMS.Controllers;
[Route("api/v{version:apiVersion}/[controller]")]
public class InstrumentationController : BoreholeControllerBase<Instrumentation>
{
public InstrumentationController(BdmsContext context, ILogger<Instrumentation> logger, IBoreholeLockService boreholeLockService)
public InstrumentationController(BdmsContext context, ILogger<InstrumentationController> logger, IBoreholeLockService boreholeLockService)
: base(context, logger, boreholeLockService)
{
}
Expand Down
2 changes: 1 addition & 1 deletion src/api/Controllers/LayerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace BDMS.Controllers;
[Route("api/v{version:apiVersion}/[controller]")]
public class LayerController : BoreholeControllerBase<Layer>
{
public LayerController(BdmsContext context, ILogger<Layer> logger, IBoreholeLockService boreholeLockService)
public LayerController(BdmsContext context, ILogger<LayerController> logger, IBoreholeLockService boreholeLockService)
: base(context, logger, boreholeLockService)
{
}
Expand Down
2 changes: 1 addition & 1 deletion src/api/Controllers/LithologicalDescriptionController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace BDMS.Controllers;
[Route("api/v{version:apiVersion}/[controller]")]
public class LithologicalDescriptionController : BoreholeControllerBase<LithologicalDescription>
{
public LithologicalDescriptionController(BdmsContext context, ILogger<LithologicalDescription> logger, IBoreholeLockService boreholeLockService)
public LithologicalDescriptionController(BdmsContext context, ILogger<LithologicalDescriptionController> logger, IBoreholeLockService boreholeLockService)
: base(context, logger, boreholeLockService)
{
}
Expand Down
2 changes: 1 addition & 1 deletion src/api/Controllers/LithostratigraphyController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace BDMS.Controllers;
[Route("api/v{version:apiVersion}/[controller]")]
public class LithostratigraphyController : BoreholeControllerBase<LithostratigraphyLayer>
{
public LithostratigraphyController(BdmsContext context, ILogger<LithostratigraphyLayer> logger, IBoreholeLockService boreholeLockService)
public LithostratigraphyController(BdmsContext context, ILogger<LithostratigraphyController> logger, IBoreholeLockService boreholeLockService)
: base(context, logger, boreholeLockService)
{
}
Expand Down
2 changes: 1 addition & 1 deletion src/api/Controllers/SectionController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace BDMS.Controllers;
[Route("api/v{version:apiVersion}/[controller]")]
public class SectionController : BoreholeControllerBase<Section>
{
public SectionController(BdmsContext context, ILogger<Section> logger, IBoreholeLockService boreholeLockService)
public SectionController(BdmsContext context, ILogger<SectionController> logger, IBoreholeLockService boreholeLockService)
: base(context, logger, boreholeLockService)
{
}
Expand Down
2 changes: 1 addition & 1 deletion src/api/Controllers/StratigraphyController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace BDMS.Controllers;
[Route("api/v{version:apiVersion}/[controller]")]
public class StratigraphyController : BoreholeControllerBase<Stratigraphy>
{
public StratigraphyController(BdmsContext context, ILogger<Stratigraphy> logger, IBoreholeLockService boreholeLockService)
public StratigraphyController(BdmsContext context, ILogger<StratigraphyController> logger, IBoreholeLockService boreholeLockService)
: base(context, logger, boreholeLockService)
{
}
Expand Down
2 changes: 1 addition & 1 deletion src/api/Controllers/WaterIngressController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace BDMS.Controllers;
[Route("api/v{version:apiVersion}/[controller]")]
public class WaterIngressController : BoreholeControllerBase<WaterIngress>
{
public WaterIngressController(BdmsContext context, ILogger<WaterIngress> logger, IBoreholeLockService boreholeLockService)
public WaterIngressController(BdmsContext context, ILogger<WaterIngressController> logger, IBoreholeLockService boreholeLockService)
: base(context, logger, boreholeLockService)
{
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Controllers/BackfillControllerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void TestInitialize()
boreholeLockServiceMock
.Setup(x => x.IsBoreholeLockedAsync(It.IsAny<int?>(), It.IsAny<string?>()))
.ReturnsAsync(false);
controller = new BackfillController(context, new Mock<ILogger<Backfill>>().Object, boreholeLockServiceMock.Object) { ControllerContext = GetControllerContextAdmin() };
controller = new BackfillController(context, new Mock<ILogger<BackfillController>>().Object, boreholeLockServiceMock.Object) { ControllerContext = GetControllerContextAdmin() };
}

[TestCleanup]
Expand Down
Loading

0 comments on commit 2059040

Please sign in to comment.