Move relevant methods from User class to new Dictionary class

This commit is contained in:
Robbie Antenesse 2018-01-04 22:08:18 -07:00
parent c997dabd26
commit cfa29660a8
3 changed files with 72 additions and 36 deletions

60
backend/Dictionary.php Normal file
View File

@ -0,0 +1,60 @@
<?php
require_once('./Db.php');
require_once('./Token.php');
class Dictionary {
private $db;
private $token;
function __construct () {
$this->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();
}
}

View File

@ -1,6 +1,7 @@
<?php <?php
require_once('./Db.php'); require_once('./Db.php');
require_once('./Token.php'); require_once('./Token.php');
require_once('./Dictionary.php');
class User { class User {
private $db; private $db;
@ -8,6 +9,7 @@ class User {
function __construct () { function __construct () {
$this->db = new Db(); $this->db = new Db();
$this->token = new Token(); $this->token = new Token();
$this->dictionary = new Dictionary();
} }
public function logIn ($email, $password) { public function logIn ($email, $password) {
@ -46,7 +48,7 @@ class User {
if ($insert_user === true) { if ($insert_user === true) {
$new_user_id = $this->db->lastInsertId(); $new_user_id = $this->db->lastInsertId();
$token = $this->createDictionary($new_user_id); $token = $this->dictionary->create($new_user_id);
if ($token !== false) { if ($token !== false) {
return $token; return $token;
@ -56,28 +58,12 @@ class User {
return false; return false;
} }
public function createDictionary ($user) { public function createNewDictionary ($token) {
$insert_dictionary_query = "INSERT INTO dictionaries (user) VALUES ($user)"; $user_data = $this->token->decode($token);
$insert_dictionary = $this->db->execute($insert_dictionary_query); if ($user_data !== false) {
$id = $user_data->id;
if ($insert_dictionary === true) { return $this->dictionary->create($id);
$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; return false;
} }
@ -90,21 +76,11 @@ class User {
return false; return false;
} }
public function getAllDictionaries ($token) { public function listAllDictionaryNames ($token) {
$user_data = $this->token->decode($token); $user_data = $this->token->decode($token);
if ($user_data !== false) { if ($user_data !== false) {
$id = $user_data->id; $id = $user_data->id;
$query = "SELECT id, name FROM dictionaries WHERE user=$id"; return $this->dictionary->getAllNames($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 false; return false;
} }

View File

@ -55,10 +55,10 @@ switch ($action) {
'error' => true, 'error' => true,
), 400); ), 400);
} }
case 'get-all-dictionaries': { case 'get-all-dictionary-names': {
if ($token !== false) { if ($token !== false) {
$user = new User(); $user = new User();
$all_dictionaries = $user->getAllDictionaries($token); $all_dictionaries = $user->listAllDictionaryNames($token);
if ($all_dictionaries !== false) { if ($all_dictionaries !== false) {
return Response::json(array( return Response::json(array(
'data' => $all_dictionaries, 'data' => $all_dictionaries,