don't delete memos, just flag as done and keep archived

This commit is contained in:
əlemi 2022-03-14 22:41:32 +01:00
parent fe9dabed7b
commit ea91973e5a
No known key found for this signature in database
GPG key ID: BBCBFE5D7244634E
2 changed files with 23 additions and 15 deletions

View file

@ -15,9 +15,10 @@ use utils::{parse_human_duration, HumanDisplay};
struct Cli { struct Cli {
#[clap(subcommand)] #[clap(subcommand)]
command: Option<Commands>, command: Option<Commands>,
#[clap(short, long, help = "location for database file")] #[clap(short, long, help = "location for database file")]
db_path: Option<String>, db_path: Option<String>,
#[clap(long, help = "show completed tasks")]
old: bool,
} }
#[derive(Subcommand)] #[derive(Subcommand)]
@ -69,7 +70,7 @@ fn main() {
let mut found = false; let mut found = false;
let mut to_remove: Option<Memo> = None; let mut to_remove: Option<Memo> = None;
if let Some(re) = rex.ok() { if let Some(re) = rex.ok() {
for memo in storage.all().unwrap() { for memo in storage.all(false).unwrap() {
if re.is_match(memo.body.as_str()) { if re.is_match(memo.body.as_str()) {
if many { if many {
storage.del(memo.id).unwrap(); storage.del(memo.id).unwrap();
@ -97,7 +98,7 @@ fn main() {
println!("[-] task #{} deleted", id); println!("[-] task #{} deleted", id);
} }
None => { None => {
let all = storage.all().unwrap(); let all = storage.all(args.old).unwrap();
if all.len() < 1 { if all.len() < 1 {
println!("[ ] nothing to remember"); println!("[ ] nothing to remember");
} }

View file

@ -25,7 +25,7 @@ impl fmt::Display for Memo {
} }
pub trait MemoStorage { pub trait MemoStorage {
fn all(&self) -> Result<Vec<Memo>, Error>; fn all(&self, done: bool) -> Result<Vec<Memo>, Error>;
fn add(&self, body: &str, due: Option<DateTime<Utc>>) -> Result<(), Error>; fn add(&self, body: &str, due: Option<DateTime<Utc>>) -> Result<(), Error>;
fn del(&self, id: u32) -> Result<bool, Error>; fn del(&self, id: u32) -> Result<bool, Error>;
fn get(&self, id: u32) -> Result<Memo, Error>; fn get(&self, id: u32) -> Result<Memo, Error>;
@ -44,7 +44,8 @@ pub fn open_sqlite_storage(path: &str) -> Result<SQLiteStorage, Error> {
"CREATE TABLE IF NOT EXISTS memo ( "CREATE TABLE IF NOT EXISTS memo (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
body TEXT NOT NULL, body TEXT NOT NULL,
due DATETIME due DATETIME,
done BOOL DEFAULT FALSE
);", );",
[], [],
)?; )?;
@ -52,7 +53,7 @@ pub fn open_sqlite_storage(path: &str) -> Result<SQLiteStorage, Error> {
} }
impl MemoStorage for SQLiteStorage { impl MemoStorage for SQLiteStorage {
fn all(&self) -> Result<Vec<Memo>, Error> { fn all(&self, done: bool) -> Result<Vec<Memo>, Error> {
let mut results = Vec::new(); let mut results = Vec::new();
/* /*
@ -62,8 +63,10 @@ impl MemoStorage for SQLiteStorage {
*/ */
{ {
let mut statement = self.conn.prepare("SELECT * FROM memo WHERE due IS NOT NULL ORDER BY due, id")?; let mut statement = self.conn.prepare(
let mut rows = statement.query([])?; "SELECT * FROM memo WHERE due IS NOT NULL AND done = ? ORDER BY due, id",
)?;
let mut rows = statement.query(params![done as u8])?;
while let Some(row) = rows.next()? { while let Some(row) = rows.next()? {
results.push(Memo { results.push(Memo {
@ -75,8 +78,10 @@ impl MemoStorage for SQLiteStorage {
} }
{ {
let mut statement = self.conn.prepare("SELECT * FROM memo WHERE due IS NULL ORDER BY due, id")?; let mut statement = self
let mut rows = statement.query([])?; .conn
.prepare("SELECT * FROM memo WHERE due IS NULL AND done = ? ORDER BY due, id")?;
let mut rows = statement.query(params![done as u8])?;
while let Some(row) = rows.next()? { while let Some(row) = rows.next()? {
results.push(Memo { results.push(Memo {
@ -107,7 +112,7 @@ impl MemoStorage for SQLiteStorage {
fn del(&self, id: u32) -> Result<bool, Error> { fn del(&self, id: u32) -> Result<bool, Error> {
let count = self let count = self
.conn .conn
.execute("DELETE FROM memo WHERE id = ?", params![id])?; .execute("UPDATE memo SET done = 1 WHERE id = ?", params![id])?;
if count > 0 { if count > 0 {
return Ok(true); return Ok(true);
} else { } else {
@ -116,14 +121,16 @@ impl MemoStorage for SQLiteStorage {
} }
fn get(&self, id: u32) -> Result<Memo, Error> { fn get(&self, id: u32) -> Result<Memo, Error> {
return Ok(self return Ok(self.conn.query_row(
.conn "SELECT * FROM memo WHERE id = ? AND done = 0",
.query_row("SELECT * FROM memo WHERE id = ?", params![id], |row| { params![id],
|row| {
return Ok(Memo { return Ok(Memo {
id: row.get(0)?, id: row.get(0)?,
body: row.get(1)?, body: row.get(1)?,
due: row.get(2).unwrap_or(None), due: row.get(2).unwrap_or(None),
}); });
})?); },
)?);
} }
} }