示例#1
0
        async Task <Pagination <FormVM> > IRequestHandler <GetFormsQuery, Pagination <FormVM> > .Handle(GetFormsQuery request, CancellationToken cancellationToken)
        {
            int page  = request.Page ?? 1;
            int limit = request.Limit ?? 10;
            int total = await context.Forms.CountAsync(cancellationToken);

            var query = context.Forms.AsQueryable();

            if (!string.IsNullOrEmpty(request.SearchInput))
            {
                if (request.SearchColumns.Length != 0)
                {
                    var predicate = PredicateBuilder.New <Form>();
                    foreach (var column in request.SearchColumns)
                    {
                        ParameterExpression  param      = Expression.Parameter(typeof(Form), "form");
                        MemberExpression     property   = Expression.Property(param, column);
                        MethodInfo           method     = typeof(string).GetMethod("Contains", new[] { typeof(string) });
                        ConstantExpression   inputValue = Expression.Constant(request.SearchInput, typeof(string));
                        MethodCallExpression containsMethodExpression = Expression.Call(property, method, inputValue);
                        predicate = predicate.Or(Expression.Lambda <Func <Form, bool> >(containsMethodExpression, param));
                    }
                    query = query.Where(predicate);
                }
                else
                {
                    query = query.Where(x =>
                                        x.Name.Contains(request.SearchInput) ||
                                        x.Company.Contains(request.SearchInput) ||
                                        x.Telephone.Contains(request.SearchInput));
                }
            }
            if (!string.IsNullOrEmpty(request.OrderBy))
            {
                ParameterExpression param    = Expression.Parameter(typeof(Form), "x");
                MemberExpression    property = Expression.Property(param, request.OrderBy);
                var lambda = Expression.Lambda(property, param);

                var orderBy = Expression.Call(typeof(Queryable),
                                              request.OrderDirection != "desc" ? "OrderBy" : "OrderByDescending",
                                              new Type[] { typeof(Form), property.Type },
                                              query.Expression,
                                              Expression.Quote(lambda));

                query = query.Provider.CreateQuery <Form>(orderBy);
            }

            int         filtered = query.Count();
            List <Form> items    = await query.Skip((page - 1) *limit).Take(limit).ToListAsync(cancellationToken);

            List <FormVM> listsVM = mapper.Map <List <FormVM> >(items);

            foreach (var item in listsVM)
            {
                item.Sender.Username = (await userRepository.GetUserById(item.Sender.Id))?.Username;
            }

            Pagination <FormVM> pagination = new Pagination <FormVM> {
                TotalItems    = total,
                FilteredItems = filtered,
                Page          = page,
                Items         = listsVM,
                TotalPages    = (int)Math.Ceiling((double)filtered / limit)
            };

            pagination.BuildNavigation(request);

            return(pagination);
        }
示例#2
0
        public async Task <Pagination <User> > QueryUsersAsync(GetUsersQuery request)
        {
            //Todo generalization of this block
            int page  = request.Page ?? 1;
            int limit = request.Limit ?? 10;
            int total = await context.Users.CountAsync();

            var query = context.Users.AsQueryable();

            if (!string.IsNullOrEmpty(request.SearchInput))
            {
                if (request.SearchColumns.Length != 0)
                {
                    var predicate = PredicateBuilder.New <ApplicationUser>();
                    foreach (var column in request.SearchColumns)
                    {
                        ParameterExpression  param      = Expression.Parameter(typeof(ApplicationUser), "user");
                        MemberExpression     property   = Expression.Property(param, column);
                        MethodInfo           method     = typeof(string).GetMethod("Contains", new[] { typeof(string) });
                        ConstantExpression   inputValue = Expression.Constant(request.SearchInput, typeof(string));
                        MethodCallExpression containsMethodExpression = Expression.Call(property, method, inputValue);
                        predicate = predicate.Or(Expression.Lambda <Func <ApplicationUser, bool> >(containsMethodExpression, param));
                    }
                    query = query.Where(predicate);
                }
                else
                {
                    query = query.Where(x =>
                                        x.UserName.Contains(request.SearchInput) ||
                                        x.Email.Contains(request.SearchInput) ||
                                        x.PhoneNumber.Contains(request.SearchInput));
                }
            }
            if (!string.IsNullOrEmpty(request.OrderBy))
            {
                ParameterExpression param    = Expression.Parameter(typeof(ApplicationUser), "x");
                MemberExpression    property = Expression.Property(param, request.OrderBy);
                var lambda = Expression.Lambda(property, param);

                var orderBy = Expression.Call(typeof(Queryable),
                                              request.OrderDirection != "desc" ? "OrderBy" : "OrderByDescending",
                                              new Type[] { typeof(ApplicationUser), property.Type },
                                              query.Expression,
                                              Expression.Quote(lambda));

                query = query.Provider.CreateQuery <ApplicationUser>(orderBy);
            }
            int filtered = query.Count();
            List <ApplicationUser> items = await query.Skip((page - 1) *limit).Take(limit).ToListAsync();

            List <User> mappedUsers = new List <User>();

            foreach (var item in items)
            {
                mappedUsers.Add(await GetRolesAndClaimsAsync(item));
            }

            Pagination <User> pagination = new Pagination <User>
            {
                TotalItems    = await context.Users.CountAsync(),
                FilteredItems = filtered,
                Page          = page,
                Items         = mappedUsers,
                TotalPages    = (int)Math.Ceiling((double)filtered / limit)
            };

            pagination.BuildNavigation(request);

            return(pagination);
        }