Пример #1
0
        public void Post([FromBody] Clinica value)
        {
            using (var transactionScope = new TransactionScope())
            {
                var conexao = new Connection(new SqlConnection());
                try
                {
                    #region Validações
                    if (value.Usuario == null)
                    {
                        throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotAcceptable)
                        {
                            Content      = new StringContent("Usuário não informado."),
                            ReasonPhrase = "Campo inválido"
                        });
                    }

                    if (value.Endereco == null)
                    {
                        throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotAcceptable)
                        {
                            Content      = new StringContent("Endereço não informado."),
                            ReasonPhrase = "Campo inválido"
                        });
                    }

                    if (string.IsNullOrWhiteSpace(value.Usuario.Login))
                    {
                        throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotAcceptable)
                        {
                            Content      = new StringContent("Login não informado."),
                            ReasonPhrase = "Campo inválido"
                        });
                    }

                    if (string.IsNullOrWhiteSpace(value.Usuario.Senha))
                    {
                        throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotAcceptable)
                        {
                            Content      = new StringContent("Senha não informada."),
                            ReasonPhrase = "Campo inválido"
                        });
                    }
                    #endregion
                    using (var repositorioEndereco = new EnderecoRepositorio(conexao))
                        using (var repositorioClinica = new ClinicaRepositorio(conexao))
                            using (var repositorioUsuario = new UsuarioRepositorio(conexao))
                            {
                                if (value.Endereco.IdBairro <= 0)
                                {
                                    value.Endereco.IdBairro = repositorioEndereco.ObterIdBairro(value.Endereco);
                                }

                                value.Id = repositorioClinica.Criar(value);
                                value.Usuario.idClinica = value.Id;
                                repositorioUsuario.Criar(value.Usuario);
                                transactionScope.Complete();
                            }
                }
                catch
                {
                    throw;
                }
            }
        }