Example #1
0
 public function cadastrar()
 {
     try {
         if (empty($_POST['nome'])) {
             throw new \InvalidArgumentException("Favor preencher o nome do usuário");
         }
         if (empty($_POST['perfil'])) {
             throw new \InvalidArgumentException("Favor selecionar o perfil do usuárui");
         }
         if (empty($_POST['email'])) {
             throw new \InvalidArgumentException("Favor preencher o e-mail do usuário");
         }
         $perfil = new Perfil();
         $perfil->id = $_POST['perfil'];
         $perfil = $this->fachada->buscarPerfil($perfil);
         $usuario = new Usuario();
         $usuario->id = $_POST['id'];
         $usuario->nome = $_POST['nome'];
         $usuario->email = $_POST['email'];
         $usuario->ativo = !empty($_POST['ativo']);
         $usuario->perfil = $perfil;
         if (empty($usuario->id)) {
             $this->fachada->cadastrarUsuario($usuario);
         } else {
             $this->fachada->alterarUsuario($usuario);
         }
         echo new JSONResponse(true, self::MSG_OPERACAO_SUCESSO);
     } catch (\Exception $ex) {
         echo new JSONResponse(false, $ex->getMessage());
     }
 }