datum hinzu fügen

This commit is contained in:
2024-01-31 12:01:04 +01:00
parent 1cb3090f64
commit f79cbc36c4
12 changed files with 1138 additions and 657 deletions
@@ -0,0 +1,49 @@
-- Add migration script here
CREATE TABLE date (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
subtitle TEXT NOT NULL,
start_time DATETIME NOT NULL,
end_time DATETIME NOT NULL
);
INSERT INTO date (id, name, subtitle, start_time, end_time)
VALUES (
1,
'OS 2023',
'2023',
'2023-02-28 12:00:00',
'2023-02-28 18:00:00'
);
-- Create a temporary table
CREATE TEMPORARY TABLE temp_appointment_slots AS
SELECT *
FROM appointment_slots;
DROP TABLE appointment_slots;
-- Recreate the appointments table with the new column and foreign key constraint
CREATE TABLE appointment_slots (
id INTEGER PRIMARY KEY,
start_time DATETIME NOT NULL,
end_time DATETIME NOT NULL,
date_id INTEGER DEFAULT 1 Not NULL,
-- Add the new column
FOREIGN KEY (date_id) REFERENCES date(id) -- Add the foreign key constraint
);
-- Insert data back into the new appointments table from the temporary table
INSERT INTO appointment_slots
SELECT *,
1
FROM temp_appointment_slots;
-- Drop the temporary table
DROP TABLE temp_appointment_slots;
CREATE TABLE teacher_dates (
teacher_id INTEGER NOT NULL,
date_id INTEGER NOT NULL,
PRIMARY KEY (teacher_id, date_id),
FOREIGN KEY (teacher_id) REFERENCES teachers(id),
FOREIGN KEY (date_id) REFERENCES date(id)
);
INSERT INTO teacher_dates (teacher_id, date_id)
SELECT teachers.id AS teacher_id,
date.id AS date_id
FROM teachers,
date;
+13 -4
View File
@@ -1,9 +1,14 @@
use actix_web::{error, web, Error, HttpResponse};
use terminwahl_typen::IdType;
use crate::db::{self, Pool};
pub async fn get_teachers_json(pool: web::Data<Pool>) -> Result<HttpResponse, Error> {
let tasks = db::read::get_teachers(&pool)
pub async fn get_teachers_json(
pool: web::Data<Pool>,
path: web::Path<IdType>,
) -> Result<HttpResponse, Error> {
let date_id = path.into_inner();
let tasks = db::read::get_teachers(&pool, date_id)
.await
.map_err(error::ErrorInternalServerError)?;
@@ -17,8 +22,12 @@ pub async fn get_subjects_json(pool: web::Data<Pool>) -> Result<HttpResponse, Er
Ok(HttpResponse::Ok().json(tasks))
}
pub async fn get_slots_json(pool: web::Data<Pool>) -> Result<HttpResponse, Error> {
let tasks = db::read::get_slots(&pool)
pub async fn get_slots_json(
pool: web::Data<Pool>,
path: web::Path<IdType>,
) -> Result<HttpResponse, Error> {
let date_id = path.into_inner();
let tasks = db::read::get_slots(&pool, date_id)
.await
.map_err(error::ErrorInternalServerError)?;
+17 -8
View File
@@ -10,12 +10,17 @@ use terminwahl_typen::{
use super::Pool;
pub async fn get_teachers(db: &Pool) -> Result<Teachers, sqlx::Error> {
pub async fn get_teachers(db: &Pool, date_id: IdType) -> Result<Teachers, sqlx::Error> {
query_as!(
Teacher,
r#"
SELECT *
FROM `teachers`"#,
SELECT
id,
ansprache,
last_name,
subject_id
FROM `teachers` JOIN teacher_dates ON teachers.id = teacher_dates.teacher_id where date_id = ?"#,
date_id
)
.fetch_all(db)
.await
@@ -44,12 +49,13 @@ pub async fn get_subjects(db: &Pool) -> Result<Subjects, sqlx::Error> {
.await
}
pub async fn get_slots(db: &Pool) -> Result<AppointmentSlots, sqlx::Error> {
pub async fn get_slots(db: &Pool, date_id: IdType) -> Result<AppointmentSlots, sqlx::Error> {
match query_as!(
AppointmentSlot,
r#"
SELECT *
FROM `appointment_slots`"#,
SELECT id, start_time, end_time, date_id
FROM `appointment_slots` where date_id = ?"#,
date_id
)
.fetch_all(db)
.await
@@ -109,8 +115,11 @@ pub struct AssignedAppointment {
nutzer: Nutzer,
}
pub async fn get_all_teachers(db: &Pool) -> Result<Vec<TeacherWithAppointments>, sqlx::Error> {
let teachers = get_teachers(db).await?;
pub async fn get_all_teachers(
db: &Pool,
date_id: IdType,
) -> Result<Vec<TeacherWithAppointments>, sqlx::Error> {
let teachers = get_teachers(db, date_id).await?;
let mut response = Vec::new();
for teacher in teachers.into_iter() {
+6 -2
View File
@@ -70,12 +70,16 @@ async fn main() -> std::io::Result<()> {
.wrap(session_store)
.wrap(error_handlers)
.service(
web::resource("/get/teachers").route(web::get().to(api::read::get_teachers_json)),
web::resource("/get/teachers/{date_key}")
.route(web::get().to(api::read::get_teachers_json)),
)
.service(
web::resource("/get/subjects").route(web::get().to(api::read::get_subjects_json)),
)
.service(web::resource("/get/slots").route(web::get().to(api::read::get_slots_json)))
.service(
web::resource("/get/slots/{date_key}")
.route(web::get().to(api::read::get_slots_json)),
)
.service(
web::resource("/get/unavailable")
.route(web::get().to(api::read::get_unavailable_json)),
+3 -3
View File
@@ -60,12 +60,12 @@ pub async fn export_appointments(
_mailer: web::Data<AsyncSmtpTransport<Tokio1Executor>>,
handlebars: web::Data<Handlebars<'_>>,
css: web::Data<CssPath>,
path: web::Path<String>,
path: web::Path<(String, IdType)>,
) -> Result<HttpResponse, error::Error> {
let password = path.into_inner();
let (password, date_id) = path.into_inner();
dbg!(&password);
if password == "AllExport1517" {
match get_all_teachers(&pool).await {
match get_all_teachers(&pool, date_id).await {
Ok(teachers) => {
dbg!(&teachers);
let data = json!({