Exemplo n.º 1
0
        public async Task <IActionResult> OnPostAsync(string specificationId, string policyId)
        {
            Guard.IsNullOrWhiteSpace(specificationId, nameof(specificationId));

            Specification specification = await GetSpecification(specificationId);

            if (specification == null)
            {
                throw new InvalidOperationException($"Unable to retrieve specification model from the response. Specification Id value = {SpecificationId}");
            }

            IsAuthorizedToEdit = await _authorizationHelper.DoesUserHavePermission(User, specification, SpecificationActionTypes.CanEditSpecification);

            if (!IsAuthorizedToEdit)
            {
                return(new ForbidResult());
            }

            if (!ModelState.IsValid)
            {
                SpecificationName = specification.Name;

                SpecificationId = specificationId;

                return(Page());
            }

            EditPolicyModel updatePolicy = _mapper.Map <EditPolicyModel>(EditPolicyViewModel);

            updatePolicy.SpecificationId = specificationId;

            ValidatedApiResponse <Policy> updatePolicyResult = await _specsClient.UpdatePolicy(specificationId, policyId, updatePolicy);

            if (updatePolicyResult.StatusCode == HttpStatusCode.OK)
            {
                return(Redirect($"/specs/policies/{specificationId}?operationType=PolicyUpdated&operationId={updatePolicyResult.Content.Id}"));
            }
            else if (updatePolicyResult.StatusCode == HttpStatusCode.BadRequest)
            {
                updatePolicyResult.AddValidationResultErrors(ModelState);

                if (specification != null)
                {
                    SpecificationName = specification.Name;

                    FundingPeriodId = specification.FundingPeriod.Id;
                }

                return(Page());
            }
            else
            {
                return(new InternalServerErrorResult($"Unable to update policy. API returned {updatePolicyResult}"));
            }
        }
Exemplo n.º 2
0
        public Task <ValidatedApiResponse <Policy> > UpdatePolicy(string specificationId, string policyId, EditPolicyModel updatedPolicy)
        {
            Guard.ArgumentNotNull(updatedPolicy, nameof(updatedPolicy));

            return(ValidatedPutAsync <Policy, EditPolicyModel>($"policies?specificationId={specificationId}&policyId={policyId}", updatedPolicy));
        }