public When_i_publish_current_template()
            {
                _templateId  = Guid.NewGuid().ToString();
                _author      = new Reference("222", "SecondTestUser");
                _publishNote = "Test publish note";

                SetupMocks();

                _service = new TemplateBuilderService(
                    _validatorFactory,
                    Substitute.For <IFundingTemplateValidationService>(),
                    Substitute.For <ITemplateMetadataResolver>(),
                    _versionRepository,
                    _templateRepository,
                    Substitute.For <ISearchRepository <TemplateIndex> >(),
                    Substitute.For <IPolicyRepository>(),
                    _templateBlobService,
                    Substitute.For <ILogger>());

                TemplatePublishCommand command = new TemplatePublishCommand
                {
                    Author     = _author,
                    TemplateId = _templateId,
                    Note       = _publishNote
                };

                _result = _service.PublishTemplate(command).GetAwaiter().GetResult();
            }
示例#2
0
        public async Task <CommandResult> PublishTemplate(TemplatePublishCommand command)
        {
            ValidationResult validatorResult = await _validatorFactory.Validate(command);

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

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

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

            var templateVersion = template.Current;

            if (command.Version != null)
            {
                if (command.VersionNumber == 0)
                {
                    return(CommandResult.ValidationFail(nameof(command.Version), $"Invalid version '{command.Version}'"));
                }

                templateVersion = await _templateVersionRepository.GetTemplateVersion(command.TemplateId, command.VersionNumber);

                if (templateVersion == null)
                {
                    return(CommandResult.ValidationFail(nameof(command.Version),
                                                        $"Version '{command.Version}' could not be found for template '{command.TemplateId}'"));
                }
            }

            if (templateVersion.TemplateJson.IsNullOrEmpty())
            {
                return(CommandResult.ValidationFail(nameof(templateVersion.TemplateJson),
                                                    "Template doesn't contain any template content. Please ensure the template has the correct content before publishing."));
            }

            if (templateVersion.Status == TemplateStatus.Published)
            {
                return(CommandResult.ValidationFail(nameof(templateVersion.Status), "Template version is already published"));
            }

            // create new version and save it
            var newVersion = Map(template,
                                 templateVersion,
                                 command.Author,
                                 newStatus: TemplateStatus.Published,
                                 majorVersion: template.Current.MajorVersion + 1,
                                 minorVersion: 0,
                                 publishNote: command.Note);
            var templateVersionUpdateResult = await _templateVersionRepository.SaveVersion(newVersion);

            if (!templateVersionUpdateResult.IsSuccess())
            {
                return(CommandResult.ValidationFail(nameof(command.TemplateId), $"Template version failed to save: {templateVersionUpdateResult}"));
            }

            // update template
            template.AddPredecessor(template.Current.Id);
            template.Current = newVersion;
            var templateUpdateResult = await _templateRepository.Update(template);

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

            await CreateTemplateIndexItem(template, command.Author);

            // finally add to blob so it's available to the rest of CFS
            return(await _templateBlobService.PublishTemplate(template));
        }
        public async Task <IActionResult> PublishTemplate([FromRoute] string templateId, [FromBody] TemplatePublishCommand command)
        {
            Guard.ArgumentNotNull(command, nameof(command));

            command.Author = ControllerContext.HttpContext.Request?.GetUserOrDefault();

            CommandResult response = await _templateBuilderService.PublishTemplate(command);

            if (response.Succeeded)
            {
                return(Ok(response));
            }

            if (response.ValidationResult != null)
            {
                return(response.ValidationResult.AsBadRequest());
            }

            return(new InternalServerErrorResult(response.ErrorMessage ?? response.Exception?.Message ?? "Unknown error occurred"));
        }