コード例 #1
0
        public DatasetDefinitionValidator(
            IPolicyRepository policyRepository,
            IDatasetRepository datasetRepository,
            IDatasetsResiliencePolicies datasetsResiliencePolicies)
        {
            _policyRepository         = policyRepository;
            _datasetRepository        = datasetRepository;
            _datasetsRepositoryPolicy = datasetsResiliencePolicies.DatasetRepository;
            _typeIdentifierGenerator  = new VisualBasicTypeIdentifierGenerator();

            RuleFor(model => model.FundingStreamId)
            .CustomAsync(async(name, context, ct) =>
            {
                DatasetDefinition model = context.ParentContext.InstanceToValidate as DatasetDefinition;
                if (string.IsNullOrWhiteSpace(model.FundingStreamId))
                {
                    context.AddFailure("You must give a Funding Stream Id for the dataset");
                }
                else
                {
                    IEnumerable <PoliciesApiModels.FundingStream> fundingStreams = _policyRepository.GetFundingStreams().Result;

                    if (fundingStreams != null && !fundingStreams.Any(_ => _.Id == model.FundingStreamId))
                    {
                        context.AddFailure($"Unable to find given funding stream ID: {model.FundingStreamId}");
                    }
                }

                bool datasetWithGivenNameExists =
                    await _datasetsRepositoryPolicy.ExecuteAsync(() => _datasetRepository.DatasetExistsWithGivenName(model.Name, model.Id));

                if (datasetWithGivenNameExists)
                {
                    context.AddFailure($"Given dataset name already exists in repository: {model.Name}");
                }

                IDictionary <string, string> fieldIdentifierNameMap = new Dictionary <string, string>();

                IEnumerable <FieldDefinition> fieldDefinitions = model.TableDefinitions?.SelectMany(_ => _.FieldDefinitions);

                if (fieldDefinitions != null)
                {
                    foreach (FieldDefinition fieldDefinition in fieldDefinitions)
                    {
                        string fieldDefinitionNameIdentifier = _typeIdentifierGenerator.GenerateIdentifier(fieldDefinition.Name);

                        if (fieldIdentifierNameMap.ContainsKey(fieldDefinitionNameIdentifier))
                        {
                            context.AddFailure($"Given field definition name matches another field definition name. " +
                                               $"{fieldIdentifierNameMap[fieldDefinitionNameIdentifier]} and {fieldDefinition.Name}");
                        }
                        else
                        {
                            fieldIdentifierNameMap.Add(fieldDefinitionNameIdentifier, fieldDefinition.Name);
                        }
                    }
                }
            });
        }
        static IDatasetRepository CreateDatasetRepository()
        {
            IDatasetRepository repository = Substitute.For <IDatasetRepository>();

            repository
            .DatasetExistsWithGivenName(DatasetDefinitionName, DatasetDefinitionId)
            .Returns(true);

            return(repository);
        }