update logbook entries
This commit is contained in:
@ -60,6 +60,22 @@ pub struct LogToFinalize {
|
||||
pub rowers: Vec<i64>,
|
||||
}
|
||||
|
||||
#[derive(FromForm, Debug, Clone)]
|
||||
pub struct LogToUpdate {
|
||||
pub id: i64,
|
||||
pub boat_id: i64,
|
||||
pub shipmaster: i64,
|
||||
pub steering_person: i64,
|
||||
pub shipmaster_only_steering: bool,
|
||||
pub departure: String,
|
||||
pub arrival: Option<String>,
|
||||
pub destination: Option<String>,
|
||||
pub distance_in_km: Option<i64>,
|
||||
pub comments: Option<String>,
|
||||
pub logtype: Option<i64>,
|
||||
pub rowers: Vec<i64>,
|
||||
}
|
||||
|
||||
impl TryFrom<LogToAdd> for LogToFinalize {
|
||||
type Error = String;
|
||||
|
||||
@ -94,6 +110,11 @@ pub struct LogbookWithBoatAndRowers {
|
||||
pub rowers: Vec<User>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum LogbookAdminUpdateError {
|
||||
NotAllowed,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum LogbookUpdateError {
|
||||
NotYourEntry,
|
||||
@ -172,7 +193,7 @@ impl Logbook {
|
||||
.await
|
||||
.ok()
|
||||
}
|
||||
pub async fn find_by_id(db: &SqlitePool, id: i32) -> Option<Self> {
|
||||
pub async fn find_by_id(db: &SqlitePool, id: i64) -> Option<Self> {
|
||||
sqlx::query_as!(
|
||||
Self,
|
||||
"
|
||||
@ -437,6 +458,35 @@ ORDER BY departure DESC
|
||||
Ok(ret)
|
||||
}
|
||||
|
||||
pub async fn update(
|
||||
&self,
|
||||
db: &SqlitePool,
|
||||
data: LogToUpdate,
|
||||
user: &User,
|
||||
) -> Result<(), LogbookAdminUpdateError> {
|
||||
if !user.has_role(db, "Vorstand").await {
|
||||
return Err(LogbookAdminUpdateError::NotAllowed);
|
||||
}
|
||||
|
||||
sqlx::query!(
|
||||
"UPDATE logbook SET boat_id=?, shipmaster=?, steering_person=?, shipmaster_only_steering=?, departure=?, arrival=?, destination=?, distance_in_km=?, comments=?, logtype=? WHERE id=?",
|
||||
data.boat_id,
|
||||
data.shipmaster,
|
||||
data.steering_person,
|
||||
data.shipmaster_only_steering,
|
||||
data.departure,
|
||||
data.arrival,
|
||||
data.destination,
|
||||
data.distance_in_km,
|
||||
data.comments,
|
||||
data.logtype,
|
||||
self.id
|
||||
)
|
||||
.execute(db)
|
||||
.await.unwrap();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn distances(db: &SqlitePool) -> Vec<(String, i64)> {
|
||||
let result = sqlx::query!("SELECT destination, distance_in_km FROM logbook WHERE id IN (SELECT MIN(id) FROM logbook GROUP BY destination) AND destination IS NOT NULL AND distance_in_km IS NOT NULL;")
|
||||
.fetch_all(db)
|
||||
|
Reference in New Issue
Block a user