From 691346c4ba8b49ceee8fed37e3c47a248631c2c5 Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Sun, 7 Jan 2018 12:59:40 -0700 Subject: [PATCH] Test and prepare response for inserting/updating words. --- backend/Db.php | 4 ++++ backend/Dictionary.php | 49 +++++++++++++++++++++--------------------- backend/User.php | 10 ++++++++- 3 files changed, 37 insertions(+), 26 deletions(-) diff --git a/backend/Db.php b/backend/Db.php index babbaab..67a12cb 100644 --- a/backend/Db.php +++ b/backend/Db.php @@ -20,4 +20,8 @@ class Db { public function lastInsertId () { return $this->dbh->lastInsertId(); } + + public function errorInfo () { + return $this->dbh->errorInfo(); + } } \ No newline at end of file diff --git a/backend/Dictionary.php b/backend/Dictionary.php index ea364a1..f2e0db9 100644 --- a/backend/Dictionary.php +++ b/backend/Dictionary.php @@ -101,8 +101,8 @@ VALUES ($new_dictionary_id, ?, ?)"; 'isComplete' => $result['is_complete'] === '1' ? true : false, 'isPublic' => $result['is_public'] === '1' ? true : false, ), - 'lastUpdated' => $result['last_updated'], - 'createdOn' => $result['created_on'], + 'lastUpdated' => is_null($result['last_updated']) ? null : strtotime($result['last_updated']), + 'createdOn' => strtotime($result['created_on']), ); } return false; @@ -121,7 +121,7 @@ SET name=:name, last_updated=NOW() WHERE user=$user AND id=$dictionary"; - $result1 = $this->db->execute($query1, array( + $result1 = $this->db->query($query1, array( ':name' => $dictionary_object['name'], ':specification' => $dictionary_object['specification'], ':description' => $dictionary_object['description'], @@ -140,7 +140,7 @@ SET parts_of_speech=:parts_of_speech, grammar_notes=:grammar_notes WHERE dictionary=$dictionary"; - $result2 = $this->db->execute($query2, array( + $result2 = $this->db->query($query2, array( ':parts_of_speech' => json_encode($dictionary_object['partsOfSpeech']), ':phonology' => json_encode($linguistics['phonology']), ':orthography_notes' => $linguistics['orthographyNotes'], @@ -166,8 +166,8 @@ WHERE dictionary=$dictionary"; 'partOfSpeech' => $result['part_of_speech'], 'definition' => $result['definition'], 'details' => $result['details'], - 'lastUpdated' => $result['last_updated'], - 'createdOn' => $result['created_on'], + 'lastUpdated' => is_null($result['last_updated']) ? null : strtotime($result['last_updated']), + 'createdOn' => strtotime($result['created_on']), ); }, $results); } @@ -175,29 +175,28 @@ WHERE dictionary=$dictionary"; } public function setWords ($dictionary, $words = array()) { - $query = 'INSERT INTO words (word_id, name, pronunciation, part_of_speech, definition, details, createdOn) VALUES '; + $query = 'INSERT INTO words (word_id, name, pronunciation, part_of_speech, definition, details, last_updated, created_on) VALUES '; $params = array(); foreach($words as $word) { - $query .= "(?, ?, ?, ?, ?, ?, NOW()), "; - array_push( - $params, - $word['id'], - $word['name'], - $word['pronunciation'], - $word['partOfSpeech'], - $word['definition'], - $word['details'] - ); + $query .= "(?, ?, ?, ?, ?, ?, FROM_UNIXTIME(?), FROM_UNIXTIME(?)), "; + $params[] = $word['id']; + $params[] = $word['name']; + $params[] = $word['pronunciation']; + $params[] = $word['partOfSpeech']; + $params[] = $word['definition']; + $params[] = $word['details']; + $params[] = is_null($word['lastUpdated']) ? $word['createdOn'] : $word['lastUpdated']; + $params[] = $word['createdOn']; } - $query .= trim($query, ', ') . ' ON DUPLICATE KEY UPDATE + $query = trim($query, ', ') . ' ON DUPLICATE KEY UPDATE name=VALUES(name), -pronunciation=VALUE(pronunciation), -part_of_speech=VALUE(part_of_speech), -definition=VALUE(definition), -details=VALUE(details), -last_updated=NOW()'; +pronunciation=VALUES(pronunciation), +part_of_speech=VALUES(part_of_speech), +definition=VALUES(definition), +details=VALUES(details), +last_updated=VALUES(last_updated)'; - $results = $this->db->execute($query); - return $results->rowCount() > 0; + $results = $this->db->query($query, $params); + return $results->rowCount(); } } \ No newline at end of file diff --git a/backend/User.php b/backend/User.php index 7e9c28c..7df9c39 100644 --- a/backend/User.php +++ b/backend/User.php @@ -135,7 +135,15 @@ class User { $user_data = $this->token->decode($token); if ($user_data !== false) { $dictionary = $user_data->dictionary; - return $this->dictionary->setWords($dictionary, $words); + $updated_words = $this->dictionary->setWords($dictionary, $words); + if ($updated_words > 0) { + if ($updated_words === count($words)) { + return true; + } else if ($updated_words < count($words)) { + // TODO: Handle this + return 'mostly'; + } + } } return false; }