private async Task SyncBehaviors(Property property, AddNEditPropertyModel model) { var existingBehaviors = await _dbContext.PropertyBehaviors.Where(x => x.PropertyId == property.Id).ToListAsync(); if (model.PropertyBehaviors != null) { foreach (var behavior in model.PropertyBehaviors) { if (behavior.Id == default) { _dbContext.PropertyBehaviors.Add(new PropertyBehavior { AdditionalBehaviorId = behavior.AdditionalBehaviorId, PropertyId = property.Id, }); } else { var existing = existingBehaviors.FirstOrDefault(x => x.Id == behavior.Id); existing.AdditionalBehaviorId = behavior.AdditionalBehaviorId; existing.Parameters = behavior.Parameters; } } } foreach (var existing in existingBehaviors) { if (model.PropertyBehaviors == null || !model.PropertyBehaviors.Any(b => b.Id == existing.Id)) { _dbContext.PropertyBehaviors.Remove(existing); } } }
public async Task <ActionResult> AddProperty(AddNEditPropertyModel model) { if (!_permissionService.IsAllowed(new ActionRequestInfo(HttpContext, _implementations, null, ActionTypeEnum.ManageMetadata))) { return(Unauthorized()); } var validationMessage = await MetadataValidationLogic.AddPropertyValidation(model, _dbContext); if (!string.IsNullOrEmpty(validationMessage)) { return(StatusCode(400, validationMessage)); } using (var transaction = await _dbContext.Database.BeginTransactionAsync()) { // Why Transaction in here ? ( Arash ) var property = new Property { DataTypeId = (DataTypeEnum?)model.DataTypeId, DataEntityTypeId = model.DataEntityTypeId, IsNullable = model.IsNullable, Name = model.Name, Title = model.Title, GeneralUsageCategoryId = model.PropertyGeneralUsageCategoryId, OwnerEntityTypeId = model.OwnerEntityTypeId, 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); if (model.PropertyBehaviors != null) { foreach (var behavior in model.PropertyBehaviors) { _dbContext.PropertyBehaviors.Add(new PropertyBehavior { AdditionalBehaviorId = behavior.AdditionalBehaviorId, Parameters = behavior.Parameters, Property = property }); } } await ProcessPropertyLocalFacets(model, false, property); await _dbContext.SaveChangesAsync(); ((RequestLogModel)HttpContext.Items["RequestLog"]).PropertyId = property.Id; transaction.Commit(); } return(Ok()); }
public static async Task <string> EditPropertyValidation(AddNEditPropertyModel model, MetadataDbContext dbContext) { if (!await dbContext.Properties.AnyAsync(x => x.Id == model.Id)) { return("Property not found"); } return(string.Empty); }
public static async Task <string> AddPropertyValidation(AddNEditPropertyModel model, MetadataDbContext dbContext) { if (model.PropertyGeneralUsageCategoryId == 2 && await dbContext.EntityTypes.AnyAsync(x => x.Properties.Where(y => y.OwnerEntityTypeId == model.OwnerEntityTypeId).Any(y => y.GeneralUsageCategoryId == 2))) { return("This entity already has a primary key"); } if (!await dbContext.EntityTypes.AnyAsync(x => x.Id == model.OwnerEntityTypeId)) { return("Entity not found"); } return(string.Empty); }
private async Task HandleInvserseProperty(AddNEditPropertyModel model, Property property) { switch (model.InversePropertyAction) { case RelatedPropertyAction.DontChange: return; case RelatedPropertyAction.ChooseExistingById: var inverseProperty = await _dbContext.Properties.FindAsync(model.NewInversePropertyId); inverseProperty.InverseProperty = property; 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, DataEntityTypeId = model.OwnerEntityTypeId, OwnerEntityTypeId = property.DataEntityTypeId.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(); } }
public async Task <ActionResult> EditProperty(AddNEditPropertyModel model) { if (!_permissionService.IsAllowed(new ActionRequestInfo(HttpContext, _implementations, null, ActionTypeEnum.ManageMetadata))) { return(Unauthorized()); } var validationMessage = await MetadataValidationLogic.EditPropertyValidation(model, _dbContext); if (!string.IsNullOrEmpty(validationMessage)) { return(StatusCode(400, validationMessage)); } var property = await _dbContext.Properties.FindAsync(model.Id); property.DataTypeId = (DataTypeEnum?)model.DataTypeId; property.DataEntityTypeId = model.DataEntityTypeId; 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.InversePropertyId; } await SyncBehaviors(property, model); await ProcessPropertyLocalFacets(model, true, property); await _dbContext.SaveChangesAsync(); ((RequestLogModel)HttpContext.Items["RequestLog"]).PropertyId = property.Id; return(Ok()); }
private async Task HandleForeignKey(AddNEditPropertyModel 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, DataEntityTypeId = model.DataEntityTypeId, OwnerEntityTypeId = property.OwnerEntityTypeId, 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(); } }