Better data (BioRad)

EC / IFX
FromBody bug fix
Made ID optional and are get all files now
This commit is contained in:
2023-03-08 12:47:22 -07:00
parent de048b6842
commit 8040a7c6b5
6 changed files with 248 additions and 87 deletions

View File

@ -15,20 +15,28 @@ public class ExportController : Controller, IExportController<IActionResult>
public ExportController(IExportRepository exportRepository) =>
_ExportRepository = exportRepository;
private static HeaderCommon GetHeaderCommon(Stream stream)
private static string? GetJson(Stream stream)
{
HeaderCommon? result;
string? result;
if (!stream.CanRead)
result = new();
result = null;
else
{
Task<string> task = new StreamReader(stream).ReadToEndAsync();
result = string.IsNullOrEmpty(task.Result) ? null : JsonSerializer.Deserialize<HeaderCommon>(task.Result);
result ??= new();
result = task.Result;
}
return result;
}
private static HeaderCommon GetHeaderCommon(Stream stream)
{
HeaderCommon? result;
string? json = GetJson(stream);
result = string.IsNullOrEmpty(json) ? null : JsonSerializer.Deserialize<HeaderCommon>(json);
result ??= new();
return result;
}
[HttpGet]
[Route("export")]
public IActionResult GetExport() =>

View File

@ -30,18 +30,23 @@ public partial class InboundController : ControllerBase, IInboundController<IAct
_MetrologyRepository = metrologyRepository;
}
private static JToken GetJToken(Stream stream)
private static string? GetJson(Stream stream)
{
JToken jsonbody;
string? result;
if (!stream.CanRead)
jsonbody = JToken.Parse("{}");
result = null;
else
{
if (stream.CanSeek)
_ = stream.Seek(0, SeekOrigin.Begin);
string json = new StreamReader(stream).ReadToEnd();
jsonbody = JToken.Parse(json);
Task<string> task = new StreamReader(stream).ReadToEndAsync();
result = task.Result;
}
return result;
}
private static JToken GetJToken(Stream stream)
{
string? json = GetJson(stream);
JToken jsonbody = string.IsNullOrEmpty(json) ? JToken.Parse("{}") : JToken.Parse(json);
return jsonbody;
}

View File

@ -9,7 +9,6 @@ namespace OI.Metrology.Server.Repository;
public class ExportRepository : IExportRepository
{
private readonly string _MockRoot;
private readonly Serilog.ILogger _Log;
private readonly string _RepositoryName;
private readonly AppSettings _AppSettings;
@ -18,7 +17,6 @@ public class ExportRepository : IExportRepository
public ExportRepository(AppSettings appSettings)
{
_AppSettings = appSettings;
_MockRoot = appSettings.MockRoot;
_RdsToHeaderCommonCollection = new();
_RepositoryName = nameof(ExportRepository)[..^10];
_Log = Serilog.Log.ForContext<ExportRepository>();
@ -41,10 +39,13 @@ public class ExportRepository : IExportRepository
string[] weeks = Get();
foreach (string weekYear in weeks)
{
directory = Path.Combine(_AppSettings.ApiExportPath, weekYear, $"-{headerCommon.PSN}", $"-{headerCommon.Reactor}", $"-{headerCommon.RDS}", $"-{headerCommon.ID}");
if (headerCommon.ID < 1)
directory = Path.Combine(_AppSettings.ApiExportPath, weekYear, $"-{headerCommon.PSN}", $"-{headerCommon.Reactor}", $"-{headerCommon.RDS}");
else
directory = Path.Combine(_AppSettings.ApiExportPath, weekYear, $"-{headerCommon.PSN}", $"-{headerCommon.Reactor}", $"-{headerCommon.RDS}", $"-{headerCommon.ID}");
if (!Directory.Exists(directory))
continue;
results.AddRange(Directory.GetFiles(directory, searchPattern, SearchOption.TopDirectoryOnly));
results.AddRange(Directory.GetFiles(directory, searchPattern, SearchOption.AllDirectories));
}
return results;
}
@ -52,98 +53,79 @@ public class ExportRepository : IExportRepository
string IExportRepository.GetExport(HeaderCommon headerCommon)
{
string result;
if (!string.IsNullOrEmpty(_MockRoot))
result = File.ReadAllText(Path.Combine(string.Concat(AppContext.BaseDirectory, _MockRoot), $"{_RepositoryName}-{nameof(IExportRepository.GetExport)}.txt"));
List<string> files = GetFiles(headerCommon, "*.txt");
if (files.Count != 1)
result = string.Empty;
else
{
List<string> files = GetFiles(headerCommon, "*.txt");
if (files.Count != 1)
result = string.Empty;
else
result = File.ReadAllText(files.First());
}
result = File.ReadAllText(files.First());
return result;
}
Result<HeaderCommon[]> IExportRepository.GetHeaders(HeaderCommon headerCommon)
{
Result<HeaderCommon[]>? result;
if (!string.IsNullOrEmpty(_MockRoot))
List<HeaderCommon> results = new();
string json;
HeaderCommon? hc;
string ticks = "Ticks";
JsonElement? jsonElement;
JsonProperty[] jsonProperties;
List<string> files = GetFiles(headerCommon, "*.json");
foreach (string file in files)
{
string json = File.ReadAllText(Path.Combine(string.Concat(AppContext.BaseDirectory, _MockRoot), $"{_RepositoryName}-{nameof(IExportRepository.GetHeaders)}.json"));
result = JsonSerializer.Deserialize<Result<HeaderCommon[]>>(json);
if (result is null)
throw new NullReferenceException(nameof(result));
json = File.ReadAllText(file);
jsonElement = JsonSerializer.Deserialize<JsonElement>(json);
if (jsonElement is null || jsonElement.Value.ValueKind != JsonValueKind.Object)
continue;
jsonProperties = (from l in jsonElement.Value.EnumerateObject() where l.Name == ticks select l).ToArray();
if (!jsonProperties.Any() || !long.TryParse(jsonProperties[0].Value.ToString(), out long ticksValue))
continue;
hc = JsonSerializer.Deserialize<HeaderCommon>(json);
if (hc is null)
continue;
hc.ID = ticksValue;
hc.Date = new(ticksValue);
results.Add(hc);
}
else
result = new()
{
List<HeaderCommon> results = new();
string json;
HeaderCommon? hc;
List<string> files = GetFiles(headerCommon, "*.json");
foreach (string file in files)
{
json = File.ReadAllText(file);
hc = JsonSerializer.Deserialize<HeaderCommon>(json);
if (hc is null)
continue;
results.Add(hc);
}
result = new()
{
Results = results.ToArray(),
TotalRows = results.Count,
};
}
Results = results.ToArray(),
TotalRows = results.Count,
};
return result;
}
Result<HeaderCommon[]> IExportRepository.GetLogistics(HeaderCommon headerCommon)
{
Result<HeaderCommon[]>? result;
if (!string.IsNullOrEmpty(_MockRoot))
List<HeaderCommon> results = new();
string json;
HeaderCommon? hc;
List<string> files = GetFiles(headerCommon, "*.json");
foreach (string file in files)
{
string json = File.ReadAllText(Path.Combine(string.Concat(AppContext.BaseDirectory, _MockRoot), $"{_RepositoryName}-{nameof(IExportRepository.GetLogistics)}.json"));
result = JsonSerializer.Deserialize<Result<HeaderCommon[]>>(json);
if (result is null)
throw new NullReferenceException(nameof(result));
json = File.ReadAllText(file);
hc = JsonSerializer.Deserialize<HeaderCommon>(json);
if (hc is null)
continue;
results.Add(hc);
}
else
result = new()
{
List<HeaderCommon> results = new();
string json;
HeaderCommon? hc;
List<string> files = GetFiles(headerCommon, "*.json");
foreach (string file in files)
{
json = File.ReadAllText(file);
hc = JsonSerializer.Deserialize<HeaderCommon>(json);
if (hc is null)
continue;
results.Add(hc);
}
result = new()
{
Results = results.ToArray(),
TotalRows = results.Count,
};
}
Results = results.ToArray(),
TotalRows = results.Count,
};
return result;
}
string IExportRepository.GetProcessDataStandardFormat(HeaderCommon headerCommon)
{
string result;
if (!string.IsNullOrEmpty(_MockRoot))
result = File.ReadAllText(Path.Combine(string.Concat(AppContext.BaseDirectory, _MockRoot), $"{_RepositoryName}-{nameof(IExportRepository.GetProcessDataStandardFormat)}.pdsf"));
List<string> files = GetFiles(headerCommon, "*.pdsf");
if (files.Count != 1)
result = string.Empty;
else
{
List<string> files = GetFiles(headerCommon, "*.pdsf");
if (files.Count != 1)
result = string.Empty;
else
result = File.ReadAllText(files.First());
}
result = File.ReadAllText(files.First());
return result;
}