Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json import api endpoint #1714

Draft
wants to merge 36 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
4b692a9
Update model properties to enable json import
Lehats Nov 28, 2024
ce22aa1
Add json import to upload controller
Lehats Nov 28, 2024
1105e03
Add json import tests
Lehats Nov 28, 2024
78d4d0f
Update changelog
Lehats Nov 28, 2024
69b6b9f
Fix warnings
Lehats Nov 28, 2024
b5eeb10
Fix identation
Lehats Nov 28, 2024
135ae19
fix tests
Lehats Nov 28, 2024
8294d67
Update properties of LayerCode
Lehats Nov 29, 2024
02f0938
Fix float comparisation
Lehats Dec 3, 2024
c5fdb19
Merge branch 'main' into json-import-api
Lehats Dec 3, 2024
75d9b18
Update json import test data
Lehats Dec 3, 2024
cc7c31c
Create an extension method for IIdentifyable to recursively mark an o…
Lehats Dec 3, 2024
3f68f07
Use mark as new extension method in controllers
Lehats Dec 3, 2024
e977ad7
Replace hard coded assertations in upload tests
Lehats Dec 4, 2024
5e0b159
Remove unnecessary null checks
Lehats Dec 4, 2024
c599440
Provide format provider for date parse
Lehats Dec 4, 2024
79c3543
Reduce complexity of mark as new extension method
Lehats Dec 4, 2024
b8b19a0
Reduce complexity of borehole validation during upload
Lehats Dec 4, 2024
a399911
Merge branch 'main' into json-import-api
Lehats Dec 4, 2024
01dd220
Rename upload json action method
Lehats Dec 5, 2024
611d148
Change to single line if statements
Lehats Dec 5, 2024
053d518
Return the response on the same line
Lehats Dec 5, 2024
456929a
Optimize json content reading and deserialization in UploadJsonFileAsync
Lehats Dec 5, 2024
d750330
Fix typo
Lehats Dec 5, 2024
17f1ad7
Add logging to upload json file method
Lehats Dec 5, 2024
d27a2c1
Optimize duplicate detection in file by using count
Lehats Dec 5, 2024
195a8ae
Reduce complexity of borehole import validation methods
Lehats Dec 5, 2024
b20a7cf
Add comment to added workflow
Lehats Dec 5, 2024
5374268
Improve comments and complexity of mark as new extension methods
Lehats Dec 5, 2024
bd68cfb
Merge branch 'main' into json-import-api
Lehats Dec 5, 2024
6028fc2
Update error logging in upload controller
Lehats Dec 5, 2024
c66279c
Fix test
Lehats Dec 5, 2024
796339f
Fix assert
Lehats Dec 5, 2024
4076c69
Optimize json file deserialization
Lehats Dec 6, 2024
eb9f4b7
Use deserialize async method
Lehats Dec 6, 2024
bfd71c3
Merge branch 'main' into json-import-api
Lehats Dec 6, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,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
92 changes: 11 additions & 81 deletions src/api/Controllers/BoreholeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,17 @@ await Context.Entry(hydrotest)
#pragma warning restore CS8603
}

// 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();
borehole.Observations?.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 @@ -217,83 +220,10 @@ await Context.Entry(hydrotest)
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;
}
}

foreach (var observation in borehole.Observations)
{
observation.Id = 0;
if (observation is FieldMeasurement fieldMeasurement)
{
if (fieldMeasurement.FieldMeasurementResults != null)
{
foreach (var fieldMeasurementResult in fieldMeasurement.FieldMeasurementResults)
{
fieldMeasurementResult.Id = 0;
}
}
}

if (observation is Hydrotest hydrotest)
{
if (hydrotest.HydrotestResults != null)
{
foreach (var hydrotestResult in hydrotest.HydrotestResults)
{
hydrotestResult.Id = 0;
}
}
}
stratigraphy.LithologicalDescriptions?.MarkAsNew();
stratigraphy.FaciesDescriptions?.MarkAsNew();
stratigraphy.ChronostratigraphyLayers?.MarkAsNew();
stratigraphy.LithostratigraphyLayers?.MarkAsNew();
}

foreach (var boreholeFile in borehole.BoreholeFiles)
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
Loading