Ejemplo n.º 1
0
        public async Task <GridResult> ObterDadosGrid(GridRequest request = null)
        {
            var result = new GridResult();

            if (request == null)
            {
                result.Total = await _disciplinaRepository.CountAsync();

                result.Data = (await _disciplinaRepository.GetAllAsync())
                              .Select(x => new { x.Id, x.Nome });
                return(result);
            }

            var data = string.IsNullOrWhiteSpace(request.Search) ?
                       await _disciplinaRepository.GetAllAsync() :
                       await _disciplinaRepository.SearchAsync(x => x.Nome.IndexOf(request.Search, StringComparison.OrdinalIgnoreCase) >= 0);

            result.Total = data.Count();

            if (!string.IsNullOrWhiteSpace(request.OrderBy))
            {
                if (request.Direction == Direction.Descendent)
                {
                    data =
                        request.OrderBy == "nome" ? data.OrderByDescending(x => x.Nome) : data;
                }
                else
                {
                    data =
                        request.OrderBy == "nome" ? data.OrderBy(x => x.Nome) : data;
                }
            }

            if (request.PageSize > 0 && request.Page > 0)
            {
                data = data.Skip((request.Page - 1) * request.PageSize).Take(request.Page * request.PageSize);
            }

            result.Data = data.Select(x => new { x.Id, x.Nome });

            return(result);
        }