Ejemplo n.º 1
0
        public ActionResult <List <ProcedimentoAvulso> > GetAllPagination([FromHeader] string ibge, int page,
                                                                          int pagesize, string search, string fields)
        {
            try
            {
                string filtro = string.Empty;
                ibge = _config.GetConnectionString(Connection.GetConnection(ibge));

                //rever filtros com front
                if (!string.IsNullOrWhiteSpace(search))
                {
                    if (fields != null && fields.Split(",").Length > 0 && fields.Split(",")[0] != null)
                    {
                        filtro += Helper.GetFiltroInicial(fields, search);
                    }
                    else
                    {
                        var stringcod = string.Empty;
                        if (Helper.soContemNumeros(search))
                        {
                            stringcod = $" CSI_CODPAC CONTAINING '{search}' OR ";
                        }

                        filtro += $@"  {stringcod}
                                     CSI_NOMPAC CONTAINING '{search}' OR 
                                     CSI_CODMED CONTAINING '{search}' OR
                                     CSI_NOMMED CONTAINING '{search}' OR 
                                     CSI_DATA CONTAINING '{search}' OR
                                     CSI_NOMUSU CONTAINING '{search}'
                                     CSI_DATAINC CONTAINING '{search}'
                                     CSI_CONTROLE CONTAINING '{search}'";
                    }
                }

                if (!string.IsNullOrWhiteSpace(filtro))
                {
                    filtro = $" WHERE " + filtro;
                }

                int count = _repository.GetCountAll(ibge, filtro);
                if (count == 1)
                {
                    page = 0;
                }
                else
                {
                    page = page * pagesize;
                }

                var lista = _repository.GetAllPagination(ibge, filtro, page, pagesize);
                Response.Headers.Add("X-Total-Count", count.ToString());
                return(Ok(lista));
            }
            catch (Exception ex)
            {
                var response = TrataErro.GetResponse(ex.Message, true);
                return(StatusCode((int)HttpStatusCode.InternalServerError, response));
            }
        }