Пример #1
0
        public int Post([FromBody] Paciente value)
        {
            using (var transactionScope = new TransactionScope())
            {
                try
                {
                    #region Validaçãoes
                    if (!Validacoes.ValidaCpf(value.Cpf))
                    {
                        throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotAcceptable)
                        {
                            Content      = new StringContent("Cpf inválido."),
                            ReasonPhrase = "Campo inválido"
                        });
                    }
                    else
                    {
                        value.Cpf = value.Cpf.Replace(".", "").Replace("-", "");
                    }

                    if (!string.IsNullOrEmpty(value.Cep))
                    {
                        if (!Validacoes.ValidaCep(value.Cep))
                        {
                            throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotAcceptable)
                            {
                                Content      = new StringContent("Cep inválido."),
                                ReasonPhrase = "Campo inválido"
                            });
                        }
                        else
                        {
                            value.Cep = value.Cep.Replace("-", "");
                        }
                    }

                    if (string.IsNullOrWhiteSpace(value.Nome))
                    {
                        throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotAcceptable)
                        {
                            Content      = new StringContent("Nome inválido."),
                            ReasonPhrase = "Campo inválido"
                        });
                    }

                    if (!Validacoes.ValidaDataNascimento(value.DataNascimento))
                    {
                        throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotAcceptable)
                        {
                            Content      = new StringContent("Data de nascimento inválida."),
                            ReasonPhrase = "Campo inválido"
                        });
                    }

                    if (!string.IsNullOrEmpty(value.Celular))
                    {
                        if (!Validacoes.ValidaCelular(value.Celular))
                        {
                            throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotAcceptable)
                            {
                                Content      = new StringContent("Celular inválido."),
                                ReasonPhrase = "Campo inválido"
                            });
                        }
                    }

                    if (!string.IsNullOrEmpty(value.Telefone))
                    {
                        if (!Validacoes.ValidaTelefone(value.Telefone))
                        {
                            throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotAcceptable)
                            {
                                Content      = new StringContent("Telefone inválido."),
                                ReasonPhrase = "Campo inválido"
                            });
                        }
                    }
                    #endregion

                    var conexao = new Connection(new System.Data.SqlClient.SqlConnection());
                    using (var repositorio = new PacienteRepositorio(conexao))
                    {
                        int idPaciente = repositorio.Criar(value);

                        if (value.Usuario != null)
                        {
                            if (!string.IsNullOrEmpty(value.Usuario.Senha))
                            {
                                using (var repositorioUsuario = new UsuarioRepositorio(conexao))
                                {
                                    value.Usuario.Login      = value.Email;
                                    value.Usuario.idPaciente = idPaciente;
                                    repositorioUsuario.Criar(value.Usuario);
                                }
                            }
                        }

                        transactionScope.Complete();
                        return(idPaciente);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }