-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Can add a birthday and show all birthdays
- Loading branch information
1 parent
b134ff5
commit 7dc3959
Showing
2 changed files
with
67 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use anyhow::Result; | ||
use chrono::NaiveDate; | ||
use rusqlite::Connection; | ||
|
||
fn get_db() -> Result<Connection> { | ||
let db = Connection::open("test.db")?; | ||
db.execute( | ||
"CREATE TABLE IF NOT EXISTS birthdays ( | ||
id INTEGER PRIMARY KEY, | ||
name TEXT NOT NULL, | ||
date TEXT NOT NULL | ||
) STRICT", | ||
(), | ||
)?; | ||
Ok(db) | ||
} | ||
|
||
pub fn add_birthday(name: String, date: String) -> Result<()> { | ||
let db = get_db()?; | ||
let naive_date = NaiveDate::parse_from_str(&date, "%Y-%m-%d")?; | ||
db.execute( | ||
"INSERT INTO birthdays(name, date) VALUES(?1, ?2)", | ||
(name, naive_date), | ||
)?; | ||
Ok(()) | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct Birthday { | ||
name: String, | ||
date: NaiveDate, | ||
} | ||
|
||
pub fn get_all_birthdays() -> Result<Vec<Birthday>> { | ||
let db = get_db()?; | ||
let mut statement = db.prepare("SELECT name, date FROM birthdays")?; | ||
let birthday_iter = statement.query_map([], |row| { | ||
Ok(Birthday { | ||
name: row.get(0)?, | ||
date: row.get(1)?, | ||
}) | ||
})?; | ||
let birthdays: Result<Vec<Birthday>, rusqlite::Error> = birthday_iter.collect(); | ||
Ok(birthdays.unwrap()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters