예제 #1
0
        public async Task <CommandResult> CreateTemplateAsClone(TemplateCreateAsCloneCommand command, Reference author)
        {
            ValidationResult validatorResult = await _validatorFactory.Validate(command);

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

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

            try
            {
                var sourceTemplate = await _templateRepository.GetTemplate(command.CloneFromTemplateId);

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

                var sourceVersion = sourceTemplate.Current;
                if (command.Version != null)
                {
                    if (!int.TryParse(command.Version, out int versionNumber))
                    {
                        return(CommandResult.ValidationFail(nameof(command.Version), $"Invalid version '{command.Version}'"));
                    }

                    sourceVersion = await _templateVersionRepository.GetTemplateVersion(command.CloneFromTemplateId, versionNumber);

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

                IEnumerable <FundingStreamWithPeriods> available = await GetFundingStreamAndPeriodsWithoutTemplates();

                var match = available.Where(x => x.FundingStream.Id == command.FundingStreamId &&
                                            x.FundingPeriods.Any(p => p.Id == command.FundingPeriodId));
                if (!match.Any())
                {
                    string validationErrorMessage =
                        $"Combination of FundingStreamId [{command.FundingStreamId}] and FundingPeriodId [{command.FundingPeriodId}] not available";
                    _logger.Error(validationErrorMessage);
                    ValidationResult validationResult = new ValidationResult();
                    validationResult.Errors.Add(new ValidationFailure(nameof(command.FundingStreamId), validationErrorMessage));
                    validationResult.Errors.Add(new ValidationFailure(nameof(command.FundingPeriodId), validationErrorMessage));
                    return(new CommandResult
                    {
                        ErrorMessage = validationErrorMessage,
                        ValidationResult = validationResult
                    });
                }

                Guid   templateId   = Guid.NewGuid();
                string templateName = $"{command.FundingStreamId} {command.FundingPeriodId}";
                FundingStreamWithPeriods streamWithPeriods = available.Single(x => x.FundingStream.Id == command.FundingStreamId);
                Template template = new Template
                {
                    TemplateId    = templateId.ToString(),
                    Name          = templateName,
                    Description   = command.Description,
                    FundingStream = streamWithPeriods.FundingStream,
                    FundingPeriod = streamWithPeriods.FundingPeriods.Single(p => p.Id == command.FundingPeriodId)
                };
                template.Current = Map(template,
                                       sourceVersion,
                                       author,
                                       majorVersion: 0,
                                       minorVersion: 1);

                // create new version and save it
                HttpStatusCode templateVersionUpdateResult = await _templateVersionRepository.SaveVersion(template.Current);

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

                HttpStatusCode result = await _templateRepository.CreateDraft(template);

                if (result.IsSuccess())
                {
                    await CreateTemplateIndexItem(template, author);

                    return(new CommandResult
                    {
                        Succeeded = true,
                        TemplateId = template.TemplateId
                    });
                }

                string errorMessage = $"Failed to create a new template with name {templateName} in Cosmos. Status code {(int) result}";
                _logger.Error(errorMessage);
                return(CommandResult.Fail(errorMessage));
            }
            catch (Exception ex)
            {
                return(new CommandResult
                {
                    Exception = ex
                });
            }
        }
예제 #2
0
        public async Task <CommandResult> CreateTemplate(TemplateCreateCommand command, Reference author)
        {
            ValidationResult validatorResult = await _validatorFactory.Validate(command);

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

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

            try
            {
                IEnumerable <FundingStreamWithPeriods> available = await GetFundingStreamAndPeriodsWithoutTemplates();

                var match = available.Where(x => x.FundingStream.Id == command.FundingStreamId &&
                                            x.FundingPeriods.Any(p => p.Id == command.FundingPeriodId));
                if (!match.Any())
                {
                    string validationErrorMessage =
                        $"Combination of FundingStreamId [{command.FundingStreamId}] and FundingPeriodId [{command.FundingPeriodId}] not available";
                    _logger.Error(validationErrorMessage);
                    ValidationResult validationResult = new ValidationResult();
                    validationResult.Errors.Add(new ValidationFailure(nameof(command.FundingStreamId), validationErrorMessage));
                    validationResult.Errors.Add(new ValidationFailure(nameof(command.FundingPeriodId), validationErrorMessage));
                    return(new CommandResult
                    {
                        ErrorMessage = validationErrorMessage,
                        ValidationResult = validationResult
                    });
                }

                string templateName = $"{command.FundingStreamId} {command.FundingPeriodId}";
                FundingStreamWithPeriods streamWithPeriods = available.Single(x => x.FundingStream.Id == command.FundingStreamId);
                Template template = new Template
                {
                    TemplateId    = Guid.NewGuid().ToString(),
                    Name          = templateName,
                    Description   = command.Description,
                    FundingStream = streamWithPeriods.FundingStream,
                    FundingPeriod = streamWithPeriods.FundingPeriods.Single(p => p.Id == command.FundingPeriodId)
                };
                template.Current = new TemplateVersion
                {
                    TemplateId      = template.TemplateId,
                    Name            = templateName,
                    FundingStreamId = command.FundingStreamId,
                    FundingPeriodId = command.FundingPeriodId,
                    Version         = 1,
                    MajorVersion    = 0,
                    MinorVersion    = 1,
                    PublishStatus   = PublishStatus.Draft,
                    SchemaVersion   = command.SchemaVersion,
                    Author          = author,
                    Date            = DateTimeOffset.Now.ToLocalTime()
                };

                HttpStatusCode result = await _templateRepository.CreateDraft(template);

                if (result.IsSuccess())
                {
                    await _templateVersionRepository.SaveVersion(template.Current);
                    await CreateTemplateIndexItem(template, author);

                    return(new CommandResult
                    {
                        Succeeded = true,
                        TemplateId = template.TemplateId
                    });
                }

                string errorMessage = $"Failed to create a new template with name {templateName} in Cosmos. Status code {(int) result}";
                _logger.Error(errorMessage);

                return(new CommandResult
                {
                    ErrorMessage = errorMessage
                });
            }
            catch (Exception ex)
            {
                return(new CommandResult
                {
                    Exception = ex
                });
            }
        }