public object SaveEntity([ModelBinder(typeof(FluidityEntityBinder))] FluidityEntityPostModel postModel) { FluidityEntityEditModel display; var sectionConfig = Context.Config.Sections[postModel.Section]; var collectionConfig = sectionConfig.Tree.FlattenedTreeItems[postModel.Collection] as FluidityCollectionConfig; // Convert ID type for type checking var idProp = collectionConfig.IdProperty; var defaultId = idProp.Type.GetDefaultValue(); if (postModel.Id != null && postModel.Id.GetType() != idProp.Type) { var convert = postModel.Id.TryConvertTo(idProp.Type); if (convert.Success) { postModel.Id = convert.Result; } } // Get or create entity var entity = postModel.Id != null && !postModel.Id.Equals(defaultId) ? Context.Services.EntityService.GetEntity(collectionConfig, postModel.Id) : Context.Services.EntityService.NewEntity(collectionConfig); // Map property values var mapper = new FluidityEntityMapper(); entity = mapper.FromPostModel(sectionConfig, collectionConfig, postModel, entity); // Validate the property values (review ContentItemValidationHelper) var validator = new FluidityEntityPostValidator(); validator.Validate(ModelState, postModel, entity, collectionConfig); // Check to see if model is valid if (!ModelState.IsValid) { display = Context.Services.EntityService.GetEntityEditModel(sectionConfig, collectionConfig, entity); display.Errors = ModelState.ToErrorDictionary(); throw new HttpResponseException(Request.CreateValidationErrorResponse(display)); } // Do the save entity = Context.Services.EntityService.SaveEntity(collectionConfig, entity); display = Context.Services.EntityService.GetEntityEditModel(sectionConfig, collectionConfig, entity); // Return the updated model return(display); }
public IEnumerable <FluidityEntityDisplayModel> GetEntityDisplayModelsByIds(FluiditySectionConfig section, FluidityCollectionConfig collection, object[] ids) { // Get the entities var entities = GetEntitiesByIds(section, collection, ids); if (entities == null) { return(null); } // Map the results to the view display var mapper = new FluidityEntityMapper(); return(entities.Select(x => mapper.ToDisplayModel(section, collection, x))); }
public FluidityEntityEditModel GetEntityEditModel(FluiditySectionConfig section, FluidityCollectionConfig collection, object entityOrId = null) { var repo = RepositoryFactory.GetRepository(collection); object entity = null; if (entityOrId != null) { entity = (entityOrId.GetType() == collection.EntityType ? entityOrId : null) ?? repo.Get(entityOrId); } var mapper = new FluidityEntityMapper(); var scaffold = mapper.ToEditModel(section, collection, entity); return(scaffold); }
public PagedResult <FluidityEntityDisplayModel> GetEntityDisplayModels(FluiditySectionConfig section, FluidityCollectionConfig collection, int pageNumber = 1, int pageSize = 10, string query = null, string orderBy = null, string orderDirection = null, string dataView = null) { // Construct where clause LambdaExpression whereClauseExp = null; // Determine the data view where clause if (!dataView.IsNullOrWhiteSpace()) { var wc = collection.ListView?.DataViewsBuilder?.GetDataViewWhereClause(dataView); if (wc != null) { whereClauseExp = wc; } } // Construct a query where clause (and combind with the data view where clause if one exists) if (!query.IsNullOrWhiteSpace() && collection.ListView != null && collection.SearchableProperties.Any()) { LambdaExpression queryExpression = null; // Create shared expressions var parameter = whereClauseExp != null ? whereClauseExp.Parameters.First() : Expression.Parameter(collection.EntityType); var queryConstantExpression = Expression.Constant(query, typeof(string)); // Loop through searchable fields foreach (var searchProp in collection.SearchableProperties) { // Create field starts with expression var property = Expression.Property(parameter, searchProp.PropertyInfo); var startsWithCall = Expression.Call(property, "StartsWith", null, queryConstantExpression); var lambda = Expression.Lambda(startsWithCall, parameter); // Combine query queryExpression = queryExpression == null ? lambda : Expression.Lambda(Expression.OrElse(queryExpression.Body, lambda.Body), parameter); } // Combine query with any existing where clause if (queryExpression != null) { whereClauseExp = whereClauseExp == null ? queryExpression : Expression.Lambda(Expression.AndAlso(whereClauseExp.Body, queryExpression.Body), parameter); } } // Parse the order by LambdaExpression orderByExp = null; if (!orderBy.IsNullOrWhiteSpace() && !orderBy.InvariantEquals("name")) { // Convert string into an Expression<Func<TEntityType, object>> var prop = collection.EntityType.GetProperty(orderBy); if (prop != null) { var parameter = Expression.Parameter(collection.EntityType); var property = Expression.Property(parameter, prop); var castToObject = Expression.Convert(property, typeof(object)); orderByExp = Expression.Lambda(castToObject, parameter); } } var orderDir = !orderDirection.IsNullOrWhiteSpace() ? orderDirection.InvariantEquals("asc") ? SortDirection.Ascending : SortDirection.Descending : collection.SortDirection; PagedResult <object> result; using (var repo = RepositoryFactory.GetRepository(collection)) { // Perform the query result = repo?.GetPaged(pageNumber, pageSize, whereClauseExp, orderByExp, orderDir); } // If we've got no results, return an empty result set if (result == null) { return(new PagedResult <FluidityEntityDisplayModel>(0, pageNumber, pageSize)); } // Map the results to the view display var mapper = new FluidityEntityMapper(); return(new PagedResult <FluidityEntityDisplayModel>(result.TotalItems, pageNumber, pageSize) { Items = result.Items.Select(x => mapper.ToDisplayModel(section, collection, x)) }); }