diff options
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())?; |