Enable words to be deleted.

This commit is contained in:
Robbie Antenesse 2018-06-23 14:39:22 -06:00
parent 662d133ca7
commit 744506bdc3
4 changed files with 67 additions and 3 deletions

View File

@ -189,10 +189,12 @@ WHERE dictionary=$dictionary";
return array();
}
public function setWords ($dictionary, $words = array()) {
public function setWords ($user, $dictionary, $words = array()) {
$query = 'INSERT INTO words (dictionary, word_id, name, pronunciation, part_of_speech, definition, details, last_updated, created_on) VALUES ';
$params = array();
$word_ids = array();
foreach($words as $word) {
$word_ids[] = $word['id'];
$query .= "(?, ?, ?, ?, ?, ?, ?, ?, ?), ";
$params[] = $dictionary;
$params[] = $word['id'];
@ -213,6 +215,29 @@ details=VALUES(details),
last_updated=VALUES(last_updated)';
$results = $this->db->execute($query, $params);
if ($results) {
$database_words = $this->getWords($user, $dictionary);
$database_ids = array_map(function($database_word) { return $database_word['id']; }, $database_words);
$words_to_delete = array_filter($database_ids, function($database_id) use($word_ids) { return !in_array($database_id, $word_ids); });
if ($words_to_delete) {
$delete_results = $this->deleteWords($dictionary, $words_to_delete);
return $delete_results;
}
}
return $results;
}
public function deleteWords ($dictionary, $word_ids) {
$query = 'DELETE FROM words WHERE dictionary=? AND word_id IN (';
$params = array($dictionary);
foreach($word_ids as $word_id) {
$query .= '?, ';
$params[] = $word_id;
}
$query = trim($query, ', ') . ')';
$results = $this->db->execute($query, $params);
return $results;
}
}

View File

@ -203,8 +203,23 @@ VALUES (?, ?, ?, ?, ?, ?)';
$user_data = $this->token->decode($token);
if ($user_data !== false) {
$dictionary = $user_data->dictionary;
$updated_words = $this->dictionary->setWords($dictionary, $words);
if ($updated_words > 0) {
$user = $user_data->id;
$updated_words = $this->dictionary->setWords($user, $dictionary, $words);
if ($updated_words) {
return true;
}
}
return false;
}
public function deleteWordFromCurrentDictionary ($token, $word_id) {
// Useful even for just one word
$user_data = $this->token->decode($token);
if ($user_data !== false) {
$dictionary = $user_data->dictionary;
$user = $user_data->id;
$deleted_word = $this->dictionary->deleteWords($dictionary, array($word_id));
if ($deleted_word) {
return true;
}
}

View File

@ -244,6 +244,26 @@ switch ($action) {
'error' => true,
), 400);
}
case 'delete-word': {
if ($token !== false && isset($request['word'])) {
$user = new User();
$delete_word_success = $user->deleteWordFromCurrentDictionary($token, $request['word']);
if ($delete_word_success !== false) {
return Response::json(array(
'data' => 'Deleted successfully',
'error' => false,
), 200);
}
return Response::json(array(
'data' => 'Could not delete word: invalid token',
'error' => true,
), 401);
}
return Response::json(array(
'data' => 'Could not delete word: required data missing',
'error' => true,
), 400);
}
default: {
return Response::html('Hi!');

View File

@ -73,6 +73,10 @@ export class Word {
return wordDb.words.delete(wordId)
.then(() => {
console.log('Word deleted successfully');
request('delete-word', {
token: store.get('LexicongaToken'),
word: wordId,
}, response => console.log(response));
})
.catch(error => {
console.error(error);