JsonElement instead of Request body Attachment Class Bump Ready to test GetLastGroupIdWithValue Changed to v4
72 lines
3.0 KiB
C#
72 lines
3.0 KiB
C#
using Newtonsoft.Json.Linq;
|
|
using OI.Metrology.Shared.DataModels;
|
|
using OI.Metrology.Shared.Models;
|
|
using OI.Metrology.Shared.Models.Stateless;
|
|
using OI.Metrology.Shared.Services;
|
|
using System.Net;
|
|
using System.Text.Json;
|
|
|
|
namespace OI.Metrology.Server.Repository;
|
|
|
|
public class InboundRepository : IInboundRepository
|
|
{
|
|
|
|
bool IInboundRepository.IsIPAddressAllowed(string inboundApiAllowedIPList, IPAddress? remoteIP)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(inboundApiAllowedIPList))
|
|
return true;
|
|
if (remoteIP is null)
|
|
return false;
|
|
byte[] remoteIPBytes = remoteIP.GetAddressBytes();
|
|
string[] allowedIPs = inboundApiAllowedIPList.Split(';');
|
|
foreach (string ip in allowedIPs)
|
|
{
|
|
IPAddress? parsedIP;
|
|
if (IPAddress.TryParse(ip, out parsedIP))
|
|
{
|
|
if (parsedIP.GetAddressBytes().SequenceEqual(remoteIPBytes))
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
DataResponse IInboundRepository.Data(IMetrologyRepository metrologyRepository, IInboundDataService inboundDataService, string toolTypeName, JsonElement? jsonElement)
|
|
{
|
|
DataResponse result = new();
|
|
if (jsonElement is null || jsonElement.Value.ValueKind != JsonValueKind.Object)
|
|
throw new Exception("Invalid body!");
|
|
string? json = jsonElement.ToString();
|
|
JToken jToken = (string.IsNullOrEmpty(json) ? JToken.Parse("{}") : JToken.Parse(json)) ??
|
|
throw new Exception($"Invalid body: {json}");
|
|
ToolType toolType = metrologyRepository.GetToolTypeByName(toolTypeName) ??
|
|
throw new Exception($"Invalid tool type: {toolTypeName}");
|
|
List<ToolTypeMetadata> metaData = metrologyRepository.GetToolTypeMetadataByToolTypeID(toolType.ID).ToList();
|
|
inboundDataService.ValidateJSONFields(jToken, 0, metaData, result.Errors, result.Warnings);
|
|
if (result.Errors.Count == 0)
|
|
{
|
|
try
|
|
{
|
|
result.HeaderID = inboundDataService.DoSQLInsert(jToken, toolType, metaData);
|
|
result.Success = result.HeaderID > 0;
|
|
}
|
|
catch (Exception ex) { result.Errors.Add(ex.Message); }
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
void IInboundRepository.AttachFile(IMetrologyRepository metrologyRepository, IAttachmentsService attachmentsService, string toolTypeName, Attachment? attachment)
|
|
{
|
|
if (attachment is null)
|
|
throw new Exception("Invalid body!");
|
|
ToolType toolType = metrologyRepository.GetToolTypeByName(toolTypeName) ??
|
|
throw new Exception($"Invalid tool type: {toolTypeName}");
|
|
if (string.IsNullOrWhiteSpace(attachment.DestinationFileName))
|
|
throw new Exception("Empty filename");
|
|
if (attachment.DestinationFileName.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0)
|
|
throw new Exception("Invalid filename");
|
|
attachmentsService.SaveAttachment(toolType, attachment);
|
|
}
|
|
|
|
} |