Exemplo n.º 1
0
        public async Task <IActionResult> GetRelationshipsBySpecificationId(string specificationId)
        {
            if (string.IsNullOrWhiteSpace(specificationId))
            {
                _logger.Error("No specification id was provided to GetRelationshipsBySpecificationId");

                return(new BadRequestObjectResult("No specification id was provided to GetRelationshipsBySpecificationId"));
            }

            IEnumerable <DefinitionSpecificationRelationship> relationships =
                await _datasetRepository.GetDefinitionSpecificationRelationshipsByQuery(m => m.Content.Specification.Id == specificationId);

            if (relationships.IsNullOrEmpty())
            {
                relationships = new DefinitionSpecificationRelationship[0];
            }

            IList <DatasetSpecificationRelationshipViewModel> relationshipViewModels = new List <DatasetSpecificationRelationshipViewModel>();

            if (relationships.IsNullOrEmpty())
            {
                return(new OkObjectResult(relationshipViewModels));
            }

            IList <Task <DatasetSpecificationRelationshipViewModel> > tasks = new List <Task <DatasetSpecificationRelationshipViewModel> >();

            IEnumerable <KeyValuePair <string, int> > datasetLatestVersions =
                await _datasetRepository.GetDatasetLatestVersions(relationships.Select(_ => _.DatasetVersion?.Id));

            foreach (DefinitionSpecificationRelationship relationship in relationships)
            {
                KeyValuePair <string, int> datasetLatestVersion       = datasetLatestVersions.SingleOrDefault(_ => _.Key == relationship.DatasetVersion?.Id);
                Task <DatasetSpecificationRelationshipViewModel> task = CreateViewModel(relationship, datasetLatestVersion);

                tasks.Add(task);
            }

            await Task.WhenAll(tasks);

            foreach (Task <DatasetSpecificationRelationshipViewModel> task in tasks)
            {
                relationshipViewModels.Add(task.Result);
            }

            return(new OkObjectResult(tasks.Select(m => m.Result)));
        }