Added birthday reminders
+ clippy + fmt
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
use crate::error::Error;
|
||||
use chrono::Datelike;
|
||||
use j_db::database::Database;
|
||||
use j_db::model::JdbModel;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||
pub struct BirthdayEntry {
|
||||
id: Option<u64>,
|
||||
pub discord_id: u64,
|
||||
pub birthday: chrono::NaiveDate,
|
||||
}
|
||||
|
||||
impl BirthdayEntry {
|
||||
pub fn add_birthday(
|
||||
db: &Database,
|
||||
discord_id: u64,
|
||||
birthday: chrono::NaiveDate,
|
||||
) -> Result<(), Error> {
|
||||
let mut entry = db
|
||||
.filter(|_, entry: &BirthdayEntry| entry.discord_id == discord_id)?
|
||||
.next()
|
||||
.unwrap_or(BirthdayEntry {
|
||||
id: None,
|
||||
discord_id,
|
||||
birthday,
|
||||
});
|
||||
|
||||
entry.birthday = birthday;
|
||||
|
||||
db.insert(entry)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn todays_birthdays(
|
||||
db: &Database,
|
||||
date: chrono::NaiveDate,
|
||||
) -> Result<Vec<BirthdayEntry>, Error> {
|
||||
let birthdays: Vec<BirthdayEntry> = db
|
||||
.filter(|_, entry: &BirthdayEntry| {
|
||||
entry.birthday.month() == date.month() && entry.birthday.day() == date.day()
|
||||
})?
|
||||
.collect();
|
||||
|
||||
Ok(birthdays)
|
||||
}
|
||||
}
|
||||
|
||||
impl JdbModel for BirthdayEntry {
|
||||
fn id(&self) -> Option<u64> {
|
||||
self.id
|
||||
}
|
||||
|
||||
fn set_id(&mut self, id: u64) {
|
||||
self.id = Some(id)
|
||||
}
|
||||
|
||||
fn tree() -> String {
|
||||
"Birthday".to_string()
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
pub mod album;
|
||||
pub mod api_key;
|
||||
pub mod birthday;
|
||||
pub mod insult_compliment;
|
||||
pub mod motivation;
|
||||
pub mod random;
|
||||
|
||||
Reference in New Issue
Block a user