From cfa29660a80bddf447f0228d4e9f38194519f456 Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Thu, 4 Jan 2018 22:08:18 -0700 Subject: [PATCH] Move relevant methods from User class to new Dictionary class --- backend/Dictionary.php | 60 ++++++++++++++++++++++++++++++++++++++++++ backend/User.php | 44 +++++++------------------------ backend/index.php | 4 +-- 3 files changed, 72 insertions(+), 36 deletions(-) create mode 100644 backend/Dictionary.php diff --git a/backend/Dictionary.php b/backend/Dictionary.php new file mode 100644 index 0000000..f87940c --- /dev/null +++ b/backend/Dictionary.php @@ -0,0 +1,60 @@ +db = new Db(); + $this->token = new Token(); + } + + public function create ($user) { + $insert_dictionary_query = "INSERT INTO dictionaries (user) VALUES ($user)"; + $insert_dictionary = $this->db->execute($insert_dictionary_query); + + if ($insert_dictionary === true) { + $new_dictionary_id = $this->db->lastInsertId(); + + $insert_linguistics_query = "INSERT INTO dictionary_linguistics (dictionary) VALUES ($new_dictionary_id)"; + $insert_linguistics = $this->db->execute($insert_linguistics_query); + + if ($insert_linguistics === true) { + if ($this->changeCurrentDictionary($user, $new_dictionary_id)) { + $user_data = array( + 'id' => $user, + 'isMember' => $this->hasMembership($user), + 'dictionary' => $new_dictionary_id, + ); + return $this->token->encode($user_data); + } + } + } + + return false; + } + + public function changeCurrent ($user, $dictionary) { + $update_query = 'UPDATE users SET current_dictionary=? WHERE id=?'; + $update = $this->db->query($update_query, array($dictionary, $user)); + if ($update->rowCount() > 0) { + return true; + } + return false; + } + + public function getAllNames ($user) { + $query = "SELECT id, name, specification FROM dictionaries WHERE user=$user"; + $results = $this->db->query($query)->fetchAll(); + if ($results) { + return array_map(function($result) { + return array( + 'id' => $this->token->hash($result['id']), + 'name' => $result['name'] . ' ' . $result['specification'], + ); + }, $results); + } + return array(); + } +} \ No newline at end of file diff --git a/backend/User.php b/backend/User.php index 1261cb2..919d9a1 100644 --- a/backend/User.php +++ b/backend/User.php @@ -1,6 +1,7 @@ db = new Db(); $this->token = new Token(); + $this->dictionary = new Dictionary(); } public function logIn ($email, $password) { @@ -46,7 +48,7 @@ class User { if ($insert_user === true) { $new_user_id = $this->db->lastInsertId(); - $token = $this->createDictionary($new_user_id); + $token = $this->dictionary->create($new_user_id); if ($token !== false) { return $token; @@ -56,28 +58,12 @@ class User { return false; } - public function createDictionary ($user) { - $insert_dictionary_query = "INSERT INTO dictionaries (user) VALUES ($user)"; - $insert_dictionary = $this->db->execute($insert_dictionary_query); - - if ($insert_dictionary === true) { - $new_dictionary_id = $this->db->lastInsertId(); - - $insert_linguistics_query = "INSERT INTO dictionary_linguistics (dictionary) VALUES ($new_dictionary_id)"; - $insert_linguistics = $this->db->execute($insert_linguistics_query); - - if ($insert_linguistics === true) { - if ($this->changeCurrentDictionary($user, $new_dictionary_id)) { - $user_data = array( - 'id' => $user, - 'isMember' => $this->hasMembership($user), - 'dictionary' => $new_dictionary_id, - ); - return $this->token->encode($user_data); - } - } + public function createNewDictionary ($token) { + $user_data = $this->token->decode($token); + if ($user_data !== false) { + $id = $user_data->id; + return $this->dictionary->create($id); } - return false; } @@ -90,21 +76,11 @@ class User { return false; } - public function getAllDictionaries ($token) { + public function listAllDictionaryNames ($token) { $user_data = $this->token->decode($token); if ($user_data !== false) { $id = $user_data->id; - $query = "SELECT id, name FROM dictionaries WHERE user=$id"; - $results = $this->db->query($query)->fetchAll(); - if ($results) { - return array_map(function($result) { - return array( - 'id' => $this->token->hash($result['id']), - 'name' => $result['name'], - ); - }, $results); - } - return array(); + return $this->dictionary->getAllNames($id); } return false; } diff --git a/backend/index.php b/backend/index.php index aa7e563..ec05ac4 100644 --- a/backend/index.php +++ b/backend/index.php @@ -55,10 +55,10 @@ switch ($action) { 'error' => true, ), 400); } - case 'get-all-dictionaries': { + case 'get-all-dictionary-names': { if ($token !== false) { $user = new User(); - $all_dictionaries = $user->getAllDictionaries($token); + $all_dictionaries = $user->listAllDictionaryNames($token); if ($all_dictionaries !== false) { return Response::json(array( 'data' => $all_dictionaries,