Initial commit
This commit is contained in:
53
src/db.rs
Normal file
53
src/db.rs
Normal file
@@ -0,0 +1,53 @@
|
||||
use chrono::{DateTime, NaiveDateTime, TimeZone, Utc};
|
||||
use diesel::Queryable;
|
||||
|
||||
use crate::appointment::Appointment;
|
||||
|
||||
#[derive(Queryable)]
|
||||
pub struct DbChat {
|
||||
_id: i32,
|
||||
telegram_id: i64,
|
||||
calendar: String,
|
||||
next_appointment_start: Option<i64>,
|
||||
next_appointment_end: Option<i64>,
|
||||
last_reminder: Option<i64>,
|
||||
pinned_message: Option<i32>,
|
||||
}
|
||||
|
||||
pub struct ChatInfo<Tz: TimeZone> {
|
||||
pub id: i64,
|
||||
pub calendar: String,
|
||||
pub next_appointment: Option<Appointment<Tz>>,
|
||||
pub last_reminder: Option<DateTime<Tz>>,
|
||||
pub pinned_message_id: Option<i32>,
|
||||
}
|
||||
|
||||
impl From<DbChat> for ChatInfo<Utc> {
|
||||
fn from(db_chat: DbChat) -> Self {
|
||||
let next_appointment = db_chat
|
||||
.next_appointment_start
|
||||
// Join appointments into single option
|
||||
.and_then(|start| Some([start, db_chat.next_appointment_end?]))
|
||||
// Convert timestamps to datetimes
|
||||
.map(|timestamps| {
|
||||
timestamps
|
||||
.map(|timestamp| NaiveDateTime::from_timestamp(timestamp, 0))
|
||||
.map(|date_time| DateTime::<Utc>::from_utc(date_time, Utc))
|
||||
})
|
||||
// Join datetimes into Appointment
|
||||
.map(|[start, end]| Appointment { start, end });
|
||||
|
||||
let last_reminder = db_chat
|
||||
.last_reminder
|
||||
.map(|timestamp| NaiveDateTime::from_timestamp(timestamp, 0))
|
||||
.map(|date_time| DateTime::<Utc>::from_utc(date_time, Utc));
|
||||
|
||||
ChatInfo {
|
||||
id: db_chat.telegram_id,
|
||||
calendar: db_chat.calendar,
|
||||
next_appointment: next_appointment,
|
||||
last_reminder,
|
||||
pinned_message_id: db_chat.pinned_message,
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user