public async Task <IActionResult> CreateRelationship(HttpRequest request)
        {
            string json = await request.GetRawBodyStringAsync();

            CreateDefinitionSpecificationRelationshipModel model = JsonConvert.DeserializeObject <CreateDefinitionSpecificationRelationshipModel>(json);

            if (model == null)
            {
                _logger.Error("Null CreateDefinitionSpecificationRelationshipModel was provided to CreateRelationship");
                return(new BadRequestObjectResult("Null CreateDefinitionSpecificationRelationshipModel was provided"));
            }

            BadRequestObjectResult validationResult = (await _relationshipModelValidator.ValidateAsync(model)).PopulateModelState();

            if (validationResult != null)
            {
                return(validationResult);
            }

            DatasetDefinition definition = await _datasetRepository.GetDatasetDefinition(model.DatasetDefinitionId);

            if (definition == null)
            {
                _logger.Error($"Datset definition was not found for id {model.DatasetDefinitionId}");
                return(new StatusCodeResult(412));
            }

            SpecificationSummary specification = await _specificationsRepository.GetSpecificationSummaryById(model.SpecificationId);

            if (specification == null)
            {
                _logger.Error($"Specification was not found for id {model.SpecificationId}");
                return(new StatusCodeResult(412));
            }

            string relationshipId = Guid.NewGuid().ToString();

            DefinitionSpecificationRelationship relationship = new DefinitionSpecificationRelationship
            {
                Name = model.Name,
                DatasetDefinition = new Reference(definition.Id, definition.Name),
                Specification     = new Reference(specification.Id, specification.Name),
                Description       = model.Description,
                Id = relationshipId,
                IsSetAsProviderData    = model.IsSetAsProviderData,
                UsedInDataAggregations = model.UsedInDataAggregations
            };

            HttpStatusCode statusCode = await _datasetRepository.SaveDefinitionSpecificationRelationship(relationship);

            if (!statusCode.IsSuccess())
            {
                _logger.Error($"Failed to save relationship with status code: {statusCode.ToString()}");
                return(new StatusCodeResult((int)statusCode));
            }

            IDictionary <string, string> properties = request.BuildMessageProperties();

            await _messengerService.SendToQueue(ServiceBusConstants.QueueNames.AddDefinitionRelationshipToSpecification,
                                                new AssignDefinitionRelationshipMessage
            {
                SpecificationId = specification.Id,
                RelationshipId  = relationshipId
            },
                                                properties);


            DatasetRelationshipSummary relationshipSummary = new DatasetRelationshipSummary
            {
                Name                = relationship.Name,
                Id                  = Guid.NewGuid().ToString(),
                Relationship        = new Reference(relationship.Id, relationship.Name),
                DatasetDefinition   = definition,
                DatasetDefinitionId = definition.Id,
                DataGranularity     = relationship.UsedInDataAggregations ? DataGranularity.MultipleRowsPerProvider : DataGranularity.SingleRowPerProvider,
                DefinesScope        = relationship.IsSetAsProviderData
            };

            BuildProject buildProject = await _calcsRepository.UpdateBuildProjectRelationships(specification.Id, relationshipSummary);

            await _cacheProvider.RemoveAsync <IEnumerable <DatasetSchemaRelationshipModel> >($"{CacheKeys.DatasetRelationshipFieldsForSpecification}{specification.Id}");

            return(new OkObjectResult(relationship));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> CreateRelationship(CreateDefinitionSpecificationRelationshipModel model, Reference author, string correlationId)
        {
            if (model == null)
            {
                _logger.Error("Null CreateDefinitionSpecificationRelationshipModel was provided to CreateRelationship");
                return(new BadRequestObjectResult("Null CreateDefinitionSpecificationRelationshipModel was provided"));
            }

            BadRequestObjectResult validationResult = (await _relationshipModelValidator.ValidateAsync(model)).PopulateModelState();

            if (validationResult != null)
            {
                return(validationResult);
            }

            DatasetDefinition definition = await _datasetRepository.GetDatasetDefinition(model.DatasetDefinitionId);

            if (definition == null)
            {
                _logger.Error($"Datset definition was not found for id {model.DatasetDefinitionId}");
                return(new StatusCodeResult(412));
            }

            ApiResponse <SpecModel.SpecificationSummary> specificationApiResponse =
                await _specificationsApiClientPolicy.ExecuteAsync(() => _specificationsApiClient.GetSpecificationSummaryById(model.SpecificationId));

            if (!specificationApiResponse.StatusCode.IsSuccess() || specificationApiResponse.Content == null)
            {
                _logger.Error($"Specification was not found for id {model.SpecificationId}");
                return(new StatusCodeResult(412));
            }

            SpecModel.SpecificationSummary specification = specificationApiResponse.Content;

            string relationshipId = Guid.NewGuid().ToString();

            DefinitionSpecificationRelationship relationship = new DefinitionSpecificationRelationship
            {
                Name = model.Name,
                DatasetDefinition = new Reference(definition.Id, definition.Name),
                Specification     = new Reference(specification.Id, specification.Name),
                Description       = model.Description,
                Id = relationshipId,
                IsSetAsProviderData    = model.IsSetAsProviderData,
                UsedInDataAggregations = model.UsedInDataAggregations,
                Author      = author,
                LastUpdated = _dateTimeProvider.UtcNow,
            };

            HttpStatusCode statusCode = await _datasetRepository.SaveDefinitionSpecificationRelationship(relationship);

            if (!statusCode.IsSuccess())
            {
                _logger.Error($"Failed to save relationship with status code: {statusCode.ToString()}");
                return(new StatusCodeResult((int)statusCode));
            }

            IDictionary <string, string> properties = MessageExtensions.BuildMessageProperties(correlationId, author);

            await _messengerService.SendToQueue(ServiceBusConstants.QueueNames.AddDefinitionRelationshipToSpecification,
                                                new AssignDefinitionRelationshipMessage
            {
                SpecificationId = specification.Id,
                RelationshipId  = relationshipId
            },
                                                properties);


            DatasetRelationshipSummary relationshipSummary = new DatasetRelationshipSummary
            {
                Name                = relationship.Name,
                Id                  = Guid.NewGuid().ToString(),
                Relationship        = new Reference(relationship.Id, relationship.Name),
                DatasetDefinition   = definition,
                DatasetDefinitionId = definition.Id,
                DataGranularity     = relationship.UsedInDataAggregations ? DataGranularity.MultipleRowsPerProvider : DataGranularity.SingleRowPerProvider,
                DefinesScope        = relationship.IsSetAsProviderData
            };

            await _calcsRepository.UpdateBuildProjectRelationships(specification.Id, relationshipSummary);

            await _cacheProvider.RemoveAsync <IEnumerable <DatasetSchemaRelationshipModel> >($"{CacheKeys.DatasetRelationshipFieldsForSpecification}{specification.Id}");

            return(new OkObjectResult(relationship));
        }