public async Task <IActionResult> CreateAsync([FromBody] JObject jsonObject)
        {
            var requestId = Guid.NewGuid().ToString();

            _logger.LogInformation($"RequestID:{requestId} - MetaData_Create called.");

            try
            {
                if (jsonObject == null)
                {
                    _logger.LogError($"RequestID:{requestId} - MetaData_Create error: null");
                    var errorResponse = JsonErrorResponse.BadRequest($"MetaData_Create error: null", requestId);

                    return(BadRequest(errorResponse));
                }

                var modelObject = JsonConvert.DeserializeObject <MetaDataModel>(jsonObject.ToString(), new JsonSerializerSettings
                {
                    MissingMemberHandling = MissingMemberHandling.Ignore,
                    NullValueHandling     = NullValueHandling.Ignore
                });

                //TODO: P2 Refactor into Guard
                if (String.IsNullOrEmpty(modelObject.DisplayName))
                {
                    _logger.LogError($"RequestID:{requestId} - MetaData_Create error: invalid name");
                    var errorResponse = JsonErrorResponse.BadRequest($"MetaData_Create error: invalid name", requestId);

                    return(BadRequest(errorResponse));
                }

                var resultCode = await _metaDataService.CreateItemAsync(modelObject, requestId);

                if (resultCode != ApplicationCore.StatusCodes.Status201Created)
                {
                    _logger.LogError($"RequestID:{requestId} - MetaData_Create error: {resultCode.Name}");
                    var errorResponse = JsonErrorResponse.BadRequest($"MetaData_Create error: {resultCode.Name}", requestId);

                    return(BadRequest(errorResponse));
                }

                //var location = "/MetaData/Create/new"; // TODO: Get the id from the results but need to wire from factory to here

                return(Ok(resultCode));// Created(location, $"RequestId: {requestId} - MetaData created.");
            }
            catch (Exception ex)
            {
                _logger.LogError($"RequestID:{requestId} MetaData_Create error: {ex.Message}");
                var errorResponse = JsonErrorResponse.BadRequest($"MetaData_Create error: {ex} ", requestId);

                return(BadRequest(errorResponse));
            }
        }
Beispiel #2
0
        public async Task <IActionResult> CreateAsync([FromBody] JObject jsonObject)
        {
            var requestId = Guid.NewGuid().ToString();

            _logger.LogInformation($"RequestID:{requestId} - MetaData_Create called.");

            try
            {
                if (jsonObject == null)
                {
                    _logger.LogError($"RequestID:{requestId} - MetaData_Create error: null");
                    var errorResponse = JsonErrorResponse.BadRequest($"MetaData_Create error: null", requestId);

                    return(BadRequest(errorResponse));
                }

                var modelObject = JsonConvert.DeserializeObject <MetaDataModel>(jsonObject.ToString(), new JsonSerializerSettings
                {
                    MissingMemberHandling = MissingMemberHandling.Ignore,
                    NullValueHandling     = NullValueHandling.Ignore
                });

                //TODO: P2 Refactor into Guard
                if (String.IsNullOrEmpty(modelObject.DisplayName))
                {
                    _logger.LogError($"RequestID:{requestId} - MetaData_Create error: invalid name");
                    var errorResponse = JsonErrorResponse.BadRequest($"MetaData_Create error: invalid name", requestId);

                    return(BadRequest(errorResponse));
                }

                var result = await _metaDataService.CreateItemAsync(modelObject, requestId);

                return(new CreatedResult(result.SelectToken("id").ToString(), null));
            }
            catch (Exception ex)
            {
                _logger.LogError($"RequestID:{requestId} MetaData_Create error: {ex.Message}");
                var errorResponse = JsonErrorResponse.BadRequest($"MetaData_Create error: {ex} ", requestId);

                return(BadRequest(errorResponse));
            }
        }