Allow to add real guests

This commit is contained in:
2023-08-09 20:30:37 +00:00
parent 723ada09b0
commit 590b41aa33
8 changed files with 308 additions and 91 deletions

View File

@ -1,3 +1,4 @@
use crate::model::user::User;
use chrono::NaiveDate;
use rocket::FromForm;
use serde::{Deserialize, Serialize};
@ -89,6 +90,91 @@ ORDER BY day;",
.map(|a| NaiveDate::parse_from_str(&a, "%Y-%m-%d").unwrap())
.collect()
}
pub(crate) async fn user_is_rower(&self, db: &SqlitePool, user: &User) -> bool {
//check if cox if planned_event
let is_rower = sqlx::query!(
"SELECT count(*) as amount
FROM user_trip
WHERE trip_details_id = ? AND user_id = ?",
self.id,
user.id
)
.fetch_one(db)
.await
.unwrap();
is_rower.amount > 0
}
async fn belongs_to_event(&self, db: &SqlitePool) -> bool {
let amount = sqlx::query!(
"SELECT count(*) as amount
FROM planned_event
WHERE trip_details_id = ?",
self.id,
)
.fetch_one(db)
.await
.unwrap();
amount.amount > 0
}
pub(crate) async fn user_allowed_to_change(&self, db: &SqlitePool, user: &User) -> bool {
if self.belongs_to_event(db).await {
return user.is_admin;
} else {
return self.user_is_cox(db, user).await != CoxAtTrip::No;
}
}
pub(crate) async fn user_is_cox(&self, db: &SqlitePool, user: &User) -> CoxAtTrip {
//check if cox if planned_event
let is_cox = sqlx::query!(
"SELECT count(*) as amount
FROM trip
WHERE planned_event_id = (
SELECT id FROM planned_event WHERE trip_details_id = ?
)
AND cox_id = ?",
self.id,
user.id
)
.fetch_one(db)
.await
.unwrap();
if is_cox.amount > 0 {
return CoxAtTrip::Yes(Action::Helping);
}
//check if cox if own event
let is_cox = sqlx::query!(
"SELECT count(*) as amount
FROM trip
WHERE trip_details_id = ?
AND cox_id = ?",
self.id,
user.id
)
.fetch_one(db)
.await
.unwrap();
if is_cox.amount > 0 {
return CoxAtTrip::Yes(Action::Own);
}
CoxAtTrip::No
}
}
#[derive(PartialEq, Debug)]
pub(crate) enum CoxAtTrip {
No,
Yes(Action),
}
#[derive(PartialEq, Debug)]
pub(crate) enum Action {
Helping,
Own,
}
#[cfg(test)]