feat: final DB and trait structure
This commit is contained in:
@@ -48,10 +48,12 @@ impl FileDatabase {
|
||||
|
||||
pub trait WordSource {
|
||||
fn find_by_word(&self, word: &str) -> Result<Vec<Word>, ()>;
|
||||
fn find_by_word_and_lang(&self, word: &str, lang: &str) -> Result<Vec<Word>, ()>;
|
||||
fn insert_word(&self, word: Word) -> Result<(), ()>;
|
||||
fn remove_word(&self, word_key: &str) -> Result<(), ()>;
|
||||
fn edit_word(&self, word: Word) -> Result<Word, ()>;
|
||||
fn get_all_words(&self) -> Vec<Word>;
|
||||
fn get_all_words_by_lang(&self, lang: &str) -> Vec<Word>;
|
||||
fn normalize_word_key(word_key: &str) -> String;
|
||||
fn normalize_lang_key(word_key: &str) -> String;
|
||||
}
|
||||
@@ -91,6 +93,32 @@ impl WordSource for FileDatabase {
|
||||
Ok(word_list)
|
||||
}
|
||||
|
||||
fn find_by_word_and_lang(&self, word: &str, lang: &str) -> Result<Vec<Word>, ()> {
|
||||
let statement = self
|
||||
.connection
|
||||
.prepare("SELECT key, description, lang FROM word where lang = ?1");
|
||||
let mut word_list: Vec<Word> = vec![];
|
||||
|
||||
if let Ok(mut query) = statement {
|
||||
let mut rows = query.query([lang]).unwrap();
|
||||
while let Some(row) = rows.next().unwrap() {
|
||||
let found_word: String = row.get(0).unwrap();
|
||||
if fuzzy_compare(word, &found_word) {
|
||||
let found_word_description: String = row.get(1).unwrap();
|
||||
let found_word_lang: String = row.get(2).unwrap();
|
||||
word_list.push(Word {
|
||||
key: found_word,
|
||||
description: found_word_description,
|
||||
lang: found_word_lang,
|
||||
});
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return Err(());
|
||||
}
|
||||
Ok(word_list)
|
||||
}
|
||||
|
||||
fn insert_word(&self, word: Word) -> Result<(), ()> {
|
||||
let result = self.connection.execute(
|
||||
"INSERT INTO word (key, description, lang) VALUES (?1, ?2, ?3)",
|
||||
@@ -166,6 +194,30 @@ impl WordSource for FileDatabase {
|
||||
word_list
|
||||
}
|
||||
|
||||
fn get_all_words_by_lang(&self, lang: &str) -> Vec<Word> {
|
||||
let statement = self
|
||||
.connection
|
||||
.prepare("select key, description, lang from word where lang = ?1");
|
||||
let mut word_list: Vec<Word> = vec![];
|
||||
|
||||
// TODO: make this pretty with functional programming
|
||||
if let Ok(mut query) = statement {
|
||||
let mut rows = query.query([lang]).unwrap();
|
||||
while let Some(row) = rows.next().unwrap() {
|
||||
let found_word: String = row.get(0).unwrap();
|
||||
let found_word_description: String = row.get(1).unwrap();
|
||||
let found_word_lang: String = row.get(2).unwrap();
|
||||
word_list.push(Word {
|
||||
key: found_word,
|
||||
description: found_word_description,
|
||||
lang: found_word_lang,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
word_list
|
||||
}
|
||||
|
||||
fn normalize_word_key(word_key: &str) -> String {
|
||||
// TODO: real normalization, not just lowercase, but keep in mind it must be kept as the
|
||||
// final and real word -> Maybe full refactor to include a word id
|
||||
|
||||
Reference in New Issue
Block a user