use super::{event_type::EventType, location::Location};
use crate::db::pool::DbPool;
use chrono::{Local, NaiveDate, NaiveTime};
use serde::Serialize;
#[derive(Debug, Clone, Serialize)]
pub struct Event {
pub id: i32,
pub date: NaiveDate, pub time: NaiveTime, pub kind: EventType, pub location: Location, pub lunch: Option<i32>, pub work_gap: bool,
pub pair: i32, pub source: String, pub meta: Option<String>, pub created_at: String, }
#[derive(Debug, Clone, Default)]
pub struct EventExtras {
pub lunch: Option<i32>,
pub work_gap: bool,
pub meta: Option<String>,
pub source: Option<String>,
pub pair: Option<i32>,
pub created_at: Option<String>,
}
impl Event {
pub fn new(
id: i32,
date: NaiveDate,
time: NaiveTime,
kind: EventType,
location: Location,
extras: EventExtras,
) -> Self {
Self {
id,
date,
time,
kind,
location,
lunch: extras.lunch,
work_gap: extras.work_gap,
pair: extras.pair.unwrap_or(0),
source: extras.source.unwrap_or_else(|| "cli".to_string()),
meta: extras.meta,
created_at: extras
.created_at
.unwrap_or_else(|| Local::now().to_rfc3339()),
}
}
pub fn date_str(&self) -> String {
self.date.format("%Y-%m-%d").to_string()
}
pub fn time_str(&self) -> String {
self.time.format("%H:%M").to_string()
}
pub fn timestamp(&self) -> chrono::DateTime<Local> {
let dt = self.date.and_time(self.time);
dt.and_local_timezone(Local).unwrap()
}
pub fn get_date_time(&self) -> String {
self.date
.and_time(self.time)
.format("%Y-%m-%d %H:%M")
.to_string()
}
pub fn has_events_for_dates(pool: &mut DbPool, dates: &[NaiveDate]) -> rusqlite::Result<bool> {
if dates.is_empty() {
return Ok(false);
}
let date_strings: Vec<String> = dates
.iter()
.map(|d| d.format("%Y-%m-%d").to_string())
.collect();
let placeholders = vec!["?"; date_strings.len()].join(",");
let sql = format!(
"SELECT 1 FROM events WHERE date IN ({}) LIMIT 1",
placeholders
);
let params: Vec<&dyn rusqlite::ToSql> = date_strings
.iter()
.map(|s| s as &dyn rusqlite::ToSql)
.collect();
let exists = {
let conn = &mut pool.conn;
let mut stmt = conn.prepare(&sql)?;
stmt.exists(rusqlite::params_from_iter(params))?
};
Ok(exists)
}
}