Test and prepare response for inserting/updating words.

This commit is contained in:
Robbie Antenesse 2018-01-07 12:59:40 -07:00
parent f2f8379f9d
commit 691346c4ba
3 changed files with 37 additions and 26 deletions

View File

@ -20,4 +20,8 @@ class Db {
public function lastInsertId () { public function lastInsertId () {
return $this->dbh->lastInsertId(); return $this->dbh->lastInsertId();
} }
public function errorInfo () {
return $this->dbh->errorInfo();
}
} }

View File

@ -101,8 +101,8 @@ VALUES ($new_dictionary_id, ?, ?)";
'isComplete' => $result['is_complete'] === '1' ? true : false, 'isComplete' => $result['is_complete'] === '1' ? true : false,
'isPublic' => $result['is_public'] === '1' ? true : false, 'isPublic' => $result['is_public'] === '1' ? true : false,
), ),
'lastUpdated' => $result['last_updated'], 'lastUpdated' => is_null($result['last_updated']) ? null : strtotime($result['last_updated']),
'createdOn' => $result['created_on'], 'createdOn' => strtotime($result['created_on']),
); );
} }
return false; return false;
@ -121,7 +121,7 @@ SET name=:name,
last_updated=NOW() last_updated=NOW()
WHERE user=$user AND id=$dictionary"; WHERE user=$user AND id=$dictionary";
$result1 = $this->db->execute($query1, array( $result1 = $this->db->query($query1, array(
':name' => $dictionary_object['name'], ':name' => $dictionary_object['name'],
':specification' => $dictionary_object['specification'], ':specification' => $dictionary_object['specification'],
':description' => $dictionary_object['description'], ':description' => $dictionary_object['description'],
@ -140,7 +140,7 @@ SET parts_of_speech=:parts_of_speech,
grammar_notes=:grammar_notes grammar_notes=:grammar_notes
WHERE dictionary=$dictionary"; WHERE dictionary=$dictionary";
$result2 = $this->db->execute($query2, array( $result2 = $this->db->query($query2, array(
':parts_of_speech' => json_encode($dictionary_object['partsOfSpeech']), ':parts_of_speech' => json_encode($dictionary_object['partsOfSpeech']),
':phonology' => json_encode($linguistics['phonology']), ':phonology' => json_encode($linguistics['phonology']),
':orthography_notes' => $linguistics['orthographyNotes'], ':orthography_notes' => $linguistics['orthographyNotes'],
@ -166,8 +166,8 @@ WHERE dictionary=$dictionary";
'partOfSpeech' => $result['part_of_speech'], 'partOfSpeech' => $result['part_of_speech'],
'definition' => $result['definition'], 'definition' => $result['definition'],
'details' => $result['details'], 'details' => $result['details'],
'lastUpdated' => $result['last_updated'], 'lastUpdated' => is_null($result['last_updated']) ? null : strtotime($result['last_updated']),
'createdOn' => $result['created_on'], 'createdOn' => strtotime($result['created_on']),
); );
}, $results); }, $results);
} }
@ -175,29 +175,28 @@ WHERE dictionary=$dictionary";
} }
public function setWords ($dictionary, $words = array()) { 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(); $params = array();
foreach($words as $word) { foreach($words as $word) {
$query .= "(?, ?, ?, ?, ?, ?, NOW()), "; $query .= "(?, ?, ?, ?, ?, ?, FROM_UNIXTIME(?), FROM_UNIXTIME(?)), ";
array_push( $params[] = $word['id'];
$params, $params[] = $word['name'];
$word['id'], $params[] = $word['pronunciation'];
$word['name'], $params[] = $word['partOfSpeech'];
$word['pronunciation'], $params[] = $word['definition'];
$word['partOfSpeech'], $params[] = $word['details'];
$word['definition'], $params[] = is_null($word['lastUpdated']) ? $word['createdOn'] : $word['lastUpdated'];
$word['details'] $params[] = $word['createdOn'];
);
} }
$query .= trim($query, ', ') . ' ON DUPLICATE KEY UPDATE $query = trim($query, ', ') . ' ON DUPLICATE KEY UPDATE
name=VALUES(name), name=VALUES(name),
pronunciation=VALUE(pronunciation), pronunciation=VALUES(pronunciation),
part_of_speech=VALUE(part_of_speech), part_of_speech=VALUES(part_of_speech),
definition=VALUE(definition), definition=VALUES(definition),
details=VALUE(details), details=VALUES(details),
last_updated=NOW()'; last_updated=VALUES(last_updated)';
$results = $this->db->execute($query); $results = $this->db->query($query, $params);
return $results->rowCount() > 0; return $results->rowCount();
} }
} }

View File

@ -135,7 +135,15 @@ class User {
$user_data = $this->token->decode($token); $user_data = $this->token->decode($token);
if ($user_data !== false) { if ($user_data !== false) {
$dictionary = $user_data->dictionary; $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; return false;
} }