示例#1
0
        public async Task <GridResult> ObterDadosGrid(GridRequest request = null)
        {
            var result = new GridResult();

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

                result.Data = (await _projetoRepository.GetAllAsync())
                              .Select(x => new { x.Id, x.Tema, x.Descricao, x.Periodo, NomeProfessor = x.Professor.Usuario.Nome });
                return(result);
            }

            var data = string.IsNullOrWhiteSpace(request.Search) ?
                       await _projetoRepository.GetAllAsync() :
                       await _projetoRepository.SearchAsync(x =>
                                                            x.Tema.IndexOf(request.Search, StringComparison.OrdinalIgnoreCase) >= 0 ||
                                                            x.Descricao.IndexOf(request.Search, StringComparison.OrdinalIgnoreCase) >= 0 ||
                                                            x.Periodo.IndexOf(request.Search, StringComparison.OrdinalIgnoreCase) >= 0 ||
                                                            x.Professor.Usuario.Nome.IndexOf(request.Search, StringComparison.OrdinalIgnoreCase) >= 0);

            result.Total = data.Count();

            if (!string.IsNullOrWhiteSpace(request.OrderBy))
            {
                if (request.Direction == Direction.Descendent)
                {
                    data =
                        request.OrderBy == "tema" ? data.OrderByDescending(x => x.Tema) :
                        request.OrderBy == "descricao" ? data.OrderByDescending(x => x.Descricao) :
                        request.OrderBy == "periodo" ? data.OrderByDescending(x => x.Periodo) :
                        request.OrderBy == "nomeProfessor" ? data.OrderByDescending(x => x.Professor.Usuario.Nome) : data;
                }
                else
                {
                    data =
                        request.OrderBy == "tema" ? data.OrderBy(x => x.Tema) :
                        request.OrderBy == "descricao" ? data.OrderBy(x => x.Descricao) :
                        request.OrderBy == "periodo" ? data.OrderBy(x => x.Periodo) :
                        request.OrderBy == "nomeProfessor" ? data.OrderBy(x => x.Professor.Usuario.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.Tema, x.Descricao, x.Periodo, NomeProfessor = x.Professor.Usuario.Nome });

            return(result);
        }