From 94a9d285be59ba841c82ed07d2cb8712708fe8ca Mon Sep 17 00:00:00 2001 From: philipp Date: Sat, 5 Aug 2023 16:27:51 +0200 Subject: [PATCH] clean db --- migration.sql | 25 +++++++++---------------- src/model/logbook.rs | 26 +++++++++++--------------- src/tera/log.rs | 25 ++++++++++++++++++++++++- 3 files changed, 44 insertions(+), 32 deletions(-) diff --git a/migration.sql b/migration.sql index b7d1d6e..99daf39 100644 --- a/migration.sql +++ b/migration.sql @@ -26,37 +26,30 @@ CREATE TABLE IF NOT EXISTS "trip_details" ( "allow_guests" boolean NOT NULL default false, "notes" TEXT, "always_show" boolean NOT NULL default false, - "trip_type_id" INTEGER, - FOREIGN KEY(trip_type_id) REFERENCES trip_type(id) + "trip_type_id" INTEGER REFERENCES trip_type(id) ON DELETE CASCADE ); CREATE TABLE IF NOT EXISTS "planned_event" ( "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name" text NOT NULL, "planned_amount_cox" INTEGER unsigned NOT NULL, - "trip_details_id" INTEGER NOT NULL, + "trip_details_id" INTEGER NOT NULL REFERENCES TRIP_details(id) ON DELETE CASCADE, "created_at" text NOT NULL DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY(trip_details_id) REFERENCES trip_details(id) ON DELETE CASCADE ); CREATE TABLE IF NOT EXISTS "trip" ( "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, - "cox_id" INTEGER NOT NULL, - "trip_details_id" INTEGER, - "planned_event_id" INTEGER, + "cox_id" INTEGER NOT NULL REFERENCES user(id), + "trip_details_id" INTEGER REFERENCES trip_details(id) ON DELETE CASCADE, + "planned_event_id" INTEGER REFERENCES planned_event(id) ON DELETE CASCADE, "created_at" text NOT NULL DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY(cox_id) REFERENCES user(id), - FOREIGN KEY(trip_details_id) REFERENCES trip_details(id) ON DELETE CASCADE, - FOREIGN KEY(planned_event_id) REFERENCES planned_event(id) ON DELETE CASCADE, CONSTRAINT unq UNIQUE (cox_id, planned_event_id) -- allow cox to participate only once for each planned event ); CREATE TABLE IF NOT EXISTS "user_trip" ( - "user_id" INTEGER NOT NULL, - "trip_details_id" INTEGER NOT NULL, + "user_id" INTEGER NOT NULL REFERENCES user(id), + "trip_details_id" INTEGER NOT NULL REFERENCES trip_details(id), "created_at" text NOT NULL DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY(user_id) REFERENCES user(id), - FOREIGN KEY(trip_details_id) REFERENCES trip_details(id), CONSTRAINT unq UNIQUE (user_id, trip_details_id) -- allow user to participate only once for each trip ); @@ -103,9 +96,9 @@ CREATE TABLE IF NOT EXISTS "logbook" ( ); CREATE TABLE IF NOT EXISTS "rower" ( - "logbook_id" INTEGER NOT NULL REFERENCES logbook(id), + "logbook_id" INTEGER NOT NULL REFERENCES logbook(id) ON DELETE CASCADE, "rower_id" INTEGER NOT NULL REFERENCES user(id), - CONSTRAINT unq UNIQUE (logbook_id, rower_id) + CONSTRAINT unq UNIQUE (logbook_id, rower_id) ); CREATE TABLE IF NOT EXISTS "boat_damage" ( diff --git a/src/model/logbook.rs b/src/model/logbook.rs index 9781476..1ea7cbc 100644 --- a/src/model/logbook.rs +++ b/src/model/logbook.rs @@ -12,8 +12,8 @@ pub struct Logbook { pub shipmaster: i64, #[serde(default = "bool::default")] pub shipmaster_only_steering: bool, - pub departure: String, //TODO: Switch to chrono::nativedatetime - pub arrival: Option, //TODO: Switch to chrono::nativedatetime + pub departure: NaiveDateTime, + pub arrival: Option, pub destination: Option, pub distance_in_km: Option, pub comments: Option, @@ -114,7 +114,8 @@ ORDER BY departure DESC boat_id: row.boat_id, shipmaster: row.shipmaster, shipmaster_only_steering: row.shipmaster_only_steering, - departure: row.departure.unwrap(), + departure: NaiveDateTime::parse_from_str(&row.departure.unwrap(), "%Y-%m-%d %H:%M") + .unwrap(), arrival: row.arrival, destination: row.destination, distance_in_km: row.distance_in_km, @@ -125,12 +126,7 @@ ORDER BY departure DESC let mut ret = Vec::new(); for log in logs { - let date_time_naive = - NaiveDateTime::parse_from_str(&log.departure, "%Y-%m-%d %H:%M").unwrap(); - let date_time = Local - .from_local_datetime(&date_time_naive) - .single() - .unwrap(); + let date_time = Local.from_local_datetime(&log.departure).single().unwrap(); ret.push(LogbookWithBoatAndRowers { rowers: Rower::for_log(db, &log).await, @@ -313,12 +309,12 @@ ORDER BY departure DESC Ok(()) } - // pub async fn delete(&self, db: &SqlitePool) { - // sqlx::query!("DELETE FROM boat WHERE id=?", self.id) - // .execute(db) - // .await - // .unwrap(); //Okay, because we can only create a User of a valid id - // } + pub async fn delete(&self, db: &SqlitePool) { + sqlx::query!("DELETE FROM logbook WHERE id=?", self.id) + .execute(db) + .await + .unwrap(); //Okay, because we can only create a Logbook of a valid id + } } #[cfg(test)] diff --git a/src/tera/log.rs b/src/tera/log.rs index 3d0cab5..b893318 100644 --- a/src/tera/log.rs +++ b/src/tera/log.rs @@ -205,6 +205,28 @@ async fn home( home_logbook(db, data, logbook_id, &adminuser.user).await } +#[get("//delete")] +async fn delete( + db: &State, + flash: Option>, + logbook_id: i32, + _adminuser: AdminUser, +) -> Flash { + let logbook = Logbook::find_by_id(db, logbook_id).await; + if let Some(logbook) = logbook { + logbook.delete(db).await; + Flash::success( + Redirect::to("/log"), + format!("Logbook with ID {} successfully deleted!", logbook_id), + ) + } else { + Flash::error( + Redirect::to("/log"), + format!("Logbook with ID {} could not be found!", logbook_id), + ) + } +} + pub fn routes() -> Vec { routes![ index, @@ -215,7 +237,8 @@ pub fn routes() -> Vec { home_kiosk, new_kiosk, show, - show_kiosk + show_kiosk, + delete ] }