public async Task <IActionResult> UpdateSampleQueryAsync(string id, [FromBody] SampleQueryModel sampleQueryModel)
        {
            try
            {
                // Get the list of policies
                SampleQueriesPolicies policies = await GetSampleQueriesPoliciesAsync();

                string categoryName      = sampleQueryModel.Category;
                string userPrincipalName = User.Identity.Name;

                // Check if authenticated user is authorized for this action
                bool isAuthorized = SamplesPolicyService.IsUserAuthorized(policies, userPrincipalName, categoryName, HttpMethods.Put);

                if (!isAuthorized)
                {
                    return(new JsonResult(
                               $"{userPrincipalName} is not authorized to update the sample query. Category: '{categoryName}'")
                    {
                        StatusCode = StatusCodes.Status401Unauthorized
                    });
                }

                // Get the list of sample queries
                SampleQueriesList sampleQueriesList = await GetSampleQueriesListAsync();

                if (sampleQueriesList.SampleQueries.Count == 0)
                {
                    return(NotFound()); // List is empty; the sample query being searched is definitely not in an empty list
                }

                // Check if the sample query model exists in the list of sample queries
                bool sampleQueryExists = sampleQueriesList.SampleQueries.Exists(x => x.Id == Guid.Parse(id));

                if (!sampleQueryExists)
                {
                    throw new InvalidOperationException($"No sample query found with id: {id}");
                }

                // Update the provided sample query model into the list of sample queries
                SampleQueriesList updatedSampleQueriesList = SamplesService.UpdateSampleQueriesList(sampleQueriesList, sampleQueryModel, Guid.Parse(id));

                // Get the serialized JSON string of this sample query
                string updatedSampleQueriesJson = SamplesService.SerializeSampleQueriesList(updatedSampleQueriesList);

                // Save the document-readable JSON-styled string to the source file
                await _fileUtility.WriteToFile(updatedSampleQueriesJson, _queriesFilePathSource);

                // Success; return the sample query model object that was just updated
                return(Ok(sampleQueryModel));
            }
            catch (InvalidOperationException invalidOpsException)
            {
                // sample query with provided id not found
                return(new JsonResult(invalidOpsException.Message)
                {
                    StatusCode = StatusCodes.Status404NotFound
                });
            }
            catch (Exception exception)
            {
                return(new JsonResult(exception.Message)
                {
                    StatusCode = StatusCodes.Status500InternalServerError
                });
            }
        }
示例#2
0
        public async Task <IActionResult> UpdateSampleQueryAsync(string id, [FromBody] SampleQueryModel sampleQueryModel)
        {
            try
            {
                // Get the list of policies
                SampleQueriesPolicies policies = await GetSampleQueriesPoliciesAsync();

                string categoryName = sampleQueryModel.Category;

                ClaimsIdentity      identity = (ClaimsIdentity)User.Identity;
                IEnumerable <Claim> claims   = identity.Claims;
                string userPrincipalName     =
                    (claims?.FirstOrDefault(x => x.Type.Equals(Constants.ClaimTypes.UpnJwt, StringComparison.OrdinalIgnoreCase)) ??
                     claims?.FirstOrDefault(x => x.Type.Equals(Constants.ClaimTypes.UpnUriSchema, StringComparison.OrdinalIgnoreCase)))?.Value;

                // Check if authenticated user is authorized for this action
                bool isAuthorized = SamplesPolicyService.IsUserAuthorized(policies, userPrincipalName, categoryName, HttpMethods.Put);

                if (!isAuthorized)
                {
                    return(new JsonResult(
                               $"{userPrincipalName} is not authorized to update the sample query. Category: '{categoryName}'")
                    {
                        StatusCode = StatusCodes.Status403Forbidden
                    });
                }

                // Get the list of sample queries
                SampleQueriesList sampleQueriesList = await _samplesStore.FetchSampleQueriesListAsync("en-US");

                if (sampleQueriesList.SampleQueries.Count == 0)
                {
                    return(NotFound()); // List is empty; the sample query being searched is definitely not in an empty list
                }

                // Check if the sample query model exists in the list of sample queries
                bool sampleQueryExists = sampleQueriesList.SampleQueries.Exists(x => x.Id == Guid.Parse(id));

                if (!sampleQueryExists)
                {
                    throw new InvalidOperationException($"No sample query found with id: {id}");
                }

                // Update the provided sample query model into the list of sample queries
                SampleQueriesList updatedSampleQueriesList = SamplesService.UpdateSampleQueriesList(sampleQueriesList, sampleQueryModel, Guid.Parse(id));

                // Get the serialized JSON string of this sample query
                string updatedSampleQueriesJson = SamplesService.SerializeSampleQueriesList(updatedSampleQueriesList);

                // Success; return the sample query model object that was just updated
                return(Ok(sampleQueryModel));
            }
            catch (InvalidOperationException invalidOpsException)
            {
                // sample query with provided id not found
                return(new JsonResult(invalidOpsException.Message)
                {
                    StatusCode = StatusCodes.Status404NotFound
                });
            }
            catch (Exception exception)
            {
                return(new JsonResult(exception.Message)
                {
                    StatusCode = StatusCodes.Status500InternalServerError
                });
            }
        }