Skip to content

Commit

Permalink
merged main
Browse files Browse the repository at this point in the history
  • Loading branch information
vaclavbasniar committed Oct 24, 2024
1 parent 56b20d3 commit 96fd79d
Show file tree
Hide file tree
Showing 23 changed files with 1,937 additions and 190 deletions.
2 changes: 2 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, UidMappingKey>(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
98 changes: 98 additions & 0 deletions Src/WitsmlExplorer.Api/HttpHandlers/UidMappingHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

using Amazon.SecurityToken.Model;

using LiteDB;

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

using WitsmlExplorer.Api.Models;
using WitsmlExplorer.Api.Repositories;
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>))]
public static async Task<IResult> QueryUidMapping([FromBody] UidMappingDbQuery query, [FromServices] IUidMappingService uidMappingService)
{
return TypedResults.Ok(await uidMappingService.QueryUidMapping(query));
}

[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public static async Task<IResult> QueryDeleteUidMapping([FromBody] UidMappingDbQuery query, [FromServices] IUidMappingService uidMappingService)
{
if (await uidMappingService.QueryDeleteUidMapping(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());
}

}
}
81 changes: 81 additions & 0 deletions Src/WitsmlExplorer.Api/Models/UidMapping.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;

using WitsmlExplorer.Api.Repositories;

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

[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);
}
}
}
25 changes: 25 additions & 0 deletions Src/WitsmlExplorer.Api/Models/UidMappingCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
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<UidMappingKey>
{
public UidMappingCollection(UidMappingKey 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.QueryDeleteUidMapping, 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

0 comments on commit 96fd79d

Please sign in to comment.