This commit is contained in:
philipp 2023-08-05 16:27:51 +02:00
parent 64aefde8bc
commit 94a9d285be
3 changed files with 44 additions and 32 deletions

View File

@ -26,37 +26,30 @@ CREATE TABLE IF NOT EXISTS "trip_details" (
"allow_guests" boolean NOT NULL default false, "allow_guests" boolean NOT NULL default false,
"notes" TEXT, "notes" TEXT,
"always_show" boolean NOT NULL default false, "always_show" boolean NOT NULL default false,
"trip_type_id" INTEGER, "trip_type_id" INTEGER REFERENCES trip_type(id) ON DELETE CASCADE
FOREIGN KEY(trip_type_id) REFERENCES trip_type(id)
); );
CREATE TABLE IF NOT EXISTS "planned_event" ( CREATE TABLE IF NOT EXISTS "planned_event" (
"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
"name" text NOT NULL, "name" text NOT NULL,
"planned_amount_cox" INTEGER unsigned 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, "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" ( CREATE TABLE IF NOT EXISTS "trip" (
"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
"cox_id" INTEGER NOT NULL, "cox_id" INTEGER NOT NULL REFERENCES user(id),
"trip_details_id" INTEGER, "trip_details_id" INTEGER REFERENCES trip_details(id) ON DELETE CASCADE,
"planned_event_id" INTEGER, "planned_event_id" INTEGER REFERENCES planned_event(id) ON DELETE CASCADE,
"created_at" text NOT NULL DEFAULT CURRENT_TIMESTAMP, "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 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" ( CREATE TABLE IF NOT EXISTS "user_trip" (
"user_id" INTEGER NOT NULL, "user_id" INTEGER NOT NULL REFERENCES user(id),
"trip_details_id" INTEGER NOT NULL, "trip_details_id" INTEGER NOT NULL REFERENCES trip_details(id),
"created_at" text NOT NULL DEFAULT CURRENT_TIMESTAMP, "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 CONSTRAINT unq UNIQUE (user_id, trip_details_id) -- allow user to participate only once for each trip
); );
@ -103,7 +96,7 @@ CREATE TABLE IF NOT EXISTS "logbook" (
); );
CREATE TABLE IF NOT EXISTS "rower" ( 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), "rower_id" INTEGER NOT NULL REFERENCES user(id),
CONSTRAINT unq UNIQUE (logbook_id, rower_id) CONSTRAINT unq UNIQUE (logbook_id, rower_id)
); );

View File

@ -12,8 +12,8 @@ pub struct Logbook {
pub shipmaster: i64, pub shipmaster: i64,
#[serde(default = "bool::default")] #[serde(default = "bool::default")]
pub shipmaster_only_steering: bool, pub shipmaster_only_steering: bool,
pub departure: String, //TODO: Switch to chrono::nativedatetime pub departure: NaiveDateTime,
pub arrival: Option<String>, //TODO: Switch to chrono::nativedatetime pub arrival: Option<NaiveDateTime>,
pub destination: Option<String>, pub destination: Option<String>,
pub distance_in_km: Option<i64>, pub distance_in_km: Option<i64>,
pub comments: Option<String>, pub comments: Option<String>,
@ -114,7 +114,8 @@ ORDER BY departure DESC
boat_id: row.boat_id, boat_id: row.boat_id,
shipmaster: row.shipmaster, shipmaster: row.shipmaster,
shipmaster_only_steering: row.shipmaster_only_steering, 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, arrival: row.arrival,
destination: row.destination, destination: row.destination,
distance_in_km: row.distance_in_km, distance_in_km: row.distance_in_km,
@ -125,12 +126,7 @@ ORDER BY departure DESC
let mut ret = Vec::new(); let mut ret = Vec::new();
for log in logs { for log in logs {
let date_time_naive = let date_time = Local.from_local_datetime(&log.departure).single().unwrap();
NaiveDateTime::parse_from_str(&log.departure, "%Y-%m-%d %H:%M").unwrap();
let date_time = Local
.from_local_datetime(&date_time_naive)
.single()
.unwrap();
ret.push(LogbookWithBoatAndRowers { ret.push(LogbookWithBoatAndRowers {
rowers: Rower::for_log(db, &log).await, rowers: Rower::for_log(db, &log).await,
@ -313,12 +309,12 @@ ORDER BY departure DESC
Ok(()) Ok(())
} }
// pub async fn delete(&self, db: &SqlitePool) { pub async fn delete(&self, db: &SqlitePool) {
// sqlx::query!("DELETE FROM boat WHERE id=?", self.id) sqlx::query!("DELETE FROM logbook WHERE id=?", self.id)
// .execute(db) .execute(db)
// .await .await
// .unwrap(); //Okay, because we can only create a User of a valid id .unwrap(); //Okay, because we can only create a Logbook of a valid id
// } }
} }
#[cfg(test)] #[cfg(test)]

View File

@ -205,6 +205,28 @@ async fn home(
home_logbook(db, data, logbook_id, &adminuser.user).await home_logbook(db, data, logbook_id, &adminuser.user).await
} }
#[get("/<logbook_id>/delete")]
async fn delete(
db: &State<SqlitePool>,
flash: Option<FlashMessage<'_>>,
logbook_id: i32,
_adminuser: AdminUser,
) -> Flash<Redirect> {
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<Route> { pub fn routes() -> Vec<Route> {
routes![ routes![
index, index,
@ -215,7 +237,8 @@ pub fn routes() -> Vec<Route> {
home_kiosk, home_kiosk,
new_kiosk, new_kiosk,
show, show,
show_kiosk show_kiosk,
delete
] ]
} }