Move relevant methods from User class to new Dictionary class
This commit is contained in:
parent
c997dabd26
commit
cfa29660a8
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
<?php
|
||||
require_once('./Db.php');
|
||||
require_once('./Token.php');
|
||||
require_once('./Dictionary.php');
|
||||
|
||||
class User {
|
||||
private $db;
|
||||
|
@ -8,6 +9,7 @@ class User {
|
|||
function __construct () {
|
||||
$this->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;
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in New Issue