bvplan/backend/src/db/models.rs
2022-12-12 17:54:07 +01:00

447 lines
13 KiB
Rust

use chrono::prelude::*;
use diesel::prelude::*;
use std::io::Write;
use crate::db::schema;
use crate::untis;
#[derive(Identifiable, Queryable, Debug)]
#[diesel(table_name = schema::schoolyears)]
pub struct Schoolyear {
pub id: i32,
pub untis_id: i32,
pub 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::schoolyears)]
pub struct NewSchoolyear<'a> {
pub untis_id: i32,
pub name: &'a str,
pub start_date: NaiveDate,
pub end_date: NaiveDate,
}
#[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(table_name = schema::holidays)]
#[diesel(belongs_to(Schoolyear))]
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(diesel::FromSqlRow, diesel::AsExpression, PartialEq, Eq, 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, Debug)]
#[diesel(table_name = schema::substitutions)]
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 active: bool,
pub created_at: NaiveDateTime,
pub updated_at: Option<NaiveDateTime>,
}
#[derive(Insertable, Debug)]
#[diesel(table_name = schema::substitutions)]
pub struct NewSubstitution<'a> {
pub subst_type: SubstitutionType,
pub lesson_id: i32,
pub start_time: NaiveTime,
pub end_time: NaiveTime,
pub text: Option<&'a str>,
pub active: bool,
}
#[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 class_id: 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 class_id: 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 teacher_id: 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 teacher_id: 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 subject_id: 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 subject_id: 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 room_id: 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 room_id: i32,
}
#[derive(Identifiable, Queryable, Associations, Debug)]
#[diesel(table_name = schema::substitution_planned_queries)]
#[diesel(belongs_to(Schoolyear))]
pub struct SubstitutionPlannedQuery {
pub id: i32,
pub schoolyear_id: i32,
pub date: NaiveDate,
pub active: bool,
pub created_at: NaiveDateTime,
pub updated_at: Option<NaiveDateTime>,
}
#[derive(Insertable, Debug)]
#[diesel(table_name = schema::substitution_planned_queries)]
pub struct NewSubstitutionPlannedQuery {
pub schoolyear_id: i32,
pub date: NaiveDate,
pub active: bool,
}
#[derive(Identifiable, Queryable, Associations, Debug)]
#[diesel(table_name = schema::substitution_queries)]
#[diesel(belongs_to(SubstitutionPlannedQuery))]
pub struct SubstitutionQuery {
pub id: i32,
pub substitution_planned_query_id: i32,
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 substitution_planned_query_id: i32,
pub queried_at: NaiveDateTime,
}
#[derive(Identifiable, Queryable, Associations, Debug)]
#[diesel(table_name = schema::substitution_query_results)]
#[diesel(belongs_to(SubstitutionQuery))]
#[diesel(belongs_to(Substitution))]
pub struct SubstitutionQueryResult {
pub id: i32,
pub substitution_query_id: i32,
pub substitution_id: i32,
pub created_at: NaiveDateTime,
pub updated_at: Option<NaiveDateTime>,
}
#[derive(Insertable, Debug)]
#[diesel(table_name = schema::substitution_query_results)]
pub struct NewSubstitutionQueryResult {
pub substitution_query_id: i32,
pub substitution_id: i32,
}