public Task <IActionResult> CreateDefinitionSpecificationRelationship([FromBody] CreateDefinitionSpecificationRelationshipModel createDefinitionSpecificationRelationshipModel) { Reference user = ControllerContext.HttpContext.Request.GetUserOrDefault(); string correlationId = ControllerContext.HttpContext.Request.GetCorrelationId(); return(_definitionSpecificationRelationshipService.CreateRelationship(createDefinitionSpecificationRelationshipModel, user, correlationId)); }
public async Task <ApiResponse <DefinitionSpecificationRelationship> > CreateRelationship( CreateDefinitionSpecificationRelationshipModel createDefinitionSpecificationRelationshipModel) { Guard.ArgumentNotNull(createDefinitionSpecificationRelationshipModel, nameof(createDefinitionSpecificationRelationshipModel)); return(await PostAsync <DefinitionSpecificationRelationship, CreateDefinitionSpecificationRelationshipModel>( DataSetsUriFor("create-definitionspecification-relationship"), createDefinitionSpecificationRelationshipModel)); }
public void Validate_GivenValidModel_ReturnsTrue() { //Arrange CreateDefinitionSpecificationRelationshipModel model = CreateModel(); CreateDefinitionSpecificationRelationshipModelValidator validator = CreateValidator(); //Act ValidationResult result = validator.Validate(model); //Assert result .IsValid .Should() .BeTrue(); }
public void Validate_GivenMissingDescription_ReturnsFalse() { //Arrange CreateDefinitionSpecificationRelationshipModel model = CreateModel(); model.Description = string.Empty; CreateDefinitionSpecificationRelationshipModelValidator validator = CreateValidator(); //Act ValidationResult result = validator.Validate(model); //Assert result .IsValid .Should() .BeFalse(); result .Errors .Count .Should() .Be(1); }
public void Validate_GivenNameAlreadyExistsn_ReturnsFalse() { //Arrange CreateDefinitionSpecificationRelationshipModel model = CreateModel(); IDatasetRepository repository = CreateDatasetRepository(false); CreateDefinitionSpecificationRelationshipModelValidator validator = CreateValidator(repository); //Act ValidationResult result = validator.Validate(model); //Assert result .IsValid .Should() .BeFalse(); result .Errors .Count .Should() .Be(1); }
public async Task <IActionResult> AssignDatasetSchema([FromBody] AssignDatasetSchemaUpdateViewModel viewModel, [FromRoute] string specificationId) { Guard.ArgumentNotNull(viewModel.Name, nameof(viewModel.Name)); Guard.ArgumentNotNull(viewModel.DatasetDefinitionId, nameof(viewModel.DatasetDefinitionId)); Guard.ArgumentNotNull(viewModel.Description, nameof(viewModel.Description)); ApiResponse <SpecificationSummary> specificationResponse = await _specificationsApiClient.GetSpecificationSummaryById(specificationId); if (specificationResponse == null || specificationResponse.StatusCode == HttpStatusCode.NotFound) { return(new NotFoundObjectResult( $"Unable to get specification response. Specification Id value = {specificationId}")); } if (specificationResponse.StatusCode == HttpStatusCode.OK && specificationResponse.Content == null) { throw new InvalidOperationException( $"Unable to retrieve specification model from the response. Specification Id value = {specificationId}"); } bool isAuthorizedToEdit = await _authorizationHelper.DoesUserHavePermission(User, specificationResponse.Content.GetSpecificationId(), SpecificationActionTypes.CanEditSpecification); if (!isAuthorizedToEdit) { return(new ForbidResult()); } if (!string.IsNullOrWhiteSpace(viewModel.Name)) { ApiResponse <DefinitionSpecificationRelationship> existingRelationshipResponse = await _datasetApiClient.GetRelationshipBySpecificationIdAndName(specificationId, viewModel.Name); if (existingRelationshipResponse.StatusCode != HttpStatusCode.NotFound) { this.ModelState.AddModelError( $"{nameof(AssignDatasetSchemaViewModel)}.{nameof(AssignDatasetSchemaViewModel.Name)}", ValidationMessages.RelationshipNameAlreadyExists); } } if (!ModelState.IsValid) { if (specificationResponse.StatusCode == HttpStatusCode.OK) { SpecificationSummary specContent = specificationResponse.Content; ApiResponse <IEnumerable <DatasetDefinition> > datasetResponse = await _datasetApiClient.GetDatasetDefinitions(); if (datasetResponse == null || datasetResponse.StatusCode == HttpStatusCode.NotFound) { return(new NotFoundObjectResult(ErrorMessages.DatasetDefinitionNotFoundInDatasetService)); } if (datasetResponse.StatusCode == HttpStatusCode.OK) { IEnumerable <DatasetDefinition> datasetDefinitionList = datasetResponse.Content; if (datasetDefinitionList == null) { throw new InvalidOperationException( $"Unable to retrieve Dataset definition from the response. Specification Id value = {specificationId}"); } } else { return(new StatusCodeResult(500)); } } } CreateDefinitionSpecificationRelationshipModel datasetSchema = new CreateDefinitionSpecificationRelationshipModel { Name = viewModel.Name, SpecificationId = specificationId, DatasetDefinitionId = viewModel.DatasetDefinitionId, Description = viewModel.Description, IsSetAsProviderData = viewModel.IsSetAsProviderData, UsedInDataAggregations = false }; ApiResponse <DefinitionSpecificationRelationship> newAssignDatasetResponse = await _datasetApiClient.CreateRelationship(datasetSchema); if (newAssignDatasetResponse?.StatusCode == HttpStatusCode.OK) { return(new OkObjectResult(true)); } return(new StatusCodeResult(500)); }
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)); }
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)); }