Exemplo n.º 1
0
        public Retorno <Int32> Alterar(ToPessoas toPessoas)
        {
            try
            {
                Retorno <Int32> retAlterar = new();

                #region Validação dos campos da chave primária
                if (toPessoas.Id == null)
                {
                    return(retAlterar.RetornarFalha("Campo obrigatório ID não informado."));
                }
                #endregion
                //TODO: regras de negócio
                DBPessoas bdPessoas = new();

                retAlterar = bdPessoas.Alterar(toPessoas);
                if (!retAlterar.Ok)
                {
                    return(retAlterar.RetornarFalha(retAlterar.Mensagem));
                }

                return(retAlterar.RetornarSucesso(retAlterar.Dados, retAlterar.Mensagem));
            }
            catch (Exception e)
            {
                Retorno <Int32> retAlterar = new();
                return(retAlterar.RetornarFalha("Falha ao Alterar : " + e.Message));
            }
        }
Exemplo n.º 2
0
        public Retorno <Int32> Excluir(ToPessoas toPessoas)
        {
            string strCommand = "DELETE FROM CADASTROS.PESSOAS ";

            (bool where, string cmd)tID = (toPessoas.Id != null) ? (true, "ID = @ID ") : (false, "");

            strCommand += (tID.where) ? " WHERE " + tID.cmd : "";


            try
            {
                conexao = new(stringConnection);
                comando = new(strCommand, conexao);

                if (tID.where)
                {
                    comando.Parameters.AddWithValue("@ID", toPessoas.Id);
                }

                conexao.Open();
                comando.ExecuteNonQuery();

                Retorno <int> ret = new();
                return(ret.RetornarSucesso(1));
            }
            catch (Exception Ex)
            {
                Retorno <int> ret = new();
                return(ret.RetornarFalha(Ex.Message));
            }
            finally
            {
                conexao.Close();
            }
        }
Exemplo n.º 3
0
        public Retorno <Int32> Alterar(ToPessoas toPessoas)
        {
            string strCommand = "UPDATE CADASTROS.PESSOAS SET ";

            (bool where, string cmd)tNome     = (toPessoas.Nome != null) ? (true, " NOME = @NOME ,") : (false, "");
            (bool where, string cmd)tEndereco = (toPessoas.Endereco != null) ? (true, " ENDERECO = @ENDERECO ,") : (false, "");
            (bool where, string cmd)tTelefone = (toPessoas.Telefone != null) ? (true, " TELEFONE = @TELEFONE ,") : (false, "");
            (bool where, string cmd)tID       = (toPessoas.Id != null) ? (true, "ID = @ID ") : (false, "");

            string strTemp = tNome.cmd + tEndereco.cmd + tTelefone.cmd;

            strCommand += (tID.where || tNome.where || tEndereco.where || tTelefone.where) ? strTemp.Remove(strTemp.Length - 1, 1) + " WHERE " + tID.cmd : "";


            try
            {
                conexao = new(stringConnection);
                comando = new(strCommand, conexao);

                if (tID.where)
                {
                    comando.Parameters.AddWithValue("@ID", toPessoas.Id);
                }
                if (tNome.where)
                {
                    comando.Parameters.AddWithValue("@NOME", toPessoas.Nome);
                }
                if (tEndereco.where)
                {
                    comando.Parameters.AddWithValue("@ENDERECO", toPessoas.Endereco);
                }
                if (tTelefone.where)
                {
                    comando.Parameters.AddWithValue("@TELEFONE", toPessoas.Telefone);
                }

                conexao.Open();
                comando.ExecuteNonQuery();

                Retorno <int> ret = new();
                return(ret.RetornarSucesso(1));
            }
            catch (Exception Ex)
            {
                Retorno <int> ret = new();
                return(ret.RetornarFalha(Ex.Message));
            }
            finally
            {
                conexao.Close();
            }
        }
Exemplo n.º 4
0
        public Retorno <Int32> Incluir(ToPessoas toPessoas)
        {
            string strCommand = "INSERT INTO CADASTROS.PESSOAS (NOME, ENDERECO, TELEFONE) ";

            (bool where, string cmd)tID       = (toPessoas.Id != null) ? (true, " @ID ,") : (false, "");
            (bool where, string cmd)tNome     = (toPessoas.Nome != null) ? (true, " @NOME ,") : (false, "");
            (bool where, string cmd)tEndereco = (toPessoas.Endereco != null) ? (true, " @ENDERECO ,") : (false, "");
            (bool where, string cmd)tTelefone = (toPessoas.Telefone != null) ? (true, " @TELEFONE ") : (false, "");

            strCommand += (tID.where || tNome.where || tEndereco.where || tTelefone.where) ? "VALUES ( " + tID.cmd + tNome.cmd + tEndereco.cmd + tTelefone.cmd + " )": "";


            try
            {
                conexao = new(stringConnection);
                comando = new(strCommand, conexao);

                if (tID.where)
                {
                    comando.Parameters.AddWithValue("@ID", toPessoas.Id);
                }
                if (tNome.where)
                {
                    comando.Parameters.AddWithValue("@NOME", toPessoas.Nome);
                }
                if (tEndereco.where)
                {
                    comando.Parameters.AddWithValue("@ENDERECO", toPessoas.Endereco);
                }
                if (tTelefone.where)
                {
                    comando.Parameters.AddWithValue("@TELEFONE", toPessoas.Telefone);
                }

                conexao.Open();
                comando.ExecuteNonQuery();

                Retorno <int> ret = new();
                return(ret.RetornarSucesso(1));
            }
            catch (Exception Ex)
            {
                Retorno <int> ret = new();
                return(ret.RetornarFalha(Ex.Message));
            }
            finally
            {
                conexao.Close();
            }
        }
Exemplo n.º 5
0
        public Retorno <List <ToPessoas> > Listar(ToPessoas toPessoas)
        {
            try
            {
                //TODO: regras de negócio

                DBPessoas bdPessoas = new();
                Retorno <List <ToPessoas> > retListar = bdPessoas.Listar(toPessoas);
                if (!retListar.Ok)
                {
                    return(retListar.RetornarFalha("Falha ao Listar : " + retListar.Mensagem));
                }
                return(retListar.RetornarSucesso(retListar.Dados, retListar.Mensagem));
            }
            catch (Exception e)
            {
                Retorno <List <ToPessoas> > retListar = new();
                return(retListar.RetornarFalha("Falha ao Listar : " + e.Message));
            }
        }
Exemplo n.º 6
0
        public Retorno <Int32> Incluir(ToPessoas toPessoas)
        {
            try
            {
                Retorno <Int32> retIncluir = new();

                #region Validação de campos obrigatórios
                if (toPessoas.Endereco == null)
                {
                    return(retIncluir.RetornarFalha("Campo obrigatório ENDERECO não informado."));
                }
                if (toPessoas.Nome == null)
                {
                    return(retIncluir.RetornarFalha("Campo obrigatório NOME não informado."));
                }
                if (toPessoas.Telefone == null)
                {
                    return(retIncluir.RetornarFalha("Campo obrigatório TELEFONE não informado."));
                }
                #endregion

                toPessoas.Id = null;

                retIncluir = new();

                DBPessoas bdPessoas = new();
                retIncluir = bdPessoas.Incluir(toPessoas);
                if (!retIncluir.Ok)
                {
                    return(retIncluir.RetornarFalha("Falha ao Listar : " + retIncluir.Mensagem));
                }
                return(retIncluir.RetornarSucesso(retIncluir.Dados, retIncluir.Mensagem));
            }
            catch (Exception e)
            {
                Retorno <Int32> retIncluir = new();
                return(retIncluir.RetornarFalha("Falha ao Incluir : " + e.Message));
            }
        }
Exemplo n.º 7
0
        public Retorno <List <ToPessoas> > Listar(ToPessoas toPessoas)
        {
            string strCommand = "SELECT ID, NOME, ENDERECO, TELEFONE FROM CADASTROS.PESSOAS ";

            (bool where, string cmd)tID       = (toPessoas.Id != null) ? (true, "ID = @ID AND ") : (false, "");
            (bool where, string cmd)tNome     = (toPessoas.Nome != null) ? (true, "NOME = @NOME AND ") : (false, "");
            (bool where, string cmd)tEndereco = (toPessoas.Endereco != null) ? (true, "ENDERECO = @ENDERECO AND ") : (false, "");
            (bool where, string cmd)tTelefone = (toPessoas.Telefone != null) ? (true, "TELEFONE = @TELEFONE AND ") : (false, "");

            string strTemp = "WHERE " + tID.cmd + tNome.cmd + tEndereco.cmd + tTelefone.cmd;

            strCommand += (tID.where || tNome.where || tEndereco.where || tTelefone.where) ? strTemp.Remove(strTemp.Length - 4, 4) : "";


            try
            {
                conexao = new(stringConnection);
                comando = new(strCommand, conexao);

                if (tID.where)
                {
                    comando.Parameters.AddWithValue("@ID", toPessoas.Id);
                }
                if (tNome.where)
                {
                    comando.Parameters.AddWithValue("@NOME", toPessoas.Nome);
                }
                if (tEndereco.where)
                {
                    comando.Parameters.AddWithValue("@ENDERECO", toPessoas.Endereco);
                }
                if (tTelefone.where)
                {
                    comando.Parameters.AddWithValue("@TELEFONE", toPessoas.Telefone);
                }

                List <ToPessoas> list = new();

                conexao.Open();
                dataReader = comando.ExecuteReader();


                while (dataReader.Read())
                {
                    list.Add(new ToPessoas()
                    {
                        Id       = Convert.ToInt32(dataReader["ID"]),
                        Nome     = dataReader["NOME"].ToString(),
                        Endereco = dataReader["ENDERECO"].ToString(),
                        Telefone = dataReader["TELEFONE"].ToString()
                    });
                }
                dataReader.Close();

                Retorno <List <ToPessoas> > ret = new();
                return(ret.RetornarSucesso(list));
            }
            catch (Exception Ex)
            {
                Retorno <List <ToPessoas> > ret = new();
                return(ret.RetornarFalha(Ex.Message));
            }
            finally
            {
                conexao.Close();
            }
        }