Пример #1
0
        private async Task <CommandResult> UpdateOrRestoreTemplateContent(TemplateFundingLinesUpdateCommand originalCommand, Reference author,
                                                                          bool restore = false)
        {
            ValidationResult validatorResult = await _validatorFactory.Validate(originalCommand);

            validatorResult.Errors.AddRange((await _validatorFactory.Validate(author))?.Errors);

            if (!validatorResult.IsValid)
            {
                return(CommandResult.ValidationFail(validatorResult));
            }

            var template = await _templateRepository.GetTemplate(originalCommand.TemplateId);

            if (template?.Current == null)
            {
                return(CommandResult.ValidationFail(nameof(originalCommand.TemplateId), "Template doesn't exist"));
            }

            (ValidationFailure error, TemplateJsonContentUpdateCommand updateCommand) = MapCommand(originalCommand);
            if (error != null)
            {
                return(CommandResult.ValidationFail(new ValidationResult(new[] { error })));
            }

            if (!restore && template.Current.TemplateJson == updateCommand.TemplateJson)
            {
                return(CommandResult.Success());
            }

            CommandResult validationError = await ValidateTemplateContent(updateCommand.TemplateJson, template.Current.FundingStreamId,
                                                                          template.Current.FundingPeriodId);

            if (validationError != null)
            {
                return(validationError);
            }

            var updated = await UpdateTemplateContent(updateCommand,
                                                      author,
                                                      template,
                                                      template.Current.MajorVersion,
                                                      template.Current.MinorVersion + 1);

            if (!updated.IsSuccess())
            {
                return(CommandResult.Fail($"Failed to update template: {updated}"));
            }

            CommandResult commandResult = CommandResult.Success();

            commandResult.Version = template.Current.Version;

            return(commandResult);
        }
            public When_i_update_template_content()
            {
                _command = new TemplateFundingLinesUpdateCommand
                {
                    TemplateId = Guid.NewGuid().ToString(),
                    TemplateFundingLinesJson = @"[{""templateLineId"":1,""type"":""Payment"",""name"":""Funding Line 1"",""fundingLineCode"":""DSG-001"",""fundingLines"":[],""calculations"":[]}]"
                };
                _author = new Reference("222", "SecondTestUser");

                SetupMocks();

                _service = new TemplateBuilderService(
                    _validatorFactory,
                    _templateValidationService,
                    _templateMetadataResolver,
                    _versionRepository,
                    _templateRepository,
                    _searchRepository,
                    _policyRepository,
                    _templateBlobService,
                    Substitute.For <ILogger>());

                _result = _service.UpdateTemplateContent(_command, _author).GetAwaiter().GetResult();
            }
        public async Task <IActionResult> RestoreContent(TemplateFundingLinesUpdateCommand command, [FromRoute] string templateId, [FromRoute] string version)
        {
            ValidationResult validationResult = await _validatorFactory.Validate(command);

            if (!validationResult.IsValid || command.TemplateId != templateId)
            {
                return(validationResult.AsBadRequest());
            }

            Reference author = ControllerContext.HttpContext.Request?.GetUserOrDefault();

            CommandResult result = await _templateBuilderService.RestoreTemplateContent(command, author);

            if (result.Succeeded)
            {
                return(Ok(result.Version));
            }
            if (result.ValidationModelState != null)
            {
                return(BadRequest(result.ValidationModelState));
            }

            return(new InternalServerErrorResult(result.ErrorMessage ?? result.Exception?.Message ?? "Unknown error occurred"));
        }
Пример #4
0
 public async Task <CommandResult> RestoreTemplateContent(TemplateFundingLinesUpdateCommand originalCommand, Reference author)
 {
     return(await UpdateOrRestoreTemplateContent(originalCommand, author, restore : true));
 }