repo/sqlite: returns finished tasks

- repo/fs: returns finished tasks
- cli/list: add --finished flag to get finished tasks

Closes #4
This commit is contained in:
Dmitriy Pleshevskiy 2022-08-20 23:38:38 +03:00
parent 1224eb2d39
commit a8f8e23cf4
Signed by: pleshevskiy
GPG key ID: 1B59187B161C0215
5 changed files with 26 additions and 23 deletions

View file

@ -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...");

View file

@ -18,11 +18,14 @@ use crate::repo::Repository;
#[derive(clap::Args)]
pub struct Args {
#[clap(short, long)]
finished: bool,
projects: Vec<String>,
}
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),
};

View file

@ -69,7 +69,7 @@ pub trait Repository {
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>;
@ -86,6 +86,4 @@ pub trait Repository {
fn stop_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()))
}
fn get_tasks(&self) -> Result<Vec<domain::Task>, Error> {
self.get_tasks_impl()
.map(|tasks| tasks.into_iter().map(Task::into).collect())
fn get_tasks(&self, finished: bool) -> Result<Vec<domain::Task>, 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<domain::Task, Error> {
@ -203,10 +208,6 @@ impl Repository for FsRepo {
Ok(task.into())
}
fn get_finished_tasks(&self) -> Result<domain::Task, Error> {
todo!()
}
}
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 {
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<Vec<domain::Task>, Error> {
let mut stmt = self
.conn
.prepare("SELECT * FROM active_tasks")
.map_err(|_| Error::PrepareQuery)?;
fn get_tasks(&self, finished: bool) -> Result<Vec<domain::Task>, 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<domain::Task, Error> {
todo!()
}
}
impl SqliteRepo {