add stats

This commit is contained in:
2023-07-24 20:56:46 +02:00
parent e90e27fc3d
commit 082fac9789
11 changed files with 142 additions and 9 deletions

View File

@ -30,6 +30,7 @@ pub struct LogbookWithBoatAndRowers {
pub enum LogbookUpdateError {
NotYourEntry,
TooManyRowers(usize, usize),
}
pub enum LogbookCreateError {
@ -170,6 +171,13 @@ impl Logbook {
Ok(())
}
async fn remove_rowers(&self, db: &SqlitePool) {
sqlx::query!("DELETE FROM rower WHERE logbook_id=?", self.id)
.execute(db)
.await
.unwrap();
}
pub async fn home(
&self,
db: &SqlitePool,
@ -178,10 +186,21 @@ impl Logbook {
distance_in_km: i64,
comments: Option<String>,
logtype: Option<i64>,
rower: Vec<i64>,
) -> Result<(), LogbookUpdateError> {
if user.id != self.shipmaster {
return Err(LogbookUpdateError::NotYourEntry);
}
let boat = Boat::find_by_id(db, self.boat_id as i32).await.unwrap(); //ok
if rower.len() > boat.amount_seats as usize - 1 {
return Err(LogbookUpdateError::TooManyRowers(
boat.amount_seats as usize,
rower.len() + 1,
));
}
//TODO: check current date
let arrival = format!("{}", chrono::offset::Local::now().format("%Y-%m-%d %H:%M"));
@ -197,6 +216,13 @@ impl Logbook {
)
.execute(db)
.await.unwrap(); //TODO: fixme
self.remove_rowers(db).await;
for rower in &rower {
Rower::create(db, self.id, *rower).await;
}
Ok(())
}