public async Task <IActionResult> AddStructure([FromBody] DtoStructure structure) { if (structure is null) { return(BadRequest("no Structure received")); } if (string.IsNullOrWhiteSpace(structure.Name)) { return(BadRequest($"Structure.{nameof(DtoStructure.Name)} is empty")); } if (structure.Version <= 0) { return(BadRequest($"invalid version provided '{structure.Version}'")); } switch (structure.Structure.ValueKind) { case JsonValueKind.Object: if (!structure.Structure.EnumerateObject().Any()) { return(BadRequest("empty structure-body given")); } break; case JsonValueKind.Array: if (!structure.Structure.EnumerateArray().Any()) { return(BadRequest("empty structure-body given")); } break; default: return(BadRequest("invalid structure-body given (invalid type or null)")); } if (structure.Variables is null || !structure.Variables.Any()) { Logger.LogDebug($"Structure.{nameof(DtoStructure.Variables)} is null or empty, seems fishy but may be correct"); } try { var keys = _translator.ToDictionary(structure.Structure); var variables = (structure.Variables ?? new Dictionary <string, object>()).ToDictionary(kvp => kvp.Key, kvp => kvp.Value?.ToString()); var result = await _store.Structures.Create(new StructureIdentifier(structure.Name, structure.Version), keys, variables); if (result.IsError) { return(ProviderError(result)); } return(AcceptedAtAction(nameof(GetStructureKeys), RouteUtilities.ControllerName <StructureController>(), new { version = ApiVersions.V1, name = structure.Name, structureVersion = structure.Version, offset = -1, length = -1 }, keys)); } catch (Exception e) { Logger.LogError(e, $"failed to process given Structure.{nameof(DtoStructure.Structure)}"); return(StatusCode((int)HttpStatusCode.InternalServerError, $"failed to process given Structure.{nameof(DtoStructure.Structure)}")); } }
public async Task <IActionResult> InspectUploadedStructure([FromRoute] string environmentCategory, [FromRoute] string environmentName, [FromBody] DtoStructure structure) { if (string.IsNullOrWhiteSpace(environmentCategory)) { return(BadRequest("no environment-category given")); } if (string.IsNullOrWhiteSpace(environmentName)) { return(BadRequest("no environment-name given")); } if (structure is null) { return(BadRequest("no structure was uploaded")); } switch (structure.Structure.ValueKind) { case JsonValueKind.Object: if (!structure.Structure.EnumerateObject().Any()) { return(BadRequest("empty structure-body given")); } break; case JsonValueKind.Array: if (!structure.Structure.EnumerateArray().Any()) { return(BadRequest("empty structure-body given")); } break; default: return(BadRequest("invalid structure-body given (invalid type or null)")); } IDictionary <string, string> structKeys; try { structKeys = _translator.ToDictionary(structure.Structure); } catch (Exception e) { Logger.LogWarning(e, "could not translate given json.Structure to dictionary"); return(BadRequest("structure could not be mapped to a dictionary ($.Structure)")); } var envId = new EnvironmentIdentifier(environmentCategory, environmentName); var envKeysResult = await _store.Environments.GetKeys(new EnvironmentKeyQueryParameters { Environment = envId, Range = QueryRange.All }); if (envKeysResult.IsError) { return(ProviderError(envKeysResult)); } var envKeys = envKeysResult.Data; CompilationResult compilationResult; try { compilationResult = _compiler.Compile( new EnvironmentCompilationInfo { Keys = envKeys, Name = envId.ToString() }, new StructureCompilationInfo { Name = structure.Name ?? "Inspected Structure", Keys = structKeys, Variables = (structure.Variables ?? new Dictionary <string, object>()).ToDictionary(kvp => kvp.Key, kvp => kvp.Value?.ToString()) }, _parser); } catch (Exception e) { Logger.LogWarning(e, $"structure could not be inspected in context of '{envId}'; compilation failed"); return(Ok(new StructureInspectionResult { CompilationSuccessful = false })); } var result = AnalyzeCompilation(compilationResult); return(Ok(result)); }