mi/backend/src/models.rs

53 lines
1.2 KiB
Rust
Raw Normal View History

2020-11-02 18:06:50 +00:00
use crate::schema::*;
use chrono::NaiveDateTime;
use serde::Serialize;
2020-11-03 14:47:19 +00:00
use std::convert::TryInto;
2020-11-02 18:06:50 +00:00
#[derive(Queryable, Debug, Serialize)]
2020-09-13 16:07:06 +00:00
pub struct Member {
pub id: i32,
2020-11-02 18:06:50 +00:00
#[serde(rename = "name")]
2020-09-13 16:07:06 +00:00
pub cmene: String,
pub picurl: String,
}
2020-11-03 14:47:19 +00:00
#[derive(Queryable, Associations, Insertable)]
2020-11-02 18:06:50 +00:00
#[belongs_to(Member)]
#[table_name = "switches"]
2020-09-13 16:07:06 +00:00
pub struct Switch {
pub id: String,
2020-11-02 18:06:50 +00:00
pub member_id: i32,
pub started_at: NaiveDateTime,
pub ended_at: Option<NaiveDateTime>,
2020-11-03 14:47:19 +00:00
}
impl Switch {
pub fn duration(&self) -> Option<i32> {
match self.ended_at {
None => None,
Some(end_time) => Some(
end_time
.clone()
.signed_duration_since(self.started_at)
.num_seconds()
.try_into()
.expect("don't expect a switch to last 30+ years"),
),
}
}
2020-11-02 19:03:32 +00:00
}
#[derive(Insertable)]
#[table_name = "switches"]
pub struct NewSwitch {
pub id: String,
pub member_id: i32,
pub started_at: NaiveDateTime,
}
#[derive(AsChangeset)]
#[table_name = "switches"]
pub struct UpdateSwitchTime {
pub ended_at: Option<NaiveDateTime>,
2020-09-13 16:07:06 +00:00
}