From 549803558d22b36ac6ac178722f2a8cc0548531e Mon Sep 17 00:00:00 2001 From: Joan Lledó Date: Sat, 16 Sep 2023 11:07:56 +0200 Subject: Query DB by task name --- src/database.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'src/database.rs') diff --git a/src/database.rs b/src/database.rs index 7050cbb..6c558fe 100644 --- a/src/database.rs +++ b/src/database.rs @@ -343,6 +343,29 @@ pub fn get_list_by_id(id_list: Vec) -> Result, rusqlite::Error> { Ok(tasks_vec) } +pub fn get_list_by_name_or_tag(task_name: String) -> Result, rusqlite::Error> { + let conn = Connection::open(get_directory())?; + let mut tasks_vec: Vec = Vec::new(); + let name_param = format!("%{}%", task_name); + + let mut query = conn.prepare("SELECT * FROM tasks WHERE lower(task_name) LIKE lower(:task_name)")?; + let task_iter = query.query_map(&[(":task_name", &name_param)], |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)?, + }) + })?; + + for task_item in task_iter { + tasks_vec.push(task_item.unwrap()); + } + + Ok(tasks_vec) +} + pub fn check_for_tasks() -> Result { let conn = Connection::open(get_directory())?; -- cgit 1.4.1