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 {
#[clap(subcommand)]
command: Option<Commands>,
#[clap(short, long, help = "location for database file")]
db_path: Option<String>,
#[clap(long, help = "show completed tasks")]
old: bool,
}
#[derive(Subcommand)]
@ -69,7 +70,7 @@ fn main() {
let mut found = false;
let mut to_remove: Option<Memo> = None;
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 many {
storage.del(memo.id).unwrap();
@ -97,7 +98,7 @@ fn main() {
println!("[-] task #{} deleted", id);
}
None => {
let all = storage.all().unwrap();
let all = storage.all(args.old).unwrap();
if all.len() < 1 {
println!("[ ] nothing to remember");
}

View file

@ -25,7 +25,7 @@ impl fmt::Display for Memo {
}
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 del(&self, id: u32) -> Result<bool, 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 (
id INTEGER PRIMARY KEY,
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 {
fn all(&self) -> Result<Vec<Memo>, Error> {
fn all(&self, done: bool) -> Result<Vec<Memo>, Error> {
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 rows = statement.query([])?;
let mut statement = self.conn.prepare(
"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()? {
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 rows = statement.query([])?;
let mut statement = self
.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()? {
results.push(Memo {
@ -107,7 +112,7 @@ impl MemoStorage for SQLiteStorage {
fn del(&self, id: u32) -> Result<bool, Error> {
let count = self
.conn
.execute("DELETE FROM memo WHERE id = ?", params![id])?;
.execute("UPDATE memo SET done = 1 WHERE id = ?", params![id])?;
if count > 0 {
return Ok(true);
} else {
@ -116,14 +121,16 @@ impl MemoStorage for SQLiteStorage {
}
fn get(&self, id: u32) -> Result<Memo, Error> {
return Ok(self
.conn
.query_row("SELECT * FROM memo WHERE id = ?", params![id], |row| {
return Ok(self.conn.query_row(
"SELECT * FROM memo WHERE id = ? AND done = 0",
params![id],
|row| {
return Ok(Memo {
id: row.get(0)?,
body: row.get(1)?,
due: row.get(2).unwrap_or(None),
});
})?);
},
)?);
}
}