public async Task <IActionResult> UpdateDatasetElement(long id, [FromBody] DatasetElementForUpdateDto datasetElementForUpdate) { if (datasetElementForUpdate == null) { ModelState.AddModelError("Message", "Unable to locate payload for new request"); return(BadRequest(ModelState)); } if (Regex.Matches(datasetElementForUpdate.ElementName, @"[a-zA-Z() ']").Count < datasetElementForUpdate.ElementName.Length) { ModelState.AddModelError("Message", "Element contains invalid characters (Enter A-Z, a-z, open and Close brackets)"); return(BadRequest(ModelState)); } if (!String.IsNullOrWhiteSpace(datasetElementForUpdate.OID)) { if (Regex.Matches(datasetElementForUpdate.OID, @"[-a-zA-Z0-9 ']").Count < datasetElementForUpdate.OID.Length) { ModelState.AddModelError("Message", "OID contains invalid characters (Enter A-Z, a-z, 0-9, hyphen)"); return(BadRequest(ModelState)); } } if (!String.IsNullOrWhiteSpace(datasetElementForUpdate.DefaultValue)) { if (Regex.Matches(datasetElementForUpdate.DefaultValue, @"[-a-zA-Z0-9 ']").Count < datasetElementForUpdate.DefaultValue.Length) { ModelState.AddModelError("Message", "Default value contains invalid characters (Enter A-Z, a-z, 0-9, hyphen)"); return(BadRequest(ModelState)); } } if (_unitOfWork.Repository <DatasetElement>().Queryable(). Where(l => l.ElementName == datasetElementForUpdate.ElementName && l.Id != id) .Count() > 0) { ModelState.AddModelError("Message", "Item with same name already exists"); return(BadRequest(ModelState)); } var fieldType = await _fieldTypeRepository.GetAsync(ft => ft.Description == datasetElementForUpdate.FieldTypeName.ToString()); if (fieldType == null) { ModelState.AddModelError("Message", "Unable to locate field type"); return(BadRequest(ModelState)); } var datasetElementFromRepo = await _datasetElementRepository.GetAsync(f => f.Id == id, new string[] { "Field.FieldType" }); if (datasetElementFromRepo == null) { return(NotFound()); } if (ModelState.IsValid) { datasetElementFromRepo.ElementName = datasetElementForUpdate.ElementName; datasetElementFromRepo.Oid = datasetElementForUpdate.OID; datasetElementFromRepo.DefaultValue = datasetElementForUpdate.DefaultValue; datasetElementFromRepo.System = (datasetElementForUpdate.System == Models.ValueTypes.YesNoValueType.Yes); datasetElementFromRepo.Field.Mandatory = (datasetElementForUpdate.Mandatory == Models.ValueTypes.YesNoValueType.Yes); datasetElementFromRepo.Field.Anonymise = (datasetElementForUpdate.Anonymise == Models.ValueTypes.YesNoValueType.Yes); datasetElementFromRepo.Field.MaxLength = datasetElementForUpdate.FieldTypeName == FieldTypes.AlphaNumericTextbox ? datasetElementForUpdate.MaxLength : (short?)null; datasetElementFromRepo.Field.Decimals = datasetElementForUpdate.FieldTypeName == FieldTypes.NumericTextbox ? datasetElementForUpdate.Decimals : (short?)null; datasetElementFromRepo.Field.MinSize = datasetElementForUpdate.FieldTypeName == FieldTypes.NumericTextbox ? datasetElementForUpdate.MinSize : (decimal?)null; datasetElementFromRepo.Field.MaxSize = datasetElementForUpdate.FieldTypeName == FieldTypes.NumericTextbox ? datasetElementForUpdate.MaxSize : (decimal?)null; var rule = datasetElementFromRepo.GetRule(DatasetRuleType.ElementCanoOnlyLinkToSingleDataset); rule.RuleActive = datasetElementForUpdate.SingleDatasetRule == Models.ValueTypes.YesNoValueType.Yes; _datasetElementRepository.Update(datasetElementFromRepo); await _unitOfWork.CompleteAsync(); } return(Ok()); }
public async Task <IActionResult> CreateDatasetElement( [FromBody] DatasetElementForUpdateDto datasetElementForUpdate) { if (datasetElementForUpdate == null) { ModelState.AddModelError("Message", "Unable to load payload for new request"); } if (Regex.Matches(datasetElementForUpdate.ElementName, @"[a-zA-Z() ']").Count < datasetElementForUpdate.ElementName.Length) { ModelState.AddModelError("Message", "Element contains invalid characters (Enter A-Z, a-z, open and Close brackets)"); } if (Regex.Matches(datasetElementForUpdate.OID, @"[-a-zA-Z0-9 ']").Count < datasetElementForUpdate.OID.Length) { ModelState.AddModelError("Message", "OID contains invalid characters (Enter A-Z, a-z, 0-9, hyphen)"); } if (Regex.Matches(datasetElementForUpdate.DefaultValue, @"[-a-zA-Z0-9 ']").Count < datasetElementForUpdate.DefaultValue.Length) { ModelState.AddModelError("Message", "Default value contains invalid characters (Enter A-Z, a-z, 0-9, hyphen)"); } if (_unitOfWork.Repository <DatasetElement>().Queryable(). Where(l => l.ElementName == datasetElementForUpdate.ElementName) .Count() > 0) { ModelState.AddModelError("Message", "Item with same name already exists"); } var fieldType = await _fieldTypeRepository.GetAsync(ft => ft.Description == datasetElementForUpdate.FieldTypeName.ToString()); if (fieldType == null) { ModelState.AddModelError("Message", "Unable to locate field type"); } var elementType = await _datasetElementTypeRepository.GetAsync(ft => ft.Description == "Generic"); if (elementType == null) { ModelState.AddModelError("Message", "Unable to locate element type"); } long id = 0; if (ModelState.IsValid) { var newDatasetElement = new DatasetElement() { DatasetElementType = elementType, ElementName = datasetElementForUpdate.ElementName, Oid = datasetElementForUpdate.OID, DefaultValue = datasetElementForUpdate.DefaultValue, Field = new Field() { Anonymise = (datasetElementForUpdate.Anonymise == Models.ValueTypes.YesNoValueType.Yes), Mandatory = (datasetElementForUpdate.Mandatory == Models.ValueTypes.YesNoValueType.Yes), FieldType = fieldType, MaxLength = datasetElementForUpdate.FieldTypeName == FieldTypes.AlphaNumericTextbox ? datasetElementForUpdate.MaxLength : (short?)null, Decimals = datasetElementForUpdate.FieldTypeName == FieldTypes.NumericTextbox ? datasetElementForUpdate.Decimals : (short?)null, MinSize = datasetElementForUpdate.FieldTypeName == FieldTypes.NumericTextbox ? datasetElementForUpdate.MinSize : (decimal?)null, MaxSize = datasetElementForUpdate.FieldTypeName == FieldTypes.NumericTextbox ? datasetElementForUpdate.MaxSize : (decimal?)null }, System = (datasetElementForUpdate.System == Models.ValueTypes.YesNoValueType.Yes) }; var rule = newDatasetElement.GetRule(DatasetRuleType.ElementCanoOnlyLinkToSingleDataset); rule.RuleActive = (datasetElementForUpdate.SingleDatasetRule == Models.ValueTypes.YesNoValueType.Yes); _datasetElementRepository.Save(newDatasetElement); id = newDatasetElement.Id; var mappedDatasetElement = await GetDatasetElementAsync <DatasetElementIdentifierDto>(id); if (mappedDatasetElement == null) { return(StatusCode(500, "Unable to locate newly added item")); } return(CreatedAtAction("GetDatasetElementByIdentifier", new { id = mappedDatasetElement.Id }, CreateLinksForDatasetElement <DatasetElementIdentifierDto>(mappedDatasetElement))); } return(BadRequest(ModelState)); }