Example #1
0
        public ActionResult <List <Cidadao> > GetAllPaginationWithFamilia([FromHeader] string ibge, int page, int pagesize, string search, string fields)
        {
            try
            {
                ibge = _config.GetConnectionString(Connection.GetConnection(ibge));
                string filtro = string.Empty;

                if (!string.IsNullOrWhiteSpace(search))
                {
                    if (fields != null && fields.Split(",").Length > 0 && fields.Split(",")[0] != null)
                    {
                        filtro += Helper.GetFiltro(fields, search);
                    }
                    else
                    {
                        var stringcod = string.Empty;
                        if (Helper.soContemNumeros(search))
                        {
                            stringcod = $" cp.csi_codpac = {search} OR ";
                        }

                        filtro += $@" WHERE ( {stringcod}
                                             (SELECT UPPER(RETORNO) FROM TIRA_ACENTOS(CP.CSI_NOMPAC)) LIKE '%{Helper.RemoveAcentos(search.ToUpper())}%')";
                    }
                }

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

                Response.Headers.Add("X-Total-Count", count.ToString());
                List <CidadaoFamiliaViewModel> lista = _cidadaoRepository.GetAllPaginationWithFamilia(ibge, page, pagesize, filtro);

                return(Ok(lista));
            }
            catch (Exception ex)
            {
                var response = TrataErro.GetResponse(ex.Message, true);
                return(StatusCode((int)HttpStatusCode.InternalServerError, response));
            }
        }