static IDatasetRepository CreateDatasetsRepository(bool hasDataset = false, bool hasDatasetDefinitionForFundingStream = false)
        {
            IDatasetRepository repository = Substitute.For <IDatasetRepository>();

            repository
            .GetDatasetsByQuery(Arg.Any <Expression <Func <DocumentEntity <Dataset>, bool> > >())
            .Returns(hasDataset ? new[] { new Dataset() } : new Dataset[0]);

            repository
            .GetDatasetDefinitionsByFundingStreamId(Arg.Is(FundingStreamId))
            .Returns(hasDatasetDefinitionForFundingStream ?
                     new[] { new Models.Datasets.Schema.DatasetDefinationByFundingStream()
                             {
                                 Id = DefinitionId
                             } }
                : Enumerable.Empty <Models.Datasets.Schema.DatasetDefinationByFundingStream>());

            return(repository);
        }
        public CreateNewDatasetModelValidator(
            IDatasetRepository datasetsRepository,
            IPolicyRepository policyRepository)
        {
            _datasetsRepository = datasetsRepository;
            _policyRepository   = policyRepository;

            RuleFor(model => model.Description)
            .NotEmpty()
            .WithMessage("You must give a description for the dataset");

            RuleFor(model => model.Filename)
            .Custom((name, context) =>
            {
                CreateNewDatasetModel model = context.ParentContext.InstanceToValidate as CreateNewDatasetModel;
                if (string.IsNullOrWhiteSpace(model.Filename))
                {
                    context.AddFailure("You must provide a filename");
                }
                else if (!validExtensions.Contains(Path.GetExtension(model.Filename.ToLower())))
                {
                    context.AddFailure("Check you have the right file format");
                }
            });

            RuleFor(model => model.Name)
            .Custom((name, context) =>
            {
                CreateNewDatasetModel model = context.ParentContext.InstanceToValidate as CreateNewDatasetModel;
                if (string.IsNullOrWhiteSpace(model.Name))
                {
                    context.AddFailure("Use a descriptive unique name other users can understand");
                }
                else
                {
                    IEnumerable <Dataset> datasets = _datasetsRepository.GetDatasetsByQuery(m => m.Content.Name.ToLower() == model.Name.ToLower()).Result;
                    if (datasets != null && datasets.Any())
                    {
                        context.AddFailure("Use a descriptive unique name other users can understand");
                    }
                }
            });

            RuleFor(model => model.FundingStreamId)
            .Custom((name, context) =>
            {
                CreateNewDatasetModel model = context.ParentContext.InstanceToValidate as CreateNewDatasetModel;
                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}");
                    }
                }
            });

            RuleFor(model => model.DefinitionId)
            .Custom((name, context) =>
            {
                CreateNewDatasetModel model = context.ParentContext.InstanceToValidate as CreateNewDatasetModel;
                if (string.IsNullOrWhiteSpace(model.DefinitionId))
                {
                    context.AddFailure("You must provide a valid dataset schema");
                }
                else if (!string.IsNullOrWhiteSpace(model.FundingStreamId))
                {
                    IEnumerable <DatasetDefinationByFundingStream> datasetDefinitions = _datasetsRepository.GetDatasetDefinitionsByFundingStreamId(model.FundingStreamId).Result;

                    if (datasetDefinitions?.Any(d => d.Id == model.DefinitionId) == false)
                    {
                        context.AddFailure($"Dataset definition not found for funding stream ID: {model.FundingStreamId}");
                    }
                }
            });
        }
Beispiel #3
0
        public async Task <IActionResult> GetDatasetDefinitionsByFundingStreamId(string fundingStreamId)
        {
            if (string.IsNullOrWhiteSpace(fundingStreamId))
            {
                _logger.Error("No FundingStreamId was provided to GetDatasetDefinitionByFundingStreamId");

                return(new BadRequestObjectResult("Null or empty fundingStreamId provided"));
            }

            IEnumerable <DatasetDefinationByFundingStream> defintions = await _datasetsRepositoryPolicy.ExecuteAsync(() => _datasetsRepository.GetDatasetDefinitionsByFundingStreamId(fundingStreamId));

            if (defintions?.Any() == false)
            {
                return(new NotFoundResult());
            }

            return(new OkObjectResult(defintions));
        }