Exemple #1
0
        public PaginationQueryDto Find(PaginationQueryInput input)
        {
            if (input == null)
            {
                throw new ArgumentNullException($"{nameof(input)} should not be null.");
            }
            input.Validate();
            PaginationQueryDto output;

            Expression <Func <Core.Entities.Task, bool> > where = t => !t.IsDeleted;

            var keyword = input.GetFilterValue("keyword");

            if (!string.IsNullOrWhiteSpace(keyword))
            {
                where = where.AndAlso(t => t.Name.Contains(keyword));
            }

            var isRunning = input.GetFilterValue("isrunning");

            if (!string.IsNullOrWhiteSpace(isRunning))
            {
                if ("true" == isRunning.ToLower())
                {
                    where = where.AndAlso(t => t.IsRunning);
                }
                else
                {
                    where = where.AndAlso(t => !t.IsRunning);
                }
            }

            switch (input.Sort)
            {
            case "name":
            {
                output = DbContext.Task.PageList(input, where, t => t.Name);
                break;
            }

            case "nodecount":
            {
                output = DbContext.Task.PageList(input, where, t => t.NodeCount);
                break;
            }

            default:
            {
                output = DbContext.Task.PageList(input, where, t => t.CreationTime);
                break;
            }
            }
            output.Result = Mapper.Map <List <TaskDto> >(output.Result);
            return(output);
        }
        public static PaginationQueryDto PageList <TEntity, TKey>(this DbSet <TEntity> dbSet, PaginationQueryInput input,
                                                                  Expression <Func <TEntity, bool> > where    = null,
                                                                  Expression <Func <TEntity, TKey> > orderyBy = null) where TEntity : Entity <long>
        {
            input.Validate();
            PaginationQueryDto   output   = new PaginationQueryDto();
            IQueryable <TEntity> entities = dbSet.AsQueryable();

            if (where != null)
            {
                entities = entities.Where(where);
            }

            output.Total = entities.Count();

            if (orderyBy == null)
            {
                if (input.SortByDesc)
                {
                    entities = entities.OrderByDescending(e => e.Id).Skip((input.Page.Value - 1) * input.Size.Value).Take(input.Size.Value);
                }
                else
                {
                    entities = entities.Skip((input.Page.Value - 1) * input.Size.Value).Take(input.Size.Value);
                }
            }
            else
            {
                if (input.SortByDesc)
                {
                    entities = entities.OrderByDescending(orderyBy).Skip((input.Page.Value - 1) * input.Size.Value).Take(input.Size.Value);
                }
                else
                {
                    entities = entities.OrderBy(orderyBy).Skip((input.Page.Value - 1) * input.Size.Value).Take(input.Size.Value);
                }
            }

            output.Page   = input.Page.Value;
            output.Size   = input.Size.Value;
            output.Result = entities.ToList();
            return(output);
        }