From a8f8e23cf453e71010420087d60388cb2c099460 Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Sat, 20 Aug 2022 23:38:38 +0300 Subject: [PATCH] repo/sqlite: returns finished tasks - repo/fs: returns finished tasks - cli/list: add --finished flag to get finished tasks Closes #4 --- src/bin/fs_to_sqlite.rs | 4 ++-- src/cli/list.rs | 5 ++++- src/repo.rs | 4 +--- src/repo/fs.rs | 15 ++++++++------- src/repo/sqlite.rs | 21 +++++++++++---------- 5 files changed, 26 insertions(+), 23 deletions(-) diff --git a/src/bin/fs_to_sqlite.rs b/src/bin/fs_to_sqlite.rs index 3501306..9f509dc 100644 --- a/src/bin/fs_to_sqlite.rs +++ b/src/bin/fs_to_sqlite.rs @@ -4,10 +4,10 @@ use xdg::BaseDirectories; fn main() { let xdg_dirs = BaseDirectories::with_prefix(env!("CARGO_PKG_NAME")).unwrap(); let fs_repo = repo::fs::FsRepo::new(xdg_dirs.clone()); - let tasks = fs_repo.get_tasks().unwrap(); + let fs_tasks = fs_repo.get_tasks(false).unwrap(); let sqlite_repo = repo::sqlite::SqliteRepo::new(xdg_dirs).unwrap(); - for task in tasks { + for task in fs_tasks { log::info!("task: {}", task.name); log::info!(" inserting..."); diff --git a/src/cli/list.rs b/src/cli/list.rs index 9703bfc..3ed7fdd 100644 --- a/src/cli/list.rs +++ b/src/cli/list.rs @@ -18,11 +18,14 @@ use crate::repo::Repository; #[derive(clap::Args)] pub struct Args { + #[clap(short, long)] + finished: bool, + projects: Vec, } pub fn execute(repo: impl Repository, args: Args) { - let tasks = match repo.get_tasks() { + let tasks = match repo.get_tasks(args.finished) { Ok(tasks) => tasks, Err(err) => return eprintln!("Cannot read tasks: {}", err), }; diff --git a/src/repo.rs b/src/repo.rs index 70dd069..119da4d 100755 --- a/src/repo.rs +++ b/src/repo.rs @@ -69,7 +69,7 @@ pub trait Repository { fn get_task_opt(&self, id: domain::TaskIdx) -> Result, Error>; - fn get_tasks(&self) -> Result, Error>; + fn get_tasks(&self, finished: bool) -> Result, Error>; fn remove_task(&self, id: domain::TaskIdx) -> Result; @@ -86,6 +86,4 @@ pub trait Repository { fn stop_task(&self) -> Result; fn finish_task(&self) -> Result; - - fn get_finished_tasks(&self) -> Result; } diff --git a/src/repo/fs.rs b/src/repo/fs.rs index 9cb4f98..9ec94b8 100644 --- a/src/repo/fs.rs +++ b/src/repo/fs.rs @@ -89,9 +89,14 @@ impl Repository for FsRepo { Ok(Some(tasks[id - 1].clone().into())) } - fn get_tasks(&self) -> Result, Error> { - self.get_tasks_impl() - .map(|tasks| tasks.into_iter().map(Task::into).collect()) + fn get_tasks(&self, finished: bool) -> Result, Error> { + let fs_tasks = if finished { + self.get_finished_tasks_impl()?.into_iter().rev().collect() + } else { + self.get_tasks_impl()? + }; + + Ok(fs_tasks.into_iter().map(Task::into).collect()) } fn remove_task(&self, id: domain::TaskIdx) -> Result { @@ -203,10 +208,6 @@ impl Repository for FsRepo { Ok(task.into()) } - - fn get_finished_tasks(&self) -> Result { - todo!() - } } impl FsRepo { diff --git a/src/repo/sqlite.rs b/src/repo/sqlite.rs index 7239cb9..5e09e4a 100644 --- a/src/repo/sqlite.rs +++ b/src/repo/sqlite.rs @@ -53,7 +53,7 @@ impl<'r> TryFrom<&'r rusqlite::Row<'_>> for Task { } } -const SCHEMA_FILE: &str = "schema.sql"; +const SCHEMA_FILE: &str = "tas.db"; pub struct SqliteRepo { conn: Connection, @@ -82,11 +82,16 @@ impl Repository for SqliteRepo { self.get_task_opt_impl(id).map(|t| t.map(From::from)) } - fn get_tasks(&self) -> Result, Error> { - let mut stmt = self - .conn - .prepare("SELECT * FROM active_tasks") - .map_err(|_| Error::PrepareQuery)?; + fn get_tasks(&self, finished: bool) -> Result, Error> { + let mut stmt = if finished { + self.conn + .prepare("SELECT * FROM finished_tasks") + .map_err(|_| Error::PrepareQuery)? + } else { + self.conn + .prepare("SELECT * FROM active_tasks") + .map_err(|_| Error::PrepareQuery)? + }; let rows = stmt .query_map([], |row| Task::try_from(row)) @@ -211,10 +216,6 @@ impl Repository for SqliteRepo { Ok(db_task.into()) } - - fn get_finished_tasks(&self) -> Result { - todo!() - } } impl SqliteRepo {