Esempio n. 1
0
        public async Task <IActionResult> AssignDatasourceVersionToRelationship([FromRoute] string relationshipId, [FromRoute] string specificationId,
                                                                                [FromRoute] string datasetVersionId)
        {
            Guard.IsNullOrWhiteSpace(relationshipId, nameof(relationshipId));
            Guard.IsNullOrWhiteSpace(specificationId, nameof(specificationId));
            Guard.IsNullOrWhiteSpace(datasetVersionId, nameof(datasetVersionId));

            ApiResponse <SelectDatasourceModel> sourcesResponse =
                await _datasetApiClient.GetDataSourcesByRelationshipId(relationshipId);

            if (sourcesResponse.StatusCode != HttpStatusCode.OK || sourcesResponse.Content == null)
            {
                _logger.Error($"Failed to fetch data sources with status code {sourcesResponse.StatusCode.ToString()}");
                return(NotFound());
            }

            bool isAuthorizedToMap = await _authorizationHelper.DoesUserHavePermission(User,
                                                                                       sourcesResponse.Content.SpecificationId, SpecificationActionTypes.CanMapDatasets);

            if (!isAuthorizedToMap)
            {
                return(new ForbidResult());
            }

            if (string.IsNullOrWhiteSpace(datasetVersionId))
            {
                SelectDataSourceViewModel viewModel = PopulateViewModel(sourcesResponse.Content);
                this.ModelState.AddModelError($"{nameof(SelectDataSourceViewModel)}.{nameof(datasetVersionId)}", "");
                if (viewModel == null)
                {
                    return(new StatusCodeResult(500));
                }
            }

            string[] datasetVersionArray = datasetVersionId.Split("_");
            if (datasetVersionArray.Length != 2)
            {
                _logger.Error($"Dataset version: {datasetVersionId} is invalid");
                return(new StatusCodeResult(500));
            }

            AssignDatasourceModel assignDatasetVersion = new AssignDatasourceModel
            {
                RelationshipId = relationshipId,
                DatasetId      = datasetVersionArray[0],
                Version        = Convert.ToInt32(datasetVersionArray[1])
            };

            HttpStatusCode httpStatusCode =
                await _datasetApiClient.AssignDatasourceVersionToRelationship(assignDatasetVersion);

            if (httpStatusCode == HttpStatusCode.OK || httpStatusCode == HttpStatusCode.NoContent)
            {
                return(new OkObjectResult(true));
            }

            _logger.Error($"Failed to assign dataset version with status code: {httpStatusCode.ToString()}");

            return(new StatusCodeResult(500));
        }