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

#2534 UID Mapping #2567

Merged
5 changes: 5 additions & 0 deletions Src/WitsmlExplorer.Api/Configuration/Dependencies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ public static void ConfigureDependencies(this IServiceCollection services, IConf
.AsPublicImplementedInterfaces();
AddRepository<Server, Guid>(services, configuration);
AddRepository<LogCurvePriority, string>(services, configuration);
AddRepository<UidMappingCollection, string>(services, configuration);
services.AddSingleton<ICredentialsService, CredentialsService>();
services.AddSingleton<IJobCache, JobCache>();
services.AddSingleton<IJobQueue, JobQueue>();
services.AddSingleton<IWitsmlSystemCredentials, WitsmlSystemCredentials>();
services.AddScoped<IWitsmlClientProvider, WitsmlClientProvider>();
services.AddSingleton<ICredentialsCache, CredentialsCache>();
services.AddSingleton<IJobProgressService, JobProgressService>();
services.AddSingleton<IUidMappingService, UidMappingService>();
}

private static void AddRepository<TDocument, T>(IServiceCollection services, IConfiguration configuration) where TDocument : DbDocument<T>
Expand Down Expand Up @@ -77,6 +79,9 @@ public static void InitializeRepository(this IApplicationBuilder app)

IDocumentRepository<LogCurvePriority, string> logCurvePriorityRepository = app.ApplicationServices.GetService<IDocumentRepository<LogCurvePriority, string>>();
logCurvePriorityRepository?.InitClientAsync().GetAwaiter().GetResult();

IDocumentRepository<UidMappingCollection, string> uidMappingCollectionRepository = app.ApplicationServices.GetService<IDocumentRepository<UidMappingCollection, string>>();
uidMappingCollectionRepository?.InitClientAsync().GetAwaiter().GetResult();
}
}
}
103 changes: 103 additions & 0 deletions Src/WitsmlExplorer.Api/HttpHandlers/UidMappingHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading.Tasks;

using LiteDB;

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;

using WitsmlExplorer.Api.Models;
using WitsmlExplorer.Api.Services;

namespace WitsmlExplorer.Api.HttpHandlers
{
public static class UidMappingHandler
{
[Produces(typeof(UidMapping))]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status409Conflict)]
public static async Task<IResult> CreateUidMapping([FromBody] UidMapping uidMapping, [FromServices] IUidMappingService uidMappingService, HttpContext httpContext)
{
if (!Validate(uidMapping))
{
return TypedResults.BadRequest();
}

var result = await uidMappingService.CreateUidMapping(uidMapping, httpContext);

if (result != null)
{
return TypedResults.Ok(uidMapping);
}
else
{
return TypedResults.Conflict();
}
}

[Produces(typeof(UidMapping))]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public static async Task<IResult> UpdateUidMapping([FromBody] UidMapping uidMapping, [FromServices] IUidMappingService uidMappingService, HttpContext httpContext)
{
if (!Validate(uidMapping))
{
return TypedResults.BadRequest();
}

var result = await uidMappingService.UpdateUidMapping(uidMapping, httpContext);

if (result != null)
{
return TypedResults.Ok(uidMapping);
}
else
{
return TypedResults.NotFound();
}
}

[Produces(typeof(ICollection<UidMapping>))]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public static async Task<IResult> QueryUidMapping([FromBody] UidMappingDbQuery query, [FromServices] IUidMappingService uidMappingService)
{
if (!ValidateQuery(query))
{
return TypedResults.BadRequest();
}

return TypedResults.Ok(await uidMappingService.QueryUidMapping(query));
}

[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public static async Task<IResult> DeleteUidMapping([FromBody] UidMappingDbQuery query, [FromServices] IUidMappingService uidMappingService)
{
if (await uidMappingService.DeleteUidMapping(query))
{
return TypedResults.NoContent();
}
else
{
return TypedResults.NotFound();
}
}

private static bool Validate(UidMapping uidMapping)
{
return uidMapping != null && uidMapping.SourceServerId != Guid.Empty && uidMapping.TargetServerId != Guid.Empty
&& !uidMapping.SourceWellId.IsNullOrEmpty() && !uidMapping.TargetWellId.IsNullOrEmpty()
&& !uidMapping.SourceWellboreId.IsNullOrEmpty() && !uidMapping.TargetWellboreId.IsNullOrEmpty();
}

private static bool ValidateQuery(UidMappingDbQuery query)
{
return query != null && query.SourceServerId != Guid.Empty && query.TargetServerId != Guid.Empty
&& ((query.SourceWellId.IsNullOrEmpty() && query.SourceWellboreId.IsNullOrEmpty())
|| (!query.SourceWellId.IsNullOrEmpty() && !query.SourceWellboreId.IsNullOrEmpty()));
}
}
}
79 changes: 79 additions & 0 deletions Src/WitsmlExplorer.Api/Models/UidMapping.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace WitsmlExplorer.Api.Models
{
public class UidMapping
{
[JsonPropertyName("sourceServerId")]
public Guid SourceServerId { get; set; }

[JsonPropertyName("sourceWellId")]
public string SourceWellId { get; set; }

[JsonPropertyName("sourceWellboreId")]
public string SourceWellboreId { get; set; }

[JsonPropertyName("targetServerId")]
public Guid TargetServerId { get; set; }

[JsonPropertyName("targetWellId")]
public string TargetWellId { get; set; }

[JsonPropertyName("targetWellboreId")]
public string TargetWellboreId { get; set; }

[JsonPropertyName("username")]
public string Username { get; set; }
vaclavbasniar marked this conversation as resolved.
Show resolved Hide resolved

[JsonPropertyName("timestamp")]
public DateTime? Timestamp { get; set; }

public override string ToString()
{
return JsonSerializer.Serialize(this);
}
}

public class UidMappingDbQuery
{
[JsonPropertyName("sourceServerId")]
public Guid SourceServerId { get; set; }

[JsonPropertyName("sourceWellId")]
public string SourceWellId { get; set; }

[JsonPropertyName("sourceWellboreId")]
public string SourceWellboreId { get; set; }

[JsonPropertyName("targetServerId")]
public Guid TargetServerId { get; set; }

public override string ToString()
{
return JsonSerializer.Serialize(this);
}
}

public class UidMappingKey
{
public UidMappingKey() { }
public UidMappingKey(Guid sourceServerId, Guid targetServerId)
{
SourceServerId = sourceServerId;
TargetServerId = targetServerId;
}

[JsonPropertyName("sourceServerId")]
public Guid SourceServerId { get; set; }

[JsonPropertyName("targetServerId")]
public Guid TargetServerId { get; set; }

public override string ToString()
{
return JsonSerializer.Serialize(this);
}
}
}
24 changes: 24 additions & 0 deletions Src/WitsmlExplorer.Api/Models/UidMappingCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;

using WitsmlExplorer.Api.Repositories;

namespace WitsmlExplorer.Api.Models
{
public class UidMappingCollection : DbDocument<string>
{
public UidMappingCollection(string id) : base(id)
{
MappingCollection = new List<UidMapping>();
}

[JsonPropertyName("mappingCollection")]
public List<UidMapping> MappingCollection { get; set; }

public override string ToString()
{
return JsonSerializer.Serialize(this);
}
}
}
5 changes: 5 additions & 0 deletions Src/WitsmlExplorer.Api/Routes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public static void ConfigureApi(this WebApplication app, IConfiguration configur
app.MapPost("/universal/logCurvePriority", LogCurvePriorityHandler.SetPrioritizedUniversalCurves, useOAuth2);
app.MapPost("/wells/{wellUid}/wellbores/{wellboreUid}/logCurvePriority", LogCurvePriorityHandler.SetPrioritizedLocalCurves, useOAuth2);

app.MapPost("/uidmapping/", UidMappingHandler.CreateUidMapping, useOAuth2, AuthorizationPolicyRoles.ADMIN);
app.MapPatch("/uidmapping/", UidMappingHandler.UpdateUidMapping, useOAuth2, AuthorizationPolicyRoles.ADMIN);
app.MapPost("/uidmapping/query/", UidMappingHandler.QueryUidMapping, useOAuth2);
app.MapPost("/uidmapping/deletequery/", UidMappingHandler.DeleteUidMapping, useOAuth2, AuthorizationPolicyRoles.ADMIN);

Dictionary<EntityType, string> types = EntityTypeHelper.ToPluralLowercase();
Dictionary<EntityType, string> routes = types.ToDictionary(entry => entry.Key, entry => "/wells/{wellUid}/wellbores/{wellboreUid}/" + entry.Value);

Expand Down
Loading