From eda5314258f6194e33cac73d81047874ed82772e Mon Sep 17 00:00:00 2001 From: ducdetronquito Date: Mon, 1 Jan 2024 14:36:12 +0100 Subject: [PATCH] feat: Forget a birthday --- README.md | 4 ++++ src/birthday.rs | 1 + src/birthday_store.rs | 23 +++++++++++++++++++++-- src/lib.rs | 4 ++++ src/main.rs | 10 ++++++++++ src/output.rs | 10 +++++++++- 6 files changed, 49 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a3891e3..314b81a 100644 --- a/README.md +++ b/README.md @@ -67,4 +67,8 @@ $ birthday search --month 3 ├────┼────────────┼──────────────┼───────────┼───────────────┤ │ 1 │ Ben Dover │ 3 may | 34 (1990) │ today │ ╰────┴────────────┴──────────────┴───────────┴───────────────╯ + +# Forget a birthday by ID +$ birthday forget 3 +Birthday of 'Anita Bath' has been forgotten 🗑️ ``` diff --git a/src/birthday.rs b/src/birthday.rs index d35c224..13ed0d2 100644 --- a/src/birthday.rs +++ b/src/birthday.rs @@ -1,5 +1,6 @@ use chrono::{Datelike, NaiveDate}; +#[derive(Clone)] pub struct Birthday { pub id: i32, pub name: String, diff --git a/src/birthday_store.rs b/src/birthday_store.rs index 56fb05b..17db1cd 100644 --- a/src/birthday_store.rs +++ b/src/birthday_store.rs @@ -25,8 +25,27 @@ pub fn get_all() -> Result> { let date = from_timestamp(timestamp); Ok(Birthday { id, name, date }) })?; - let birthdays: Result, rusqlite::Error> = birthday_iter.collect(); - Ok(birthdays.unwrap()) + let birthdays = birthday_iter.collect::, rusqlite::Error>>()?; + Ok(birthdays) +} + +pub fn remove(id: i32) -> Result> { + let db = get_db()?; + let mut statement = + db.prepare("DELETE FROM birthdays WHERE id = :id RETURNING id, name, date_timestamp")?; + let birthday_iter = statement.query_map(&[(":id", id.to_string().as_str())], |row| { + let id = row.get(0)?; + let name = row.get(1)?; + let timestamp = row.get(2)?; + let date = from_timestamp(timestamp); + Ok(Birthday { id, name, date }) + })?; + let birthdays = birthday_iter.collect::, rusqlite::Error>>()?; + if birthdays.is_empty() { + Ok(None) + } else { + Ok(Some(birthdays[0].clone())) + } } fn get_db() -> Result { diff --git a/src/lib.rs b/src/lib.rs index f8ac85a..9d4e1fd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -50,3 +50,7 @@ pub fn search( Ok(birthdays) } + +pub fn forget(id: i32) -> Result> { + birthday_store::remove(id) +} diff --git a/src/main.rs b/src/main.rs index 08030cd..d075256 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,6 +34,8 @@ enum Command { }, #[command(about = "Show today's birthdays")] Today {}, + #[command(about = "Forget a birthday by ID")] + Forget { id: i32 }, } fn main() -> Result<()> { @@ -74,5 +76,13 @@ fn main() -> Result<()> { output::output(birthdays, today); Ok(()) } + Command::Forget { id } => { + let maybe_birthday = birthday::forget(id)?; + match maybe_birthday { + Some(birthday) => output::birthday_forgotten(birthday), + None => output::no_birthday_found(), + } + Ok(()) + } } } diff --git a/src/output.rs b/src/output.rs index 5cd5b93..0a3e1fc 100644 --- a/src/output.rs +++ b/src/output.rs @@ -46,7 +46,7 @@ impl DisplayedBirthday { pub fn output(birthdays: Vec, today: NaiveDate) { if birthdays.is_empty() { - println!("No birthday found 🔎"); + no_birthday_found(); return; } let mut displayed_birthdays: Vec = birthdays @@ -59,3 +59,11 @@ pub fn output(birthdays: Vec, today: NaiveDate) { .to_string(); println!("{table}") } + +pub fn no_birthday_found() { + println!("No birthday found 🔎"); +} + +pub fn birthday_forgotten(birthday: Birthday) { + println!("Birthday of '{}' has been forgotten 🗑️", birthday.name) +}