mirror of
https://github.com/Alamantus/Lexiconga.git
synced 2025-06-06 01:06:36 +02:00
Add get-current-dictionary endpoint and retrieve them correctly
This commit is contained in:
parent
ffca3c9fcc
commit
d2336bf9c9
3 changed files with 50 additions and 6 deletions
|
@ -5,9 +5,25 @@ require_once('./Token.php');
|
||||||
class Dictionary {
|
class Dictionary {
|
||||||
private $db;
|
private $db;
|
||||||
private $token;
|
private $token;
|
||||||
|
private $defaults;
|
||||||
function __construct () {
|
function __construct () {
|
||||||
$this->db = new Db();
|
$this->db = new Db();
|
||||||
$this->token = new Token();
|
$this->token = new Token();
|
||||||
|
|
||||||
|
$this->defaults = array(
|
||||||
|
'partsOfSpeech' => array("Noun","Adjective","Verb"),
|
||||||
|
'phonology'=> array(
|
||||||
|
'consonants' => array(),
|
||||||
|
'vowels' => array(),
|
||||||
|
'blends' => array(),
|
||||||
|
'phonotactics' => array(
|
||||||
|
'onset' => array(),
|
||||||
|
'nucleus' => array(),
|
||||||
|
'coda' => array(),
|
||||||
|
'exceptions' => '',
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function create ($user) {
|
public function create ($user) {
|
||||||
|
@ -17,8 +33,12 @@ class Dictionary {
|
||||||
if ($insert_dictionary === true) {
|
if ($insert_dictionary === true) {
|
||||||
$new_dictionary_id = $this->db->lastInsertId();
|
$new_dictionary_id = $this->db->lastInsertId();
|
||||||
|
|
||||||
$insert_linguistics_query = "INSERT INTO dictionary_linguistics (dictionary) VALUES ($new_dictionary_id)";
|
$insert_linguistics_query = "INSERT INTO dictionary_linguistics (dictionary, partsOfSpeech, phonology)
|
||||||
$insert_linguistics = $this->db->execute($insert_linguistics_query);
|
VALUES ($new_dictionary_id, ?, ?)";
|
||||||
|
$insert_linguistics = $this->db->execute($insert_linguistics_query, array(
|
||||||
|
json_encode($this->defaults['partsOfSpeech']),
|
||||||
|
json_encode($this->defaults['phonotactics']),
|
||||||
|
));
|
||||||
|
|
||||||
if ($insert_linguistics === true) {
|
if ($insert_linguistics === true) {
|
||||||
return $this->changeCurrent($user, $new_dictionary_id);
|
return $this->changeCurrent($user, $new_dictionary_id);
|
||||||
|
@ -55,14 +75,18 @@ class Dictionary {
|
||||||
$query = "SELECT * FROM dictionaries JOIN dictionary_linguistics ON dictionary = id WHERE user=$user AND id=$dictionary";
|
$query = "SELECT * FROM dictionaries JOIN dictionary_linguistics ON dictionary = id WHERE user=$user AND id=$dictionary";
|
||||||
$result = $this->db->query($query)->fetch();
|
$result = $this->db->query($query)->fetch();
|
||||||
if ($result) {
|
if ($result) {
|
||||||
|
// Default json values in case they are somehow not created by front end first
|
||||||
|
$partsOfSpeech = $result['parts_of_speech'] !== '' ? json_decode($result['parts_of_speech']) : $this->defaults['partsOfSpeech'];
|
||||||
|
$phonology = $result['phonology'] !== '' ? json_decode($result['phonology']) : $this->defaults['phonology'];
|
||||||
|
|
||||||
return array(
|
return array(
|
||||||
'id' => $this->token->hash($result['id']),
|
'id' => $this->token->hash($result['id']),
|
||||||
'name' => $result['name'],
|
'name' => $result['name'],
|
||||||
'specification' => $result['specification'],
|
'specification' => $result['specification'],
|
||||||
'description' => $result['description'],
|
'description' => $result['description'],
|
||||||
'partsOfSpeech' => json_decode($result['parts_of_speech']),
|
'partsOfSpeech' => $partsOfSpeech,
|
||||||
'details' => array(
|
'details' => array(
|
||||||
'phonology' => json_decode($result['phonology']),
|
'phonology' => $phonology,
|
||||||
'orthography' => array(
|
'orthography' => array(
|
||||||
'notes' => $result['orthography_notes'],
|
'notes' => $result['orthography_notes'],
|
||||||
),
|
),
|
||||||
|
@ -101,6 +125,6 @@ class Dictionary {
|
||||||
);
|
);
|
||||||
}, $results);
|
}, $results);
|
||||||
}
|
}
|
||||||
return false;
|
return array();
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -93,7 +93,7 @@ class User {
|
||||||
$user_data = $this->token->decode($token);
|
$user_data = $this->token->decode($token);
|
||||||
if ($user_data !== false) {
|
if ($user_data !== false) {
|
||||||
$user = $user_data->id;
|
$user = $user_data->id;
|
||||||
$dictionary = $user_data->current_dictionary;
|
$dictionary = $user_data->dictionary;
|
||||||
return array(
|
return array(
|
||||||
'details' => $this->dictionary->getDetails($user, $dictionary),
|
'details' => $this->dictionary->getDetails($user, $dictionary),
|
||||||
'words' => $this->dictionary->getWords($user, $dictionary),
|
'words' => $this->dictionary->getWords($user, $dictionary),
|
||||||
|
|
|
@ -115,6 +115,26 @@ switch ($action) {
|
||||||
'error' => true,
|
'error' => true,
|
||||||
), 400);
|
), 400);
|
||||||
}
|
}
|
||||||
|
case 'get-current-dictionary': {
|
||||||
|
if ($token !== false) {
|
||||||
|
$user = new User();
|
||||||
|
$dictionary_data = $user->getCurrentDictionary($token);
|
||||||
|
if ($dictionary_data !== false) {
|
||||||
|
return Response::json(array(
|
||||||
|
'data' => $dictionary_data,
|
||||||
|
'error' => false,
|
||||||
|
), 200);
|
||||||
|
}
|
||||||
|
return Response::json(array(
|
||||||
|
'data' => 'Could not get dictionary: invalid token',
|
||||||
|
'error' => true,
|
||||||
|
), 401);
|
||||||
|
}
|
||||||
|
return Response::json(array(
|
||||||
|
'data' => 'Could not get dictionary: no token provided',
|
||||||
|
'error' => true,
|
||||||
|
), 400);
|
||||||
|
}
|
||||||
|
|
||||||
default: {
|
default: {
|
||||||
return Response::html('Hi!');
|
return Response::html('Hi!');
|
||||||
|
|
Loading…
Add table
Reference in a new issue