Beispiel #1
0
        public async Task <Response> CadastrarAsync(EnderecoDto dto)
        {
            try
            {
                var cliente = await _clienteRepository.ObterAsync(Cpf.DesformatarNumero(dto.Cpf));

                if (cliente == null)
                {
                    return(CriarResposta(UNPROCESSABLE_ENTITY, false, "Cliente inexistente."));
                }

                if (await _enderecoRepository.ObterAsync(cliente.Id) != null)
                {
                    return(CriarResposta(UNPROCESSABLE_ENTITY, false, "Já existe um endereço para esse cliente."));
                }

                var endereco = EnderecoMapper.MapearDtoParaModelo(cliente.Id, dto);

                if (!endereco.EValido())
                {
                    return(CriarResposta(UNPROCESSABLE_ENTITY, false, cliente.GetMensagemValidacao()));
                }

                await _enderecoRepository.CadastrarAsync(endereco);

                return(CriarResposta(OK, true, "Endereço cadastrado."));
            }
            catch (System.Exception)
            {
                return(CriarResposta(INTERNAL_SERVER_ERROR, false, "Ocorreu um erro ao tentar cadastrar o endereço. Favor aguardar uns minutos e tentar novamente."));
            }
        }
Beispiel #2
0
        public async Task <IEnumerable <ListarClienteDto> > ListarAsync(ClienteDto clienteDto)
        {
            IList <ListarClienteDto> clientes = new List <ListarClienteDto>();

            //using (MySqlConnection con = new MySqlConnection(_connectionString))
            using (var con = _apiClientesContext.CriarConexao())
            {
                string comandoSQL = "select nome, cpf, idade from cliente";
                string filter     = "";

                if (!string.IsNullOrWhiteSpace(clienteDto.Cpf))
                {
                    filter += "cpf like " + "'%" + Cpf.DesformatarNumero(clienteDto.Cpf) + "%'";
                }

                if (!string.IsNullOrWhiteSpace(clienteDto.Nome))
                {
                    if (!string.IsNullOrEmpty(filter))
                    {
                        filter += " and ";
                    }

                    filter += "nome like " + "'%" + clienteDto.Nome + "%'";
                }

                if (!string.IsNullOrEmpty(filter))
                {
                    comandoSQL += " where " + filter;
                }

                var cmd = _apiClientesContext.CriarComando(comandoSQL, con);

                cmd.CommandType = CommandType.Text;

                con.Open();
                var reader = await cmd.ExecuteReaderAsync();

                while (reader.Read())
                {
                    clientes.Add(
                        new ListarClienteDto
                    {
                        Nome  = reader[0].ToString(),
                        Cpf   = reader[1].ToString(),
                        Idade = reader[2].ToString()
                    }
                        );
                }

                reader.Close();
            }

            return(clientes.AsEnumerable());
        }
Beispiel #3
0
        public async Task <IEnumerable <EnderecoDto> > ListarAsync(EnderecoDto enderecoDto)
        {
            IList <EnderecoDto> enderecos = new List <EnderecoDto>();

            using (var con = _apiClientesContext.CriarConexao())
            {
                string comandoSQL = "select nome, cpf, logradouro, bairro, cidade, estado from cliente c inner join endereco e on c.id = e.id_cliente";
                string filter     = "";

                if (!string.IsNullOrWhiteSpace(enderecoDto.Cpf))
                {
                    filter += "cpf like " + "'%" + Cpf.DesformatarNumero(enderecoDto.Cpf) + "%'";
                }

                if (!string.IsNullOrWhiteSpace(enderecoDto.Cliente))
                {
                    if (!string.IsNullOrEmpty(filter))
                    {
                        filter += " and ";
                    }

                    filter += "nome like " + "'%" + enderecoDto.Cliente + "%'";
                }

                if (!string.IsNullOrWhiteSpace(enderecoDto.Logradouro))
                {
                    if (!string.IsNullOrEmpty(filter))
                    {
                        filter += " and ";
                    }

                    filter += "logradouro like " + "'%" + enderecoDto.Logradouro + "%'";
                }

                if (!string.IsNullOrWhiteSpace(enderecoDto.Bairro))
                {
                    if (!string.IsNullOrEmpty(filter))
                    {
                        filter += " and ";
                    }

                    filter += "bairro like " + "'%" + enderecoDto.Bairro + "%'";
                }

                if (!string.IsNullOrWhiteSpace(enderecoDto.Cidade))
                {
                    if (!string.IsNullOrEmpty(filter))
                    {
                        filter += " and ";
                    }

                    filter += "cidade like " + "'%" + enderecoDto.Cidade + "%'";
                }

                if (!string.IsNullOrWhiteSpace(enderecoDto.Estado))
                {
                    if (!string.IsNullOrEmpty(filter))
                    {
                        filter += " and ";
                    }

                    filter += "estado like " + "'%" + enderecoDto.Estado + "%'";
                }

                if (!string.IsNullOrEmpty(filter))
                {
                    comandoSQL += " where " + filter;
                }

                var cmd = _apiClientesContext.CriarComando(comandoSQL, con);

                cmd.CommandType = CommandType.Text;

                con.Open();
                var reader = await cmd.ExecuteReaderAsync();

                while (reader.Read())
                {
                    enderecos.Add(
                        new EnderecoDto
                    {
                        Cliente    = reader[0].ToString(),
                        Cpf        = reader[1].ToString(),
                        Logradouro = reader[2].ToString(),
                        Bairro     = reader[3].ToString(),
                        Cidade     = reader[4].ToString(),
                        Estado     = reader[5].ToString(),
                    }
                        );
                }

                reader.Close();
            }

            return(enderecos.AsEnumerable());
        }