Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
44 changes: 44 additions & 0 deletions SW.Bitween.Api/Resources/Mappers/Preview.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Threading.Tasks;
using SW.Bitween.Domain.Accounts;
using SW.Bitween.NativeAdapters.JsonMapper;
using SW.PrimitiveTypes;

namespace SW.Bitween.Resources.Mappers;

public class MapperPreviewRequest
{
public string ScribanTemplate { get; set; } = "{}";
public string InputJson { get; set; } = "{}";
}

public class MapperPreviewResponse
{
public string? OutputJson { get; set; }
public string? Error { get; set; }
}

public class Preview : ICommandHandler<MapperPreviewRequest, MapperPreviewResponse>
{
private readonly RequestContext _requestContext;

public Preview(RequestContext requestContext)
{
_requestContext = requestContext;
}

public Task<MapperPreviewResponse> Handle(MapperPreviewRequest request)
{
_requestContext.EnsureAccess(AccountRole.Admin, AccountRole.Member);

try
{
var output = ScribanJsonHelper.Render(request.ScribanTemplate, request.InputJson);
return Task.FromResult(new MapperPreviewResponse { OutputJson = output });
}
catch (Exception ex)
{
return Task.FromResult(new MapperPreviewResponse { Error = ex.Message });
}
}
}
82 changes: 82 additions & 0 deletions SW.Bitween.Api/Resources/Subscriptions/SaveMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using FluentValidation;
using Microsoft.Extensions.DependencyInjection;
using SW.EfCoreExtensions;
using SW.Bitween.Domain;
using SW.Bitween.Model;
using SW.PrimitiveTypes;
using System;
using System.Linq;
using System.Threading.Tasks;
using SW.Bitween.Domain.Accounts;

namespace SW.Bitween.Resources.Subscriptions
{
[HandlerName("savemapper")]
public class SaveMapper : ICommandHandler<int, SubscriptionSaveMapper, object>
{
private readonly BitweenDbContext _dbContext;
private readonly IInfolinkCache _BitweenCache;
private readonly RequestContext _requestContext;

public SaveMapper(BitweenDbContext dbContext, IInfolinkCache BitweenCache, RequestContext requestContext)
{
_dbContext = dbContext;
_BitweenCache = BitweenCache;
_requestContext = requestContext;
}

public async Task<object> Handle(int key, SubscriptionSaveMapper model)
{
_requestContext.EnsureAccess(AccountRole.Admin, AccountRole.Member);
var entity = await _dbContext.FindAsync<Subscription>(key);

entity.MapperId = model.MapperId;
entity.SetDictionaries(
entity.HandlerProperties,
model.MapperProperties.ToDictionary(),
entity.ReceiverProperties,
entity.DocumentFilter,
entity.ValidatorProperties
);

await _dbContext.SaveChangesAsync();
_BitweenCache.BroadcastRevoke();
return null;
}

private class Validate : AbstractValidator<SubscriptionSaveMapper>
{
public Validate(NativeAdapterDiscoveryService nativeAdapterDiscovery, IServiceProvider serviceProvider)
{
RuleFor(i => i.MapperId).NotEmpty();

When(i => i.MapperId != null, () =>
{
RuleFor(i => i.MapperProperties).CustomAsync(async (i, context, _) =>
{
var mapperId = ((SubscriptionSaveMapper)context.InstanceToValidate).MapperId;
var mustProps = Enumerable.Empty<string>();

if (mapperId.StartsWith(NativeAdapterDiscoveryService.NativePrefix, StringComparison.OrdinalIgnoreCase))
{
var properties = nativeAdapterDiscovery.GetExpectedStartupValues(mapperId);
mustProps = properties.Where(p => p.Value.EndsWith(" *")).Select(p => p.Key);
}
else
{
var serverless = serviceProvider.GetRequiredService<IServerlessService>();
await serverless.StartAsync(mapperId, null);
mustProps = (await serverless.GetExpectedStartupValues())
.Where(p => p.Value.Optional == false).Select(p => p.Key);
}

var missing = mustProps.ToHashSet(StringComparer.OrdinalIgnoreCase)
.Except(i.Where(p => !string.IsNullOrEmpty(p.Value)).Select(p => p.Key));
if (missing.Any())
context.AddFailure($"Missing: {string.Join(",", missing)}");
});
});
}
}
}
}

This file was deleted.

119 changes: 0 additions & 119 deletions SW.Bitween.NativeAdapters/JsonFieldMapper/NativeJsonFieldMapper.cs

This file was deleted.

9 changes: 9 additions & 0 deletions SW.Bitween.NativeAdapters/JsonMapper/JsonMapperInput.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.ComponentModel.DataAnnotations;

namespace SW.Bitween.NativeAdapters.JsonMapper;

public class JsonMapperInput
{
[Required]
public string ScribanTemplate { get; set; } = "{}";
}
26 changes: 26 additions & 0 deletions SW.Bitween.NativeAdapters/JsonMapper/NativeJSONMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using SW.Bitween.NativeAdapters.JsonMapper;
using SW.PrimitiveTypes;

namespace SW.Bitween.NativeAdapters;

public class NativeJSONMapper : INativeInfolinkHandler
{
public string Name => "NativeJSONMapper";
public Type StartupValuesType => typeof(JsonMapperInput);

private JsonMapperInput _options = new();

public void InitializeStartupValues(IDictionary<string, string> settings)
{
_options = new JsonMapperInput
{
ScribanTemplate = settings.TryGetValue("ScribanTemplate", out var t) ? t : "{}"
};
}

public Task<XchangeFile> Handle(XchangeFile xchangeFile)
{
var outputJson = ScribanJsonHelper.Render(_options.ScribanTemplate, xchangeFile.Data);
return Task.FromResult(new XchangeFile(outputJson));
}
}
Loading