diff options
author | LakoLiu <99976966+lakoliu@users.noreply.github.com> | 2023-10-13 09:26:09 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-13 09:26:09 +0200 |
commit | 03b910060da4171209dcc834f0065a0fbc038224 (patch) | |
tree | 2ec472aeebd7d12d9c9ceb43c73122e8a61f5aca /src/database.rs | |
parent | 28a73d4af630398cfa0818bdefa57e50f76dc4f4 (diff) | |
parent | 14ae87d469555af8301356f99a11c2254edee1a4 (diff) | |
download | Furtherance-03b910060da4171209dcc834f0065a0fbc038224.tar.zst |
Merge pull request #118 from jlledom/autocomplete
Implement task name autocompletion
Diffstat (limited to 'src/database.rs')
-rw-r--r-- | src/database.rs | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/database.rs b/src/database.rs index 7050cbb..2f97d2d 100644 --- a/src/database.rs +++ b/src/database.rs @@ -38,6 +38,12 @@ pub struct Task { pub tags: String, } +impl ToString for Task { + fn to_string(&self) -> String { + format!("{} #{}", self.task_name, self.tags) + } +} + #[derive( Debug, Clone, @@ -343,6 +349,35 @@ pub fn get_list_by_id(id_list: Vec<i32>) -> Result<Vec<Task>, rusqlite::Error> { Ok(tasks_vec) } +pub fn get_list_by_name_and_tags(task_name: String, tag_list: Vec<String>) -> Result<Vec<Task>, rusqlite::Error> { + let conn = Connection::open(get_directory())?; + + let name_param = format!("%{}%", task_name); + let tag_list_params: Vec<String> = tag_list.iter().map(|tag| format!("%{}%", tag)).collect(); + + let mut sql_query = String::from("SELECT * FROM tasks WHERE lower(task_name) LIKE lower(?)"); + tag_list_params.iter().for_each(|_| sql_query.push_str(" AND lower(tags) LIKE lower(?)")); + sql_query.push_str(" ORDER BY task_name"); + + let mut query = conn.prepare(sql_query.as_str())?; + query.raw_bind_parameter(1, name_param)?; + for (i, tag) in tag_list_params.iter().enumerate() { + query.raw_bind_parameter(i + 2, tag)?; + } + + let tasks_vec = query.raw_query().mapped(|row| { + Ok(Task { + id: row.get(0)?, + task_name: row.get(1)?, + start_time: row.get(2)?, + stop_time: row.get(3)?, + tags: row.get(4)?, + }) + }).map(|task_item| task_item.unwrap()).collect(); + + Ok(tasks_vec) +} + pub fn check_for_tasks() -> Result<String> { let conn = Connection::open(get_directory())?; |