WIP: more db actions
This commit is contained in:
@@ -10,7 +10,7 @@ pub struct FileDatabase<'a> {
|
|||||||
pub trait WordSource {
|
pub trait WordSource {
|
||||||
fn find_by_word(&self, word: &str) -> Result<Vec<Word>, ()>;
|
fn find_by_word(&self, word: &str) -> Result<Vec<Word>, ()>;
|
||||||
fn insert_word(&mut self, word: Word) -> Result<(), ()>;
|
fn insert_word(&mut self, word: Word) -> Result<(), ()>;
|
||||||
fn remove_word(&self, word: &str) -> Result<(), ()>;
|
fn remove_word(&self, word_id: i32) -> Result<(), ()>;
|
||||||
fn edit_word(&mut self, word: Word) -> Result<(), ()>;
|
fn edit_word(&mut self, word: Word) -> Result<(), ()>;
|
||||||
fn get_all_words(&self) -> Vec<Word>;
|
fn get_all_words(&self) -> Vec<Word>;
|
||||||
}
|
}
|
||||||
@@ -43,19 +43,38 @@ impl<'a> WordSource for FileDatabase<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn insert_word(&mut self, word: Word) -> Result<(), ()> {
|
fn insert_word(&mut self, word: Word) -> Result<(), ()> {
|
||||||
// TODO: insert into db
|
let result = self.connection.execute(
|
||||||
Ok(())
|
"INSERT INTO word (key, description) VALUES (?1, ?2)",
|
||||||
|
(word.0, word.1),
|
||||||
|
);
|
||||||
|
if let Ok(_) = result {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
Err(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn edit_word(&mut self, word: Word) -> Result<(), ()> {
|
fn edit_word(&mut self, word: Word) -> Result<(), ()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove_word(&self, word: &str) -> Result<(), ()> {
|
fn remove_word(&self, word_id: i32) -> Result<(), ()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_all_words(&self) -> Vec<Word> {
|
fn get_all_words(&self) -> Vec<Word> {
|
||||||
vec![]
|
let statement = self.connection.prepare("select key, description from word");
|
||||||
|
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([]).unwrap();
|
||||||
|
while let Some(row) = rows.next().unwrap() {
|
||||||
|
let found_word: String = row.get(0).unwrap();
|
||||||
|
let found_word_description: String = row.get(0).unwrap();
|
||||||
|
word_list.push(Word(found_word, found_word_description));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
word_list
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user