mirror of
				https://github.com/Alamantus/Lexiconga.git
				synced 2025-11-04 10:17:01 +01:00 
			
		
		
		
	Add endpoints and methods for updating dictionaries + words
This commit is contained in:
		
							parent
							
								
									d2336bf9c9
								
							
						
					
					
						commit
						6646bf19d7
					
				
					 3 changed files with 179 additions and 8 deletions
				
			
		| 
						 | 
				
			
			@ -108,6 +108,52 @@ VALUES ($new_dictionary_id, ?, ?)";
 | 
			
		|||
    return false;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public function setDetails ($user, $dictionary, $dictionary_object) {
 | 
			
		||||
    $query1 = "UPDATE dictionaries
 | 
			
		||||
SET name=:name,
 | 
			
		||||
  specification=:specification,
 | 
			
		||||
  description=:description,
 | 
			
		||||
  allow_duplicates=:allow_duplicates,
 | 
			
		||||
  case_sensitive=:case_sensitive,
 | 
			
		||||
  sort_by_definition=:sort_by_definition,
 | 
			
		||||
  is_complete=:is_complete,
 | 
			
		||||
  is_public=:is_public,
 | 
			
		||||
  last_updated=NOW()
 | 
			
		||||
WHERE user=$user AND id=$dictionary";
 | 
			
		||||
 | 
			
		||||
    $result1 = $this->db->execute($query1, array(
 | 
			
		||||
      ':name' => $dictionary_object['name'],
 | 
			
		||||
      ':specification' => $dictionary_object['specification'],
 | 
			
		||||
      ':description' => $dictionary_object['description'],
 | 
			
		||||
      ':allow_duplicates' => $dictionary_object['settings']['allowDuplicates'],
 | 
			
		||||
      ':case_sensitive' => $dictionary_object['settings']['caseSensitive'],
 | 
			
		||||
      ':sort_by_definition' => $dictionary_object['settings']['sortByDefinition'],
 | 
			
		||||
      ':is_complete' => $dictionary_object['settings']['isComplete'],
 | 
			
		||||
      ':is_public' => $dictionary_object['settings']['isPublic'],
 | 
			
		||||
    ));
 | 
			
		||||
    if ($result1->rowCount() > 0) {
 | 
			
		||||
      $linguistics = $dictionary_object['details'];
 | 
			
		||||
      $query2 = "UPDATE dictionary_linguistics
 | 
			
		||||
SET parts_of_speech=:parts_of_speech,
 | 
			
		||||
  phonology=:phonology,
 | 
			
		||||
  orthography_notes=:orthography_notes,
 | 
			
		||||
  grammar_notes=:grammar_notes
 | 
			
		||||
WHERE dictionary=$dictionary";
 | 
			
		||||
 | 
			
		||||
      $result2 = $this->db->execute($query2, array(
 | 
			
		||||
        ':parts_of_speech' => json_encode($dictionary_object['partsOfSpeech']),
 | 
			
		||||
        ':phonology' => json_encode($linguistics['phonology']),
 | 
			
		||||
        ':orthography_notes' => $linguistics['orthographyNotes'],
 | 
			
		||||
        ':grammar_notes' => $linguistics['grammarNotes'],
 | 
			
		||||
      ));
 | 
			
		||||
 | 
			
		||||
      if ($result2->rowCount() > 0) {
 | 
			
		||||
        return true;
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
    return false;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public function getWords ($user, $dictionary) {
 | 
			
		||||
    $query = "SELECT words.* FROM words JOIN dictionaries ON id = dictionary WHERE dictionary=$dictionary AND user=$user";
 | 
			
		||||
    $results = $this->db->query($query)->fetchAll();
 | 
			
		||||
| 
						 | 
				
			
			@ -127,4 +173,31 @@ VALUES ($new_dictionary_id, ?, ?)";
 | 
			
		|||
    }
 | 
			
		||||
    return array();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public function setWords ($dictionary, $words = array()) {
 | 
			
		||||
    $query = 'INSERT INTO words (word_id, name, pronunciation, part_of_speech, definition, details, createdOn) 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 .= 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()';
 | 
			
		||||
    
 | 
			
		||||
    $results = $this->db->execute($query);
 | 
			
		||||
    return $results->rowCount() > 0;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -59,7 +59,10 @@ class User {
 | 
			
		|||
      $id = $user_data->id;
 | 
			
		||||
      $new_dictionary = $this->dictionary->create($id);
 | 
			
		||||
      if ($new_dictionary !== false) {
 | 
			
		||||
        return $this->generateUserToken($id, $new_dictionary);
 | 
			
		||||
        return array(
 | 
			
		||||
            'token' => $this->generateUserToken($id, $new_dictionary),
 | 
			
		||||
            'dictionary' => $this->getCurrentDictionary($token),
 | 
			
		||||
          );
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
    return false;
 | 
			
		||||
| 
						 | 
				
			
			@ -73,7 +76,10 @@ class User {
 | 
			
		|||
      if ($dictionary_id !== false) {
 | 
			
		||||
        $changed_dictionary = $this->dictionary->changeCurrent($id, $dictionary_id);
 | 
			
		||||
        if ($changed_dictionary !== false) {
 | 
			
		||||
          return $this->generateUserToken($id, $changed_dictionary);
 | 
			
		||||
          return array(
 | 
			
		||||
            'token' => $this->generateUserToken($id, $changed_dictionary),
 | 
			
		||||
            'dictionary' => $this->getCurrentDictionary($token),
 | 
			
		||||
          );
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			@ -102,6 +108,38 @@ class User {
 | 
			
		|||
    return false;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public function saveWholeCurrentDictionary ($token, $dictionary_data) {
 | 
			
		||||
    $user_data = $this->token->decode($token);
 | 
			
		||||
    if ($user_data !== false) {
 | 
			
		||||
      $user = $user_data->id;
 | 
			
		||||
      $dictionary = $user_data->dictionary;
 | 
			
		||||
      $details_updated = $this->dictionary->setDetails($user, $dictionary, $dictionary_data['details']);
 | 
			
		||||
      $words_updated = $this->dictionary->setWords($dictionary, $dictionary_data['words']);
 | 
			
		||||
      return $details_updated && $words_updated;
 | 
			
		||||
    }
 | 
			
		||||
    return false;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public function updateCurrentDictionaryDetails ($token, $dictionary_details) {
 | 
			
		||||
    $user_data = $this->token->decode($token);
 | 
			
		||||
    if ($user_data !== false) {
 | 
			
		||||
      $user = $user_data->id;
 | 
			
		||||
      $dictionary = $user_data->dictionary;
 | 
			
		||||
      return $this->dictionary->setDetails($user, $dictionary, $dictionary_details);
 | 
			
		||||
    }
 | 
			
		||||
    return false;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public function updateOrAddWordsToCurrentDictionary ($token, $words) {
 | 
			
		||||
    // Useful even for just one word
 | 
			
		||||
    $user_data = $this->token->decode($token);
 | 
			
		||||
    if ($user_data !== false) {
 | 
			
		||||
      $dictionary = $user_data->dictionary;
 | 
			
		||||
      return $this->dictionary->setWords($dictionary, $words);
 | 
			
		||||
    }
 | 
			
		||||
    return false;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  private function generateUserToken ($user_id, $dictionary_id) {
 | 
			
		||||
    $user_data = array(
 | 
			
		||||
      'id' => intval($user_id),
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -78,10 +78,10 @@ switch ($action) {
 | 
			
		|||
  case 'create-new-dictionary': {
 | 
			
		||||
    if ($token !== false) {
 | 
			
		||||
      $user = new User();
 | 
			
		||||
      $new_token = $user->createNewDictionary($token);
 | 
			
		||||
      if ($new_token !== false) {
 | 
			
		||||
      $new_data = $user->createNewDictionary($token);
 | 
			
		||||
      if ($new_data !== false) {
 | 
			
		||||
        return Response::json(array(
 | 
			
		||||
          'data' => $new_token,
 | 
			
		||||
          'data' => $new_data,
 | 
			
		||||
          'error' => false,
 | 
			
		||||
        ), 200);
 | 
			
		||||
      }
 | 
			
		||||
| 
						 | 
				
			
			@ -98,10 +98,10 @@ switch ($action) {
 | 
			
		|||
  case 'change-dictionary': {
 | 
			
		||||
    if ($token !== false && isset($request['dictionary'])) {
 | 
			
		||||
      $user = new User();
 | 
			
		||||
      $new_token = $user->changeCurrentDictionary($token, $request['dictionary']);
 | 
			
		||||
      if ($new_token !== false) {
 | 
			
		||||
      $new_data = $user->changeCurrentDictionary($token, $request['dictionary']);
 | 
			
		||||
      if ($new_data !== false) {
 | 
			
		||||
        return Response::json(array(
 | 
			
		||||
          'data' => $new_token,
 | 
			
		||||
          'data' => $new_data,
 | 
			
		||||
          'error' => false,
 | 
			
		||||
        ), 200);
 | 
			
		||||
      }
 | 
			
		||||
| 
						 | 
				
			
			@ -135,6 +135,66 @@ switch ($action) {
 | 
			
		|||
      'error' => true,
 | 
			
		||||
    ), 400);
 | 
			
		||||
  }
 | 
			
		||||
  case 'set-whole-current-dictionary': {
 | 
			
		||||
    if ($token !== false && isset($request['dictionary'])) {
 | 
			
		||||
      $user = new User();
 | 
			
		||||
      $dictionary_data = $user->saveWholeCurrentDictionary($token, $request['dictionary']);
 | 
			
		||||
      if ($dictionary_data !== false) {
 | 
			
		||||
        return Response::json(array(
 | 
			
		||||
          'data' => 'Updated successfully',
 | 
			
		||||
          'error' => false,
 | 
			
		||||
        ), 200);
 | 
			
		||||
      }
 | 
			
		||||
      return Response::json(array(
 | 
			
		||||
        'data' => 'Could not set dictionary: invalid token',
 | 
			
		||||
        'error' => true,
 | 
			
		||||
      ), 401);
 | 
			
		||||
    }
 | 
			
		||||
    return Response::json(array(
 | 
			
		||||
      'data' => 'Could not set dictionary: required data missing',
 | 
			
		||||
      'error' => true,
 | 
			
		||||
    ), 400);
 | 
			
		||||
  }
 | 
			
		||||
  case 'set-dictionary-details': {
 | 
			
		||||
    if ($token !== false && isset($request['details'])) {
 | 
			
		||||
      $user = new User();
 | 
			
		||||
      $update_details_success = $user->updateCurrentDictionaryDetails($token, $request['details']);
 | 
			
		||||
      if ($update_details_success !== false) {
 | 
			
		||||
        return Response::json(array(
 | 
			
		||||
          'data' => 'Updated successfully',
 | 
			
		||||
          'error' => false,
 | 
			
		||||
        ), 200);
 | 
			
		||||
      }
 | 
			
		||||
      return Response::json(array(
 | 
			
		||||
        'data' => 'Could not set dictionary: invalid token',
 | 
			
		||||
        'error' => true,
 | 
			
		||||
      ), 401);
 | 
			
		||||
    }
 | 
			
		||||
    return Response::json(array(
 | 
			
		||||
      'data' => 'Could not set dictionary: required data missing',
 | 
			
		||||
      'error' => true,
 | 
			
		||||
    ), 400);
 | 
			
		||||
  }
 | 
			
		||||
  case 'set-dictionary-words': {
 | 
			
		||||
    if ($token !== false && isset($request['words'])) {
 | 
			
		||||
      $user = new User();
 | 
			
		||||
      $update_words_success = $user->updateOrAddWordsToCurrentDictionary($token, $request['words']);
 | 
			
		||||
      if ($update_words_success !== false) {
 | 
			
		||||
        return Response::json(array(
 | 
			
		||||
          'data' => 'Updated successfully',
 | 
			
		||||
          'error' => false,
 | 
			
		||||
        ), 200);
 | 
			
		||||
      }
 | 
			
		||||
      return Response::json(array(
 | 
			
		||||
        'data' => 'Could not set dictionary: invalid token',
 | 
			
		||||
        'error' => true,
 | 
			
		||||
      ), 401);
 | 
			
		||||
    }
 | 
			
		||||
    return Response::json(array(
 | 
			
		||||
      'data' => 'Could not set dictionary: required data missing',
 | 
			
		||||
      'error' => true,
 | 
			
		||||
    ), 400);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  default: {
 | 
			
		||||
    return Response::html('Hi!');
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue