Compare commits

..

No commits in common. "7e41cd3f7371d90e5fc4244605aca8d0009a12c6" and "2d2e44126a01b13a16f5a4a301626ec7ea8dcbfc" have entirely different histories.

2 changed files with 30 additions and 51 deletions

View File

@ -1,6 +1,6 @@
use std::ops::DerefMut; use std::ops::DerefMut;
use chrono::{Datelike, Duration, Local, NaiveDateTime}; use chrono::{Datelike, Local, NaiveDateTime};
use rocket::FromForm; use rocket::FromForm;
use serde::Serialize; use serde::Serialize;
use sqlx::{FromRow, Sqlite, SqlitePool, Transaction}; use sqlx::{FromRow, Sqlite, SqlitePool, Transaction};
@ -110,20 +110,6 @@ pub struct LogbookWithBoatAndRowers {
pub rowers: Vec<User>, pub rowers: Vec<User>,
} }
impl LogbookWithBoatAndRowers {
pub(crate) async fn from(db: &SqlitePool, log: Logbook) -> Self {
Self {
rowers: Rower::for_log(db, &log).await,
boat: Boat::find_by_id(db, log.boat_id as i32).await.unwrap(),
shipmaster_user: User::find_by_id(db, log.shipmaster as i32).await.unwrap(),
steering_user: User::find_by_id(db, log.steering_person as i32)
.await
.unwrap(),
logbook: log,
}
}
}
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum LogbookAdminUpdateError { pub enum LogbookAdminUpdateError {
NotAllowed, NotAllowed,
@ -262,7 +248,15 @@ ORDER BY departure DESC
let mut ret = Vec::new(); let mut ret = Vec::new();
for log in logs { for log in logs {
ret.push(LogbookWithBoatAndRowers::from(db, log).await); ret.push(LogbookWithBoatAndRowers {
rowers: Rower::for_log(db, &log).await,
boat: Boat::find_by_id(db, log.boat_id as i32).await.unwrap(),
shipmaster_user: User::find_by_id(db, log.shipmaster as i32).await.unwrap(),
steering_user: User::find_by_id(db, log.steering_person as i32)
.await
.unwrap(),
logbook: log,
});
} }
ret ret
} }
@ -286,7 +280,15 @@ ORDER BY departure DESC
let mut ret = Vec::new(); let mut ret = Vec::new();
for log in logs { for log in logs {
ret.push(LogbookWithBoatAndRowers::from(db, log).await); ret.push(LogbookWithBoatAndRowers {
rowers: Rower::for_log(db, &log).await,
boat: Boat::find_by_id(db, log.boat_id as i32).await.unwrap(),
shipmaster_user: User::find_by_id(db, log.shipmaster as i32).await.unwrap(),
steering_user: User::find_by_id(db, log.steering_person as i32)
.await
.unwrap(),
logbook: log,
});
} }
ret ret
} }
@ -311,7 +313,15 @@ ORDER BY departure DESC
let mut ret = Vec::new(); let mut ret = Vec::new();
for log in logs { for log in logs {
ret.push(LogbookWithBoatAndRowers::from(db, log).await); ret.push(LogbookWithBoatAndRowers {
rowers: Rower::for_log(db, &log).await,
boat: Boat::find_by_id(db, log.boat_id as i32).await.unwrap(),
shipmaster_user: User::find_by_id(db, log.shipmaster as i32).await.unwrap(),
steering_user: User::find_by_id(db, log.steering_person as i32)
.await
.unwrap(),
logbook: log,
});
} }
ret ret
} }
@ -703,37 +713,6 @@ ORDER BY departure DESC
|| user.has_role(db, "Vorstand").await || user.has_role(db, "Vorstand").await
|| user.id == self.shipmaster || user.id == self.shipmaster
{ {
let now = Local::now().naive_local();
let difference = now - self.departure;
if difference > Duration::hours(1) {
let vorstand = Role::find_by_name(db, "Vorstand").await.unwrap();
let logbook = LogbookWithBoatAndRowers::from(db, self.clone()).await;
let mut msg = format!("{} hat folgenden Logbuch-Eintrag jetzt gelöscht, welcher bereits vor über einer Stunde begonnen wurde: Schiffsführer: {}, Steuerperson: {}, Abfahrt: {}", user.name, logbook.steering_user.name, logbook.steering_user.name, logbook.logbook.departure.format("%Y-%m-%d %H:%M"));
if let Some(destination) = logbook.logbook.destination {
msg.push_str(&format!(", Ziel: {}", destination));
} else {
msg.push_str(", kein Ziel eingegeben");
}
msg.push_str(", Ruderer: ");
let mut it = logbook.rowers.clone().into_iter().peekable();
while let Some(rower) = it.next() {
msg.push_str(&rower.name);
if it.peek().is_some() {
msg.push_str(" + ");
}
}
Notification::create_for_role(
db,
&vorstand,
&msg,
"Ungewöhnliches Verhalten",
None,
None,
)
.await;
}
sqlx::query!("DELETE FROM logbook WHERE id=?", self.id) sqlx::query!("DELETE FROM logbook WHERE id=?", self.id)
.execute(db) .execute(db)
.await .await

View File

@ -311,12 +311,12 @@ async fn update(
Flash::success( Flash::success(
Redirect::to("/log/show"), Redirect::to("/log/show"),
"Logbucheintrag erfolgreich bearbeitet".to_string(), format!("Logbucheintrag erfolgreich bearbeitet"),
) )
} }
Err(LogbookAdminUpdateError::NotAllowed) => Flash::error( Err(LogbookAdminUpdateError::NotAllowed) => Flash::error(
Redirect::to("/log/show"), Redirect::to("/log/show"),
"Du hast keine Erlaubnis, diesen Logbucheintrag zu bearbeiten!".to_string(), format!("Du hast keine Erlaubnis, diesen Logbucheintrag zu bearbeiten!"),
), ),
} }
} }