public async Task <ActionResult> EditProperty(AddNEditPropertyMetadataModel model) { if (!_permissionService.IsAllowed(new ActionRequestInfo(HttpContext, _implementations, null, ActionTypeEnum.ManageMetadata))) { return(Unauthorized()); } //TODO: Handle errors, validate model var property = await _dbContext.Properties.FindAsync(model.Id); property.DataTypeId = (DataTypeEnum?)model.DataTypeId; property.DataTypeEntityId = model.DataTypeEntityId; property.IsNullable = model.IsNullable; property.Name = model.Name; property.Title = model.Title; property.GeneralUsageCategoryId = model.PropertyGeneralUsageCategoryId; if (property.DataTypeId == DataTypeEnum.NavigationEntity) { await HandleForeignKey(model, property); await HandleInvserseProperty(model, property); } else { property.ForeignKeyPropertyId = model.NewForeignKeyId; property.InversePropertyId = model.NewInversePropertyId; } await _dbContext.SaveChangesAsync(); return(Ok()); }
private async Task HandleInvserseProperty(AddNEditPropertyMetadataModel model, Property property) { switch (model.InversePropertyAction) { case RelatedPropertyAction.DontChange: return; case RelatedPropertyAction.ChooseExistingById: property.InversePropertyId = model.NewInversePropertyId; return; case RelatedPropertyAction.RenameExisting: var existingInverseProperty = await _dbContext.Properties.FindAsync(model.InversePropertyId); existingInverseProperty.Name = model.NewForeignKeyName; existingInverseProperty.Title = model.NewInversePropertyTitle; return; case RelatedPropertyAction.CreateNewByName: var newInverseProperty = new Property { DataTypeId = DataTypeEnum.NavigationList, DataTypeEntityId = model.OwnerEntityId, EntityId = property.DataTypeEntityId.Value, GeneralUsageCategoryId = (await _dbContext.PropertyGeneralUsageCategories.FirstAsync(x => x.Name.Contains("NavigationList"))).Id, Name = model.NewInversePropertyName, Title = model.NewInversePropertyTitle }; newInverseProperty.InverseProperty = property; await _dbContext.Properties.AddAsync(newInverseProperty); return; default: throw new NotImplementedException(); } }
private async Task HandleForeignKey(AddNEditPropertyMetadataModel model, Property property) { switch (model.ForeignKeyAction) { case RelatedPropertyAction.DontChange: return; case RelatedPropertyAction.ChooseExistingById: property.ForeignKeyPropertyId = model.NewForeignKeyId; return; case RelatedPropertyAction.RenameExisting: var existingForeignKey = await _dbContext.Properties.FindAsync(model.ForeignKeyPropertyId); existingForeignKey.Name = model.NewForeignKeyName; return; case RelatedPropertyAction.CreateNewByName: var newForeignKey = new Property { DataTypeId = DataTypeEnum.ForeignKey, DataTypeEntityId = model.DataTypeEntityId, EntityId = property.EntityId, GeneralUsageCategoryId = (await _dbContext.PropertyGeneralUsageCategories.FirstAsync(x => x.Name.Contains("ForeignKey"))).Id, IsNullable = model.IsNullable, Name = model.NewForeignKeyName, Title = model.Title }; property.ForeignKeyProperty = newForeignKey; await _dbContext.Properties.AddAsync(newForeignKey); return; default: throw new NotImplementedException(); } }
public async Task <ActionResult> AddProperty(AddNEditPropertyMetadataModel model) { if (!_permissionService.IsAllowed(new ActionRequestInfo(HttpContext, _implementations, null, ActionTypeEnum.ManageMetadata))) { return(Unauthorized()); } using (var transaction = await _dbContext.Database.BeginTransactionAsync()) { //TODO: Handle errors, validate model var property = new Property { DataTypeId = (DataTypeEnum?)model.DataTypeId, DataTypeEntityId = model.DataTypeEntityId, IsNullable = model.IsNullable, Name = model.Name, Title = model.Title, GeneralUsageCategoryId = model.PropertyGeneralUsageCategoryId, EntityId = model.OwnerEntityId, InversePropertyId = model.InversePropertyId }; if (property.DataTypeId == DataTypeEnum.NavigationEntity) { await HandleForeignKey(model, property); await HandleInvserseProperty(model, property); } else { property.ForeignKeyPropertyId = model.NewForeignKeyId; property.InversePropertyId = model.NewInversePropertyId; } _dbContext.Properties.Add(property); await _dbContext.SaveChangesAsync(); transaction.Commit(); } return(Ok()); }