Get list of finished tasks #32

Merged
pleshevskiy merged 5 commits from finished into main 2022-08-21 00:02:18 +03:00
5 changed files with 26 additions and 23 deletions
Showing only changes of commit a8f8e23cf4 - Show all commits

View file

@ -4,10 +4,10 @@ use xdg::BaseDirectories;
fn main() { fn main() {
let xdg_dirs = BaseDirectories::with_prefix(env!("CARGO_PKG_NAME")).unwrap(); let xdg_dirs = BaseDirectories::with_prefix(env!("CARGO_PKG_NAME")).unwrap();
let fs_repo = repo::fs::FsRepo::new(xdg_dirs.clone()); 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(); 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!("task: {}", task.name);
log::info!(" inserting..."); log::info!(" inserting...");

View file

@ -18,11 +18,14 @@ use crate::repo::Repository;
#[derive(clap::Args)] #[derive(clap::Args)]
pub struct Args { pub struct Args {
#[clap(short, long)]
finished: bool,
projects: Vec<String>, projects: Vec<String>,
} }
pub fn execute(repo: impl Repository, args: Args) { 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, Ok(tasks) => tasks,
Err(err) => return eprintln!("Cannot read tasks: {}", err), Err(err) => return eprintln!("Cannot read tasks: {}", err),
}; };

View file

@ -69,7 +69,7 @@ pub trait Repository {
fn get_task_opt(&self, id: domain::TaskIdx) -> Result<Option<domain::Task>, Error>; fn get_task_opt(&self, id: domain::TaskIdx) -> Result<Option<domain::Task>, Error>;
fn get_tasks(&self) -> Result<Vec<domain::Task>, Error>; fn get_tasks(&self, finished: bool) -> Result<Vec<domain::Task>, Error>;
fn remove_task(&self, id: domain::TaskIdx) -> Result<domain::Task, Error>; fn remove_task(&self, id: domain::TaskIdx) -> Result<domain::Task, Error>;
@ -86,6 +86,4 @@ pub trait Repository {
fn stop_task(&self) -> Result<domain::Task, Error>; fn stop_task(&self) -> Result<domain::Task, Error>;
fn finish_task(&self) -> Result<domain::Task, Error>; fn finish_task(&self) -> Result<domain::Task, Error>;
fn get_finished_tasks(&self) -> Result<domain::Task, Error>;
} }

View file

@ -89,9 +89,14 @@ impl Repository for FsRepo {
Ok(Some(tasks[id - 1].clone().into())) Ok(Some(tasks[id - 1].clone().into()))
} }
fn get_tasks(&self) -> Result<Vec<domain::Task>, Error> { fn get_tasks(&self, finished: bool) -> Result<Vec<domain::Task>, Error> {
self.get_tasks_impl() let fs_tasks = if finished {
.map(|tasks| tasks.into_iter().map(Task::into).collect()) 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<domain::Task, Error> { fn remove_task(&self, id: domain::TaskIdx) -> Result<domain::Task, Error> {
@ -203,10 +208,6 @@ impl Repository for FsRepo {
Ok(task.into()) Ok(task.into())
} }
fn get_finished_tasks(&self) -> Result<domain::Task, Error> {
todo!()
}
} }
impl FsRepo { impl FsRepo {

View file

@ -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 { pub struct SqliteRepo {
conn: Connection, conn: Connection,
@ -82,11 +82,16 @@ impl Repository for SqliteRepo {
self.get_task_opt_impl(id).map(|t| t.map(From::from)) self.get_task_opt_impl(id).map(|t| t.map(From::from))
} }
fn get_tasks(&self) -> Result<Vec<domain::Task>, Error> { fn get_tasks(&self, finished: bool) -> Result<Vec<domain::Task>, Error> {
let mut stmt = self let mut stmt = if finished {
.conn self.conn
.prepare("SELECT * FROM active_tasks") .prepare("SELECT * FROM finished_tasks")
.map_err(|_| Error::PrepareQuery)?; .map_err(|_| Error::PrepareQuery)?
} else {
self.conn
.prepare("SELECT * FROM active_tasks")
.map_err(|_| Error::PrepareQuery)?
};
let rows = stmt let rows = stmt
.query_map([], |row| Task::try_from(row)) .query_map([], |row| Task::try_from(row))
@ -211,10 +216,6 @@ impl Repository for SqliteRepo {
Ok(db_task.into()) Ok(db_task.into())
} }
fn get_finished_tasks(&self) -> Result<domain::Task, Error> {
todo!()
}
} }
impl SqliteRepo { impl SqliteRepo {