Skip to content

Commit

Permalink
Json import api endpoint (#1714)
Browse files Browse the repository at this point in the history
  • Loading branch information
MiraGeowerkstatt authored Dec 16, 2024
2 parents 4809218 + 9fe668f commit 3dd6111
Show file tree
Hide file tree
Showing 25 changed files with 9,092 additions and 144 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- Add JSON export for single and multiple boreholes.
- The workgroup name is now displayed in the borehole location tab.
- Added new API endpoint to retrieve all boreholes.
- Add JSON import for boreholes.

### Changed

Expand Down
12 changes: 12 additions & 0 deletions src/api/BdmsContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ public static void SeedData(this BdmsContext context)
var fakeBoreholes = new Faker<Borehole>()
.StrictMode(true)
.RuleFor(o => o.Id, f => borehole_ids++)
.RuleFor(o => o.Stratigraphies, _ => new Collection<Stratigraphy>())
.RuleFor(o => o.Completions, _ => new Collection<Completion>())
.RuleFor(o => o.Sections, _ => new Collection<Section>())
.RuleFor(o => o.Observations, _ => new Collection<Observation>())
.RuleFor(o => o.CreatedById, f => f.PickRandom(userRange))
.RuleFor(o => o.CreatedBy, _ => default!)
.RuleFor(o => o.UpdatedById, f => f.PickRandom(userRange))
Expand Down Expand Up @@ -258,6 +262,11 @@ public static void SeedData(this BdmsContext context)
var fakeStratigraphies = new Faker<Stratigraphy>()
.StrictMode(true)
.RuleFor(o => o.Id, f => stratigraphy_ids++)
.RuleFor(o => o.Layers, _ => new Collection<Layer>())
.RuleFor(o => o.LithologicalDescriptions, _ => new Collection<LithologicalDescription>())
.RuleFor(o => o.LithostratigraphyLayers, _ => new Collection<LithostratigraphyLayer>())
.RuleFor(o => o.ChronostratigraphyLayers, _ => new Collection<ChronostratigraphyLayer>())
.RuleFor(o => o.FaciesDescriptions, _ => new Collection<FaciesDescription>())
.RuleFor(o => o.CreatedById, f => f.PickRandom(userRange).OrNull(f, .05f))
.RuleFor(o => o.CreatedBy, _ => default!)
.RuleFor(o => o.BoreholeId, f => f.PickRandom(boreholeRange).OrNull(f, .05f))
Expand Down Expand Up @@ -569,6 +578,9 @@ void SeedCodelists<T>(Faker<T> faker)
var completionRange = Enumerable.Range(completion_ids, 500);
var fakeCompletions = new Faker<Completion>()
.StrictMode(true)
.RuleFor(c => c.Instrumentations, _ => new Collection<Instrumentation>())
.RuleFor(c => c.Casings, _ => new Collection<Casing>())
.RuleFor(c => c.Backfills, _ => new Collection<Backfill>())
.RuleFor(c => c.BoreholeId, f => f.PickRandom(richBoreholeRange))
.RuleFor(c => c.Borehole, _ => default!)
.RuleFor(c => c.Created, f => f.Date.Past().ToUniversalTime())
Expand Down
73 changes: 14 additions & 59 deletions src/api/Controllers/BoreholeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,16 @@ public async Task<ActionResult<int>> CopyAsync([Required] int id, [Required] int

if (borehole == null) return NotFound();

// Set ids of copied entities to zero. Entities with an id of zero are added as new entities to the DB.
borehole.Id = 0;
borehole.MarkAsNew();
borehole.Completions?.MarkAsNew();
borehole.Sections?.MarkAsNew();

foreach (var stratigraphy in borehole.Stratigraphies)
{
stratigraphy.Id = 0;
stratigraphy.MarkAsNew();
foreach (var layer in stratigraphy.Layers)
{
layer.Id = 0;
layer.MarkAsNew();
layer.LayerColorCodes?.ResetLayerIds();
layer.LayerDebrisCodes?.ResetLayerIds();
layer.LayerGrainShapeCodes?.ResetLayerIds();
Expand All @@ -163,69 +165,22 @@ public async Task<ActionResult<int>> CopyAsync([Required] int id, [Required] int
layer.LayerUscs3Codes?.ResetLayerIds();
}

foreach (var lithologicalDescription in stratigraphy.LithologicalDescriptions)
{
lithologicalDescription.Id = 0;
}

foreach (var faciesDescription in stratigraphy.FaciesDescriptions)
{
faciesDescription.Id = 0;
}

foreach (var chronostratigraphy in stratigraphy.ChronostratigraphyLayers)
{
chronostratigraphy.Id = 0;
}

foreach (var lithostratigraphy in stratigraphy.LithostratigraphyLayers)
{
lithostratigraphy.Id = 0;
}
}

foreach (var completion in borehole.Completions)
{
completion.Id = 0;
foreach (var casing in completion.Casings)
{
casing.Id = 0;
foreach (var casingElement in casing.CasingElements)
{
casingElement.Id = 0;
}
}

foreach (var instrumentation in completion.Instrumentations)
{
instrumentation.Id = 0;
}

foreach (var backfill in completion.Backfills)
{
backfill.Id = 0;
}
}

foreach (var section in borehole.Sections)
{
section.Id = 0;
foreach (var sectionElement in section.SectionElements)
{
sectionElement.Id = 0;
}
stratigraphy.LithologicalDescriptions?.MarkAsNew();
stratigraphy.FaciesDescriptions?.MarkAsNew();
stratigraphy.ChronostratigraphyLayers?.MarkAsNew();
stratigraphy.LithostratigraphyLayers?.MarkAsNew();
}

foreach (var observation in borehole.Observations)
{
observation.Id = 0;
observation.MarkAsNew();
if (observation is FieldMeasurement fieldMeasurement)
{
if (fieldMeasurement.FieldMeasurementResults != null)
{
foreach (var fieldMeasurementResult in fieldMeasurement.FieldMeasurementResults)
{
fieldMeasurementResult.Id = 0;
fieldMeasurementResult.MarkAsNew();
}
}
}
Expand All @@ -236,7 +191,7 @@ public async Task<ActionResult<int>> CopyAsync([Required] int id, [Required] int
{
foreach (var hydrotestResult in hydrotest.HydrotestResults)
{
hydrotestResult.Id = 0;
hydrotestResult.MarkAsNew();
}
}

Expand Down Expand Up @@ -271,7 +226,7 @@ public async Task<ActionResult<int>> CopyAsync([Required] int id, [Required] int

foreach (var boreholeGeometry in borehole.BoreholeGeometry)
{
boreholeGeometry.Id = 0;
boreholeGeometry.MarkAsNew();
}

borehole.UpdatedBy = null;
Expand Down
7 changes: 7 additions & 0 deletions src/api/Controllers/FileTypeChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ public static class FileTypeChecker
/// <returns><c>true</c> if the <paramref name="file"/> is a csv file; <c>false</c> otherwise.</returns>
public static bool IsCsv(IFormFile file) => HasCorrectFileExtension(file, ".csv");

/// <summary>
/// Checks if the <paramref name="file"/> is a JSON file.
/// </summary>
/// <param name="file">The file to check the type for.</param>
/// <returns><c>true</c> if the <paramref name="file"/> is a JSON file; <c>false</c> otherwise.</returns>
public static bool IsJson(IFormFile file) => HasCorrectFileExtension(file, ".json");

/// <summary>
/// Checks if the <paramref name="file"/> is of the expected type.
/// </summary>
Expand Down
Loading

0 comments on commit 3dd6111

Please sign in to comment.