bvplan/bvplan/src/db/models.rs
2023-02-27 11:09:16 +01:00

624 lines
17 KiB
Rust

use anyhow::bail;
use chrono::prelude::*;
use diesel::prelude::*;
use serde::{Deserialize, Serialize};
use std::fmt::Display;
use std::io::Write;
use std::str::FromStr;
use crate::db::schema;
#[derive(Identifiable, Queryable, Debug)]
#[diesel(table_name = schema::schoolyears)]
pub struct Schoolyear {
pub id: i32,
pub untis_id: i32,
pub name: String,
pub active: bool,
pub created_at: NaiveDateTime,
pub updated_at: Option<NaiveDateTime>,
}
#[derive(Insertable, Debug)]
#[diesel(table_name = schema::schoolyears)]
pub struct NewSchoolyear<'a> {
pub untis_id: i32,
pub name: &'a str,
pub active: bool,
}
#[derive(Identifiable, Queryable, Associations, Debug)]
#[diesel(table_name = schema::tenants)]
#[diesel(belongs_to(Schoolyear))]
pub struct Tenant {
pub id: i32,
pub untis_id: i32,
pub schoolyear_id: i32,
pub name: String,
pub active: bool,
pub created_at: NaiveDateTime,
pub updated_at: Option<NaiveDateTime>,
}
#[derive(Insertable, Debug)]
#[diesel(table_name = schema::tenants)]
pub struct NewTenant<'a> {
pub untis_id: i32,
pub schoolyear_id: i32,
pub name: &'a str,
pub active: bool,
}
#[derive(Identifiable, Queryable, Associations, Debug)]
#[diesel(table_name = schema::teachers)]
#[diesel(belongs_to(Schoolyear))]
pub struct Teacher {
pub id: i32,
pub untis_id: i32,
pub schoolyear_id: i32,
pub name: String,
pub forename: Option<String>,
pub display_name: String,
pub created_at: NaiveDateTime,
pub updated_at: Option<NaiveDateTime>,
}
#[derive(Insertable, Debug)]
#[diesel(table_name = schema::teachers)]
pub struct NewTeacher<'a> {
pub untis_id: i32,
pub schoolyear_id: i32,
pub name: &'a str,
pub forename: Option<&'a str>,
pub display_name: &'a str,
}
#[derive(Identifiable, Queryable, Associations, Debug)]
#[diesel(table_name = schema::classes)]
#[diesel(belongs_to(Schoolyear))]
pub struct Class {
pub id: i32,
pub untis_id: i32,
pub schoolyear_id: i32,
pub name: String,
pub long_name: String,
pub active: bool,
pub created_at: NaiveDateTime,
pub updated_at: Option<NaiveDateTime>,
}
#[derive(Insertable, Debug)]
#[diesel(table_name = schema::classes)]
pub struct NewClass<'a> {
pub untis_id: i32,
pub schoolyear_id: i32,
pub name: &'a str,
pub long_name: &'a str,
pub active: bool,
}
#[derive(Identifiable, Queryable, Associations, Debug)]
#[diesel(table_name = schema::subjects)]
#[diesel(belongs_to(Schoolyear))]
pub struct Subject {
pub id: i32,
pub untis_id: i32,
pub schoolyear_id: i32,
pub name: String,
pub long_name: String,
pub created_at: NaiveDateTime,
pub updated_at: Option<NaiveDateTime>,
}
#[derive(Insertable, Debug)]
#[diesel(table_name = schema::subjects)]
pub struct NewSubject<'a> {
pub untis_id: i32,
pub schoolyear_id: i32,
pub name: &'a str,
pub long_name: &'a str,
}
#[derive(Identifiable, Queryable, Associations, Debug)]
#[diesel(table_name = schema::rooms)]
#[diesel(belongs_to(Schoolyear))]
pub struct Room {
pub id: i32,
pub untis_id: i32,
pub schoolyear_id: i32,
pub name: String,
pub long_name: String,
pub created_at: NaiveDateTime,
pub updated_at: Option<NaiveDateTime>,
}
#[derive(Insertable, Debug)]
#[diesel(table_name = schema::rooms)]
pub struct NewRoom<'a> {
pub untis_id: i32,
pub schoolyear_id: i32,
pub name: &'a str,
pub long_name: &'a str,
}
#[derive(Identifiable, Queryable, Associations, Debug)]
#[diesel(table_name = schema::departments)]
#[diesel(belongs_to(Schoolyear))]
pub struct Department {
pub id: i32,
pub untis_id: i32,
pub schoolyear_id: i32,
pub name: String,
pub long_name: String,
pub created_at: NaiveDateTime,
pub updated_at: Option<NaiveDateTime>,
}
#[derive(Insertable, Debug)]
#[diesel(table_name = schema::departments)]
pub struct NewDepartment<'a> {
pub untis_id: i32,
pub schoolyear_id: i32,
pub name: &'a str,
pub long_name: &'a str,
}
#[derive(Identifiable, Queryable, Associations, Debug)]
#[diesel(belongs_to(Schoolyear))]
#[diesel(table_name = schema::holidays)]
pub struct Holiday {
pub id: i32,
pub untis_id: i32,
pub schoolyear_id: i32,
pub name: String,
pub long_name: String,
pub start_date: NaiveDate,
pub end_date: NaiveDate,
pub created_at: NaiveDateTime,
pub updated_at: Option<NaiveDateTime>,
}
#[derive(Insertable, Debug)]
#[diesel(table_name = schema::holidays)]
pub struct NewHoliday<'a> {
pub untis_id: i32,
pub schoolyear_id: i32,
pub name: &'a str,
pub long_name: &'a str,
pub start_date: NaiveDate,
pub end_date: NaiveDate,
}
#[derive(Identifiable, Queryable, Associations, Debug)]
#[diesel(belongs_to(Schoolyear))]
#[diesel(table_name = schema::timegrids)]
pub struct Timegrid {
pub id: i32,
pub schoolyear_id: i32,
pub active: bool,
pub created_at: NaiveDateTime,
pub updated_at: Option<NaiveDateTime>,
}
#[derive(Insertable, Debug)]
#[diesel(table_name = schema::timegrids)]
pub struct NewTimegrid {
pub schoolyear_id: i32,
pub active: bool,
}
#[derive(diesel::FromSqlRow, diesel::AsExpression, PartialEq, Eq, Clone, Debug)]
#[diesel(sql_type = schema::sql_types::Weekday)]
pub enum Weekday {
Mon,
Tue,
Wed,
Thu,
Fri,
Sat,
Sun,
}
impl TryFrom<u8> for Weekday {
type Error = anyhow::Error;
fn try_from(value: u8) -> Result<Self, Self::Error> {
Ok(match value {
1 => Self::Sun,
2 => Self::Mon,
3 => Self::Tue,
4 => Self::Wed,
5 => Self::Thu,
6 => Self::Fri,
7 => Self::Sat,
_ => bail!("Invalid weekday: {:?}", value),
})
}
}
impl From<chrono::Weekday> for Weekday {
fn from(value: chrono::Weekday) -> Self {
match value {
chrono::Weekday::Mon => Self::Mon,
chrono::Weekday::Tue => Self::Tue,
chrono::Weekday::Wed => Self::Wed,
chrono::Weekday::Thu => Self::Thu,
chrono::Weekday::Fri => Self::Fri,
chrono::Weekday::Sat => Self::Sat,
chrono::Weekday::Sun => Self::Sun,
}
}
}
impl diesel::serialize::ToSql<schema::sql_types::Weekday, diesel::pg::Pg> for Weekday {
fn to_sql<'b>(
&'b self,
out: &mut diesel::serialize::Output<'b, '_, diesel::pg::Pg>,
) -> diesel::serialize::Result {
match *self {
Self::Mon => out.write_all(b"mon")?,
Self::Tue => out.write_all(b"tue")?,
Self::Wed => out.write_all(b"wed")?,
Self::Thu => out.write_all(b"thu")?,
Self::Fri => out.write_all(b"fri")?,
Self::Sat => out.write_all(b"sat")?,
Self::Sun => out.write_all(b"sun")?,
}
Ok(diesel::serialize::IsNull::No)
}
}
impl diesel::deserialize::FromSql<schema::sql_types::Weekday, diesel::pg::Pg> for Weekday {
fn from_sql(
bytes: diesel::backend::RawValue<'_, diesel::pg::Pg>,
) -> diesel::deserialize::Result<Self> {
match bytes.as_bytes() {
b"mon" => Ok(Self::Mon),
b"tue" => Ok(Self::Tue),
b"wed" => Ok(Self::Wed),
b"thu" => Ok(Self::Thu),
b"fri" => Ok(Self::Fri),
b"sat" => Ok(Self::Sat),
b"sun" => Ok(Self::Sun),
_ => Err("Unrecognized enum variant".into()),
}
}
}
#[derive(Identifiable, Queryable, Associations, Debug)]
#[diesel(belongs_to(Timegrid))]
#[diesel(table_name = schema::timegrid_days)]
pub struct TimegridDay {
pub id: i32,
pub timegrid_id: i32,
pub weekday: Weekday,
pub created_at: NaiveDateTime,
pub updated_at: Option<NaiveDateTime>,
}
#[derive(Insertable, Debug)]
#[diesel(table_name = schema::timegrid_days)]
pub struct NewTimegridDay {
pub timegrid_id: i32,
pub weekday: Weekday,
}
#[derive(Identifiable, Queryable, Associations, Debug)]
#[diesel(belongs_to(TimegridDay))]
#[diesel(table_name = schema::timegrid_time_unit)]
pub struct TimegridTimeUnit {
pub id: i32,
pub timegrid_day_id: i32,
pub start_time: NaiveTime,
pub end_time: NaiveTime,
pub created_at: NaiveDateTime,
pub updated_at: Option<NaiveDateTime>,
}
#[derive(Insertable, Debug)]
#[diesel(table_name = schema::timegrid_time_unit)]
pub struct NewTimegridTimeUnit {
pub timegrid_day_id: i32,
pub start_time: NaiveTime,
pub end_time: NaiveTime,
}
#[derive(
Serialize, Deserialize, diesel::FromSqlRow, diesel::AsExpression, PartialEq, Eq, Clone, Debug,
)]
#[diesel(sql_type = schema::sql_types::WeekType)]
pub enum WeekType {
A,
B,
}
impl Display for WeekType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
Self::A => "A",
Self::B => "B",
}
)
}
}
impl FromStr for WeekType {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"A" => Ok(Self::A),
"B" => Ok(Self::B),
_ => bail!("Unrecognized enum variant"),
}
}
}
impl diesel::serialize::ToSql<schema::sql_types::WeekType, diesel::pg::Pg> for WeekType {
fn to_sql<'b>(
&'b self,
out: &mut diesel::serialize::Output<'b, '_, diesel::pg::Pg>,
) -> diesel::serialize::Result {
match *self {
Self::A => out.write_all(b"a")?,
Self::B => out.write_all(b"b")?,
}
Ok(diesel::serialize::IsNull::No)
}
}
impl diesel::deserialize::FromSql<schema::sql_types::WeekType, diesel::pg::Pg> for WeekType {
fn from_sql(
bytes: diesel::backend::RawValue<'_, diesel::pg::Pg>,
) -> diesel::deserialize::Result<Self> {
match bytes.as_bytes() {
b"a" => Ok(Self::A),
b"b" => Ok(Self::B),
_ => Err("Unrecognized enum variant".into()),
}
}
}
#[derive(Identifiable, Queryable, Associations, Clone, Debug)]
#[diesel(table_name = schema::substitution_queries)]
#[diesel(belongs_to(Schoolyear))]
pub struct SubstitutionQuery {
pub id: i32,
pub schoolyear_id: i32,
pub date: NaiveDate,
pub week_type: WeekType,
pub queried_at: NaiveDateTime,
pub created_at: NaiveDateTime,
pub updated_at: Option<NaiveDateTime>,
}
#[derive(Insertable, Debug)]
#[diesel(table_name = schema::substitution_queries)]
pub struct NewSubstitutionQuery {
pub schoolyear_id: i32,
pub date: NaiveDate,
pub week_type: WeekType,
pub queried_at: NaiveDateTime,
}
#[derive(diesel::FromSqlRow, diesel::AsExpression, PartialEq, Eq, Clone, Debug)]
#[diesel(sql_type = schema::sql_types::SubstitutionType)]
pub enum SubstitutionType {
Cancel,
Substitution,
Additional,
Shifted,
RoomChange,
Locked,
BreakSupervision,
OfficeHour,
Standby,
Other,
Free,
Exam,
Activity,
Holiday,
Text,
}
impl diesel::serialize::ToSql<schema::sql_types::SubstitutionType, diesel::pg::Pg>
for SubstitutionType
{
fn to_sql<'b>(
&'b self,
out: &mut diesel::serialize::Output<'b, '_, diesel::pg::Pg>,
) -> diesel::serialize::Result {
match *self {
Self::Cancel => out.write_all(b"cancel")?,
Self::Substitution => out.write_all(b"subst")?,
Self::Additional => out.write_all(b"add")?,
Self::Shifted => out.write_all(b"shift")?,
Self::RoomChange => out.write_all(b"rmchg")?,
Self::Locked => out.write_all(b"rmlk")?,
Self::BreakSupervision => out.write_all(b"bs")?,
Self::OfficeHour => out.write_all(b"oh")?,
Self::Standby => out.write_all(b"sb")?,
Self::Other => out.write_all(b"other")?,
Self::Free => out.write_all(b"free")?,
Self::Exam => out.write_all(b"exam")?,
Self::Activity => out.write_all(b"ac")?,
Self::Holiday => out.write_all(b"holi")?,
Self::Text => out.write_all(b"stxt")?,
}
Ok(diesel::serialize::IsNull::No)
}
}
impl diesel::deserialize::FromSql<schema::sql_types::SubstitutionType, diesel::pg::Pg>
for SubstitutionType
{
fn from_sql(
bytes: diesel::backend::RawValue<'_, diesel::pg::Pg>,
) -> diesel::deserialize::Result<Self> {
match bytes.as_bytes() {
b"cancel" => Ok(Self::Cancel),
b"subst" => Ok(Self::Substitution),
b"add" => Ok(Self::Additional),
b"shift" => Ok(Self::Shifted),
b"rmchg" => Ok(Self::RoomChange),
b"rmlk" => Ok(Self::Locked),
b"bs" => Ok(Self::BreakSupervision),
b"oh" => Ok(Self::OfficeHour),
b"sb" => Ok(Self::Standby),
b"other" => Ok(Self::Other),
b"free" => Ok(Self::Free),
b"exam" => Ok(Self::Exam),
b"ac" => Ok(Self::Activity),
b"holi" => Ok(Self::Holiday),
b"stxt" => Ok(Self::Text),
_ => Err("Unrecognized enum variant".into()),
}
}
}
impl From<untis::RpcSubstitionType> for SubstitutionType {
fn from(x: untis::RpcSubstitionType) -> Self {
match x {
untis::RpcSubstitionType::Cancel => Self::Cancel,
untis::RpcSubstitionType::Substitution => Self::Substitution,
untis::RpcSubstitionType::Additional => Self::Additional,
untis::RpcSubstitionType::Shifted => Self::Shifted,
untis::RpcSubstitionType::RoomChange => Self::RoomChange,
untis::RpcSubstitionType::Locked => Self::Locked,
untis::RpcSubstitionType::BreakSupervision => Self::BreakSupervision,
untis::RpcSubstitionType::OfficeHour => Self::OfficeHour,
untis::RpcSubstitionType::Standby => Self::Standby,
untis::RpcSubstitionType::Other => Self::Other,
untis::RpcSubstitionType::Free => Self::Free,
untis::RpcSubstitionType::Exam => Self::Exam,
untis::RpcSubstitionType::Activity => Self::Activity,
untis::RpcSubstitionType::Holiday => Self::Holiday,
untis::RpcSubstitionType::Text => Self::Text,
}
}
}
#[derive(Identifiable, Queryable, Associations, Debug)]
#[diesel(table_name = schema::substitutions)]
#[diesel(belongs_to(SubstitutionQuery))]
pub struct Substitution {
pub id: i32,
pub substitution_query_id: i32,
pub subst_type: SubstitutionType,
pub lesson_id: i32,
pub start_time: NaiveTime,
pub end_time: NaiveTime,
pub text: Option<String>,
pub created_at: NaiveDateTime,
pub updated_at: Option<NaiveDateTime>,
}
#[derive(Insertable, Debug)]
#[diesel(table_name = schema::substitutions)]
pub struct NewSubstitution<'a> {
pub substitution_query_id: i32,
pub subst_type: SubstitutionType,
pub lesson_id: i32,
pub start_time: NaiveTime,
pub end_time: NaiveTime,
pub text: Option<&'a str>,
}
#[derive(Identifiable, Queryable, Associations, Debug)]
#[diesel(table_name = schema::substitution_classes)]
#[diesel(belongs_to(Substitution))]
#[diesel(belongs_to(Class))]
pub struct SubstitutionClass {
pub id: i32,
pub substitution_id: i32,
pub position: i16,
pub class_id: i32,
pub original_id: Option<i32>,
pub created_at: NaiveDateTime,
pub updated_at: Option<NaiveDateTime>,
}
#[derive(Insertable, Debug)]
#[diesel(table_name = schema::substitution_classes)]
pub struct NewSubstitutionClass {
pub substitution_id: i32,
pub position: i16,
pub class_id: i32,
pub original_id: Option<i32>,
}
#[derive(Identifiable, Queryable, Associations, Debug)]
#[diesel(table_name = schema::substitution_teachers)]
#[diesel(belongs_to(Substitution))]
#[diesel(belongs_to(Teacher))]
pub struct SubstitutionTeacher {
pub id: i32,
pub substitution_id: i32,
pub position: i16,
pub teacher_id: Option<i32>,
pub original_id: Option<i32>,
pub created_at: NaiveDateTime,
pub updated_at: Option<NaiveDateTime>,
}
#[derive(Insertable, Debug)]
#[diesel(table_name = schema::substitution_teachers)]
pub struct NewSubstitutionTeacher {
pub substitution_id: i32,
pub position: i16,
pub teacher_id: Option<i32>,
pub original_id: Option<i32>,
}
#[derive(Identifiable, Queryable, Associations, Debug)]
#[diesel(table_name = schema::substitution_subjects)]
#[diesel(belongs_to(Substitution))]
#[diesel(belongs_to(Subject))]
pub struct SubstitutionSubject {
pub id: i32,
pub substitution_id: i32,
pub position: i16,
pub subject_id: i32,
pub original_id: Option<i32>,
pub created_at: NaiveDateTime,
pub updated_at: Option<NaiveDateTime>,
}
#[derive(Insertable, Debug)]
#[diesel(table_name = schema::substitution_subjects)]
pub struct NewSubstitutionSubject {
pub substitution_id: i32,
pub position: i16,
pub subject_id: i32,
pub original_id: Option<i32>,
}
#[derive(Identifiable, Queryable, Associations, Debug)]
#[diesel(table_name = schema::substitution_rooms)]
#[diesel(belongs_to(Substitution))]
#[diesel(belongs_to(Room))]
pub struct SubstitutionRoom {
pub id: i32,
pub substitution_id: i32,
pub position: i16,
pub room_id: Option<i32>,
pub original_id: Option<i32>,
pub created_at: NaiveDateTime,
pub updated_at: Option<NaiveDateTime>,
}
#[derive(Insertable, Debug)]
#[diesel(table_name = schema::substitution_rooms)]
pub struct NewSubstitutionRoom {
pub substitution_id: i32,
pub position: i16,
pub room_id: Option<i32>,
pub original_id: Option<i32>,
}