Ejemplo n.º 1
0
        public IHttpActionResult ChangePassword([FromBody] ChangePasswordRequest changePasswordRequest)
        {
            AtendenteEmpresa atendente = null;
            UsuarioCliente   usuario   = null;

            using (var responseMsg = new HttpResponseMessage())
            {
                IHttpActionResult result = null;

                try
                {
                    //Se o email enviado é null, retornar BadRequest
                    if (changePasswordRequest == null)
                    {
                        return(BadRequest("Dados inválidos."));
                    }

                    if (changePasswordRequest.UserType == Tipos.Login.Atendimento)
                    {
                        atendente = _atendenteEmpresaBusiness.GetByUsernameAndPassword(changePasswordRequest.Username, changePasswordRequest.OldPassword);

                        if (atendente != null)
                        {
                            atendente.Password   = changePasswordRequest.NewPassword;
                            atendente.Provisorio = false;

                            if (_atendenteEmpresaBusiness.UpdatePassword(atendente))
                            {
                                atendente.Password = "******";

                                //Monta response
                                result = Ok(Retorno <AtendenteEmpresa> .Criar(true, "Troca de Senha Realizada Com Sucesso.", atendente));
                            }
                        }
                    }
                    else
                    {
                        usuario = _usuarioClienteBusiness.GetByUsernameAndPassword(changePasswordRequest.Username, changePasswordRequest.OldPassword);

                        if (usuario != null)
                        {
                            usuario.Password   = changePasswordRequest.NewPassword;
                            usuario.Provisorio = false;

                            if (_usuarioClienteBusiness.UpdatePassword(usuario))
                            {
                                usuario.Password = "******";

                                //Monta response
                                result = Ok(Retorno <UsuarioCliente> .Criar(true, "Troca de Senha Realizada Com Sucesso.", usuario));
                            }
                        }
                    }

                    //Retorna o response com o token
                    return(result);
                }
                catch (Exception ex)
                {
                    return(InternalServerError(ex));
                }
            }
        }