Add get-current-dictionary endpoint and retrieve them correctly

This commit is contained in:
Robbie Antenesse 2018-01-06 10:51:19 -07:00
parent ffca3c9fcc
commit d2336bf9c9
3 changed files with 50 additions and 6 deletions

View File

@ -5,9 +5,25 @@ require_once('./Token.php');
class Dictionary {
private $db;
private $token;
private $defaults;
function __construct () {
$this->db = new Db();
$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) {
@ -17,8 +33,12 @@ class Dictionary {
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);
$insert_linguistics_query = "INSERT INTO dictionary_linguistics (dictionary, partsOfSpeech, phonology)
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) {
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";
$result = $this->db->query($query)->fetch();
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(
'id' => $this->token->hash($result['id']),
'name' => $result['name'],
'specification' => $result['specification'],
'description' => $result['description'],
'partsOfSpeech' => json_decode($result['parts_of_speech']),
'partsOfSpeech' => $partsOfSpeech,
'details' => array(
'phonology' => json_decode($result['phonology']),
'phonology' => $phonology,
'orthography' => array(
'notes' => $result['orthography_notes'],
),
@ -101,6 +125,6 @@ class Dictionary {
);
}, $results);
}
return false;
return array();
}
}

View File

@ -93,7 +93,7 @@ class User {
$user_data = $this->token->decode($token);
if ($user_data !== false) {
$user = $user_data->id;
$dictionary = $user_data->current_dictionary;
$dictionary = $user_data->dictionary;
return array(
'details' => $this->dictionary->getDetails($user, $dictionary),
'words' => $this->dictionary->getWords($user, $dictionary),

View File

@ -115,6 +115,26 @@ switch ($action) {
'error' => true,
), 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: {
return Response::html('Hi!');