public static object ExcludeIgnoredPropertiesPagingModel <TItem>(RepositoryPagingModel <TItem> filter, List <string> loadProperties, Type itemType)
     where TItem : class
 {
     return(JsonConvert.SerializeObject(filter, Formatting.Indented, new JsonSerializerSettings()
     {
         ContractResolver = new PagingContractResolver(loadProperties, itemType)
     }));
 }
Exemplo n.º 2
0
        public static IMappingExpression <TSource, TDestination> LoadProperties <TSource, TDestination>(this IMappingExpression <TSource, TDestination> expression, RepositoryPagingModel <TDestination> filter, DbContext context = null)
            where TSource : class
        {
            var destType = typeof(TDestination);
            var infos    = destType.GetProperties();

            if (!string.IsNullOrEmpty(filter.Distinct))
            {
                filter.LoadProperties = new List <string>()
                {
                    filter.Distinct.Split(new[] { '.' }, 2)[0]
                }
            }
            ;

            var properties = (filter.LoadProperties ?? (filter.LoadProperties = new List <string>())).ToList();

            if (!properties.Any())
            {
                var propertyInfoIgnore = infos.Where(x => x.GetCustomAttributes(typeof(NotLoadByDefault), true).Any()).ToList();

                foreach (var property in propertyInfoIgnore)
                {
                    expression.ForMember(property.Name, opt => opt.Ignore());
                }
                return(expression);
            }

            var orderProperties = filter.OrderProperty?.Split(new[] { ',', ' ', '/', '\\' }, StringSplitOptions.RemoveEmptyEntries).ToList() ?? new List <string>();

            if (string.IsNullOrEmpty(filter.Distinct) && context != null)
            {
                orderProperties.AddRange(context.GetKeyNames <TSource>());
            }

            if (orderProperties.Any())
            {
                foreach (var property in orderProperties)
                {
                    var split = property.Split(new[] { '.' }, 2)[0];
                    if (string.IsNullOrEmpty(split))
                    {
                        continue;
                    }

                    var orderProperty = infos.FirstOrDefault(x => string.Equals(x.Name, split, StringComparison.InvariantCultureIgnoreCase));

                    if (orderProperty != null && !properties.Contains(orderProperty.Name, StringComparer.OrdinalIgnoreCase))
                    {
                        properties.Add(orderProperty.Name);
                    }
                }
            }

            var orderPropertyDefault = infos.FirstOrDefault(x => x.GetCustomAttributes(typeof(DefaultSortPropertyAttribute), true).Any());

            if (orderPropertyDefault != null && !properties.Contains(orderPropertyDefault.Name, StringComparer.OrdinalIgnoreCase))
            {
                properties.Add(orderPropertyDefault.Name);
            }

            if (filter.Filters?.Any() ?? false)
            {
                var propertyValuePairs = QueryableUtils.ToPropertyValuePairs(filter.Filters, destType);
                properties.AddRange(propertyValuePairs.SelectMany(x => x.Select(c => c.Property.Name)));
            }

            var propertyInfos = infos.Where(x => !properties.Contains(x.Name, StringComparer.OrdinalIgnoreCase) && (!x.GetCustomAttributes(typeof(LoadAlways), true).Any() || x.GetCustomAttributes(typeof(NotLoadByDefault), true).Any())).ToList();

            foreach (var property in propertyInfos)
            {
                expression.ForMember(property.Name, opt => opt.Ignore());
            }

            return(expression);
        }
    }
 public static OkObjectResult OkPaging <TController, TItem>(this TController controller, RepositoryPagingModel <TItem> filter)
     where TController : Controller
     where TItem : class
 {
     return(filter.LoadProperties?.Any() ?? false ? new OkObjectResult(RepositoryPagingModelUtils.ExcludeIgnoredPropertiesPagingModel(filter, filter.LoadProperties, typeof(TItem))) : controller.Ok(filter));
 }
Exemplo n.º 4
0
        public virtual PagingQueryable <TCast> GetPage <TCast>(RepositoryPagingModel <TCast> filter, Expression <Func <T, bool> > checkPermission, List <Expression <Func <T, bool> > > @where, Action <IMapperConfigurationExpression> configure, List <Expression <Func <TCast, bool> > > postWhere = null) where TCast : class
        {
            var page = new Page(filter.PageNumber, filter.PageSize);

            var queryable = Dbset;

            if (checkPermission != null)
            {
                queryable = queryable.Where(checkPermission);
            }

            foreach (var expression in where)
            {
                queryable = queryable.Where(expression);
            }

            var configurationProvider = AutoMapperDomainUtils.GetConfigurationProvider(config =>
            {
                configure.Invoke(config);
                var mappingExpression = ((IProfileConfiguration)config).TypeMapConfigs.FirstOrDefault(x => x is IMappingExpression <T, TCast>) as IMappingExpression <T, TCast>;
                mappingExpression?.LoadProperties(filter, DataContext);
            });
            var castQuery = queryable.ProjectTo <TCast>(configurationProvider).AsExpandable().WithTranslations();

            if (postWhere != null)
            {
                foreach (var expression in postWhere)
                {
                    castQuery = castQuery.Where(expression);
                }
            }

            if (filter.Filters?.Any() ?? false)
            {
                castQuery = castQuery.Where(QueryableUtils.FiltersToLambda <TCast>(filter.Filters));
            }

            var orderProperties = filter.OrderProperty?.Split(new[] { ',', ' ', '/', '\\' }, StringSplitOptions.RemoveEmptyEntries).ToList() ?? new List <string>();

            IOrderedQueryable <TCast> orderedQuery;

            if (filter.Distinct.IsNullOrEmpty())
            {
                orderProperties.AddRange(_dataContext.GetKeyNames <T>());
                orderProperties = orderProperties.Distinct(new GenericCompare <string>(x => x.ToLowerInvariant())).ToList();

                if (orderProperties.Any())
                {
                    orderedQuery = filter.IsDesc ? castQuery.OrderByDescendingWithNullLowPriority(orderProperties.First()) : castQuery.OrderByWithNullLowPriority(orderProperties.First());
                    orderProperties.RemoveAt(0);
                }
                else
                {
                    orderedQuery = filter.IsDesc ? castQuery.OrderByDescendingWithNullLowPriority() : castQuery.OrderByWithNullLowPriority();
                }
                if (orderProperties.Any())
                {
                    orderedQuery = orderProperties.Aggregate(orderedQuery, (current, property) => filter.IsDesc ? current.ThenByDescendingWithNullLowPriority(property) : current.ThenByWithNullLowPriority(property));
                }
                else
                {
                    orderedQuery = filter.IsDesc ? orderedQuery.ThenByDescendingWithNullLowPriority() : orderedQuery.ThenByWithNullLowPriority();
                }
            }
            else
            {
                castQuery    = castQuery.DistinctByField(filter.Distinct).AsExpandable();
                orderedQuery = filter.IsDesc ? castQuery.OrderByDescendingWithNullLowPriority(filter.Distinct) : castQuery.OrderByWithNullLowPriority(filter.Distinct);
            }

            return(new PagingQueryable <TCast>(orderedQuery, page));
        }