public ActionResult <Cidadao> Inserir([FromHeader] string ibge, [FromBody] Cidadao model) { try { ibge = _config.GetConnectionString(Connection.GetConnection(ibge)); #region Valida se cidadão já existe string filtro = string.Empty; if (!string.IsNullOrWhiteSpace(model.csi_cpfpac)) { filtro += $@" PAC.CSI_CPFPAC LIKE '%{model.csi_cpfpac}%'"; } if (!string.IsNullOrWhiteSpace(filtro) && !string.IsNullOrWhiteSpace(model.csi_idepac)) { filtro += $@" OR PAC.CSI_IDEPAC LIKE '%{model.csi_idepac}%'"; } else if (string.IsNullOrWhiteSpace(filtro) && !string.IsNullOrWhiteSpace(model.csi_idepac)) { filtro += $@" PAC.CSI_IDEPAC LIKE '%{model.csi_idepac}%'"; } if (!string.IsNullOrWhiteSpace(filtro) && !string.IsNullOrWhiteSpace(model.csi_ncartao)) { filtro += $@" OR PAC.CSI_CARTAO LIKE '%{model.csi_ncartao}%'"; } else if (string.IsNullOrWhiteSpace(filtro) && !string.IsNullOrWhiteSpace(model.csi_ncartao)) { filtro += $@" PAC.CSI_CARTAO LIKE '%{model.csi_ncartao}%'"; } filtro = " WHERE " + filtro; var retorno = _cidadaoRepository.ValidaExistenciaCidadao(ibge, filtro); if (retorno.Item1) { throw new Exception($"Cidadão já se encontra cadastrado. Cidadão {retorno.Item2} - {retorno.Item3}"); } #endregion var id = _cidadaoRepository.GetNewId(ibge); model.csi_codpac = id; _cidadaoRepository.Insert(model, ibge); return(Ok(model)); } catch (Exception ex) { var response = TrataErro.GetResponse(ex.Message, true); return(StatusCode((int)HttpStatusCode.InternalServerError, response)); } }