Skip to content

Commit

Permalink
feat: Forget a birthday
Browse files Browse the repository at this point in the history
  • Loading branch information
ducdetronquito committed Jan 1, 2024
1 parent f222838 commit 3243db1
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 removed 🗑️
```
1 change: 1 addition & 0 deletions src/birthday.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use chrono::{Datelike, NaiveDate};

#[derive(Clone)]
pub struct Birthday {
pub id: i32,
pub name: String,
Expand Down
23 changes: 21 additions & 2 deletions src/birthday_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,27 @@ pub fn get_all() -> Result<Vec<Birthday>> {
let date = from_timestamp(timestamp);
Ok(Birthday { id, name, date })
})?;
let birthdays: Result<Vec<Birthday>, rusqlite::Error> = birthday_iter.collect();
Ok(birthdays.unwrap())
let birthdays = birthday_iter.collect::<Result<Vec<Birthday>, rusqlite::Error>>()?;
Ok(birthdays)
}

pub fn remove(id: i32) -> Result<Option<Birthday>> {
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::<Result<Vec<Birthday>, rusqlite::Error>>()?;
if birthdays.is_empty() {
Ok(None)
} else {
Ok(Some(birthdays[0].clone()))
}
}

fn get_db() -> Result<Connection> {
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,7 @@ pub fn search(

Ok(birthdays)
}

pub fn forget(id: i32) -> Result<Option<Birthday>> {
birthday_store::remove(id)
}
10 changes: 10 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()> {
Expand Down Expand Up @@ -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(())
}
}
}
10 changes: 9 additions & 1 deletion src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl DisplayedBirthday {

pub fn output(birthdays: Vec<Birthday>, today: NaiveDate) {
if birthdays.is_empty() {
println!("No birthday found 🔎");
no_birthday_found();
return;
}
let mut displayed_birthdays: Vec<DisplayedBirthday> = birthdays
Expand All @@ -59,3 +59,11 @@ pub fn output(birthdays: Vec<Birthday>, 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)
}

0 comments on commit 3243db1

Please sign in to comment.