Separated Wafer-Counter

JsonElement instead of Request body
Attachment Class
Bump
Ready to test GetLastGroupIdWithValue
Changed to v4
This commit is contained in:
2024-05-21 12:40:20 -07:00
parent 5c9f0d1aff
commit 6317c385f6
38 changed files with 1231 additions and 476 deletions

View File

@ -31,76 +31,42 @@ public class InboundRepository : IInboundRepository
return false;
}
// this is the main endpoint, it accepts a JSON message that contains both the header and data records together
// tooltype is the ToolTypeName column from the ToolType table
// JToken is how you can accept a JSON message without deserialization.
// Using "string" doesn't work because ASP.NET Core will expect a json encoded string, not give you the actual string.
DataResponse IInboundRepository.Data(IMetrologyRepository metrologyRepository, IInboundDataService inboundDataService, string tooltype, string? json)
DataResponse IInboundRepository.Data(IMetrologyRepository metrologyRepository, IInboundDataService inboundDataService, string toolTypeName, JsonElement? jsonElement)
{
DataResponse result = new();
ToolType? toolType = metrologyRepository.GetToolTypeByName(tooltype);
if (toolType is null)
result.Errors.Add($"Invalid tool type: {tooltype}");
else
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)
{
InboundCommon? inboundCommon = string.IsNullOrEmpty(json) ? null : JsonSerializer.Deserialize<InboundCommon>(json);
if (inboundCommon is null || string.IsNullOrEmpty(inboundCommon.ProcessDataStandardFormat))
result.Errors.Add($"Invalid body: {json}");
else
try
{
string? sourceDirectory = Path.GetDirectoryName(inboundCommon.ProcessDataStandardFormat);
string? parentDirectory = Path.GetDirectoryName(sourceDirectory);
if (string.IsNullOrEmpty(sourceDirectory) || string.IsNullOrEmpty(parentDirectory) || !Directory.Exists(parentDirectory))
result.Errors.Add($"Invalid body:path: <{inboundCommon.ProcessDataStandardFormat}>");
else
{
JToken jToken = string.IsNullOrEmpty(json) ? JToken.Parse("{}") : JToken.Parse(json);
if (jToken is null)
result.Errors.Add($"Invalid body: {json}");
else
{
List<ToolTypeMetadata> metaData = metrologyRepository.GetToolTypeMetadataByToolTypeID(toolType.ID).ToList();
if (metaData is null)
result.Errors.Add($"Invalid metadata for tool type: {tooltype}");
else
{
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;
string? destinationDirectory = Path.Combine(parentDirectory, result.HeaderID.ToString());
Directory.Move(sourceDirectory, destinationDirectory);
}
catch (Exception ex) { result.Errors.Add(ex.Message); }
}
}
}
}
result.HeaderID = inboundDataService.DoSQLInsert(jToken, toolType, metaData);
result.Success = result.HeaderID > 0;
}
catch (Exception ex) { result.Errors.Add(ex.Message); }
}
return result;
}
// this is the endpoint for attaching a field. It is not JSON, it is form-data/multipart like an HTML form because that's the normal way.
// header ID is the ID value from the Header table
// datauniqueid is the Title value from the Data/Detail table
string? IInboundRepository.AttachFile(IMetrologyRepository metrologyRepository, IAttachmentsService attachmentsService, string tooltype, long headerid, string datauniqueid, string fileName, object uploadedFile)
void IInboundRepository.AttachFile(IMetrologyRepository metrologyRepository, IAttachmentsService attachmentsService, string toolTypeName, Attachment? attachment)
{
string? result = null;
ToolType toolType = metrologyRepository.GetToolTypeByName(tooltype);
if (toolType is null)
result = $"Invalid tool type: {tooltype}";
string filename = Path.GetFileName(fileName);
if (string.IsNullOrWhiteSpace(filename))
result = "Empty filename";
if (filename.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0)
result = "Invalid filename";
if (result is null && toolType is not null)
attachmentsService.SaveAttachment(toolType, headerid, datauniqueid, filename, uploadedFile);
return result;
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);
}
}