feat: basic crud done with 0 visuals
This commit is contained in:
@@ -1,30 +1,65 @@
|
||||
use rusqlite::Connection;
|
||||
use rusqlite::{Connection, params};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Word(String, String);
|
||||
pub struct Word(pub String, pub String);
|
||||
|
||||
pub struct FileDatabase<'a> {
|
||||
pub connection: &'a Connection,
|
||||
pub struct FileDatabase {
|
||||
connection: Connection,
|
||||
}
|
||||
|
||||
impl FileDatabase {
|
||||
pub fn new(path: &str) -> Result<Self, String> {
|
||||
let connection_result = Connection::open(path);
|
||||
match connection_result {
|
||||
Ok(connection) => {
|
||||
let file_database = Self {
|
||||
connection: connection,
|
||||
};
|
||||
file_database.init();
|
||||
return Ok(file_database);
|
||||
}
|
||||
Err(error) => {
|
||||
if let Some(error_code) = error.sqlite_error_code() {
|
||||
return Err(format!("{:?}", error_code));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Err(format!("Error while accessing the database file."));
|
||||
}
|
||||
|
||||
fn init(&self) {
|
||||
self.connection
|
||||
.execute(
|
||||
"CREATE TABLE IF NOT EXISTS word (
|
||||
key TEXT PRIMARY KEY,
|
||||
description TEXT NOT NULL
|
||||
)",
|
||||
(), // empty list of parameters.
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
pub trait WordSource {
|
||||
fn find_by_word(&self, word: &str) -> Result<Vec<Word>, ()>;
|
||||
fn insert_word(&mut self, word: Word) -> Result<(), ()>;
|
||||
fn remove_word(&self, word_id: i32) -> Result<(), ()>;
|
||||
fn edit_word(&mut self, word: Word) -> Result<(), ()>;
|
||||
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 normalize_word_key(word_key: &str) -> String;
|
||||
}
|
||||
|
||||
fn fuzzy_compare(a: &str, b: &str) -> bool {
|
||||
if a.eq(b) {
|
||||
if a.eq(b) || a.contains(b) || b.contains(a) {
|
||||
return true;
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
impl<'a> WordSource for FileDatabase<'a> {
|
||||
impl WordSource for FileDatabase {
|
||||
fn find_by_word(&self, word: &str) -> Result<Vec<Word>, ()> {
|
||||
let statement = self.connection.prepare("select key, description from word");
|
||||
let statement = self.connection.prepare("SELECT key, description FROM word");
|
||||
let mut word_list: Vec<Word> = vec![];
|
||||
|
||||
if let Ok(mut query) = statement {
|
||||
@@ -32,7 +67,7 @@ impl<'a> WordSource for FileDatabase<'a> {
|
||||
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(0).unwrap();
|
||||
let found_word_description: String = row.get(1).unwrap();
|
||||
word_list.push(Word(found_word, found_word_description));
|
||||
}
|
||||
}
|
||||
@@ -42,23 +77,47 @@ impl<'a> WordSource for FileDatabase<'a> {
|
||||
Ok(word_list)
|
||||
}
|
||||
|
||||
fn insert_word(&mut self, word: Word) -> Result<(), ()> {
|
||||
fn insert_word(&self, word: Word) -> Result<(), ()> {
|
||||
let result = self.connection.execute(
|
||||
"INSERT INTO word (key, description) VALUES (?1, ?2)",
|
||||
(word.0, word.1),
|
||||
(&Self::normalize_word_key(&word.0), &word.1),
|
||||
);
|
||||
if let Ok(_) = result {
|
||||
return Ok(());
|
||||
if let Ok(count) = result {
|
||||
if count > 0 {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
Err(())
|
||||
}
|
||||
|
||||
fn edit_word(&mut self, word: Word) -> Result<(), ()> {
|
||||
Ok(())
|
||||
fn edit_word(&self, word: Word) -> Result<Word, ()> {
|
||||
let result = self.connection.execute(
|
||||
"UPDATE word SET key = ?1, description = ?2 WHERE key = ?1",
|
||||
(&Self::normalize_word_key(&word.0), &word.1),
|
||||
);
|
||||
match result {
|
||||
Ok(_) => {
|
||||
return Ok(word);
|
||||
}
|
||||
Err(_) => return Err(()),
|
||||
}
|
||||
}
|
||||
|
||||
fn remove_word(&self, word_id: i32) -> Result<(), ()> {
|
||||
Ok(())
|
||||
fn remove_word(&self, word_key: &str) -> Result<(), ()> {
|
||||
let result = self.connection.execute(
|
||||
"DELETE FROM word WHERE key = ?1",
|
||||
params![Self::normalize_word_key(word_key)],
|
||||
);
|
||||
match result {
|
||||
Ok(response) => {
|
||||
if response > 0 {
|
||||
return Ok(());
|
||||
} else {
|
||||
return Err(());
|
||||
}
|
||||
}
|
||||
Err(_) => return Err(()),
|
||||
}
|
||||
}
|
||||
|
||||
fn get_all_words(&self) -> Vec<Word> {
|
||||
@@ -70,11 +129,17 @@ impl<'a> WordSource for FileDatabase<'a> {
|
||||
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();
|
||||
let found_word_description: String = row.get(1).unwrap();
|
||||
word_list.push(Word(found_word, found_word_description));
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
word_key.to_lowercase().to_string()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user