This commit is contained in:
2023-05-03 13:32:23 +02:00
parent 7981751f18
commit 913d0ac78e
7 changed files with 40 additions and 33 deletions

View File

@ -2,7 +2,7 @@ use chrono::NaiveDate;
use serde::Serialize;
use sqlx::{FromRow, SqlitePool};
use super::{tripdetails::TripDetails, triptype::TripType};
use super::{tripdetails::TripDetails, triptype::TripType, user::User};
#[derive(Serialize, Clone, FromRow)]
pub struct PlannedEvent {
@ -90,6 +90,7 @@ WHERE day=?",
}
async fn get_all_cox(&self, db: &SqlitePool) -> Vec<Registration> {
//TODO: switch to join
sqlx::query_as!(
Registration,
"
@ -107,6 +108,7 @@ FROM trip WHERE planned_event_id = ?
}
async fn get_all_rower(&self, db: &SqlitePool) -> Vec<Registration> {
//TODO: switch to join
sqlx::query_as!(
Registration,
"
@ -123,6 +125,23 @@ FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM planned_even
.unwrap() //Okay, as PlannedEvent can only be created with proper DB backing
}
//TODO: add tests
pub async fn is_rower_registered(&self, db: &SqlitePool, user: &User) -> bool {
let is_rower = sqlx::query!(
"SELECT count(*) as amount
FROM user_trip
WHERE trip_details_id =
(SELECT trip_details_id FROM planned_event WHERE id = ?)
AND user_id = ?",
self.id,
user.id
)
.fetch_one(db)
.await
.unwrap(); //Okay, bc planned_event can only be created with proper DB backing
is_rower.amount > 0
}
pub async fn create(
db: &SqlitePool,
name: String,

View File

@ -67,19 +67,7 @@ WHERE trip.id=?
cox: &CoxUser,
planned_event: &PlannedEvent,
) -> Result<(), CoxHelpError> {
let is_rower = sqlx::query!(
"SELECT count(*) as amount
FROM user_trip
WHERE trip_details_id =
(SELECT trip_details_id FROM planned_event WHERE id = ?)
AND user_id = ?",
planned_event.id,
cox.id
)
.fetch_one(db)
.await
.unwrap(); //Okay, bc planned_event can only be created with proper DB backing
if is_rower.amount > 0 {
if planned_event.is_rower_registered(db, &cox).await {
return Err(CoxHelpError::AlreadyRegisteredAsRower);
}
@ -112,6 +100,7 @@ WHERE day=?
.fetch_all(db)
.await
.unwrap(); //TODO: fixme
let mut ret = Vec::new();
for trip in trips {
let mut trip_type = None;

View File

@ -63,14 +63,7 @@ WHERE id like ?
.unwrap(); //TODO: fixme
let amount_currently_registered = i64::from(amount_currently_registered.count);
let amount_allowed_to_register =
sqlx::query!("SELECT max_people FROM trip_details WHERE id = ?", self.id)
.fetch_one(db)
.await
.unwrap(); //Okay, TripDetails can only be created if self.id exists
let amount_allowed_to_register = amount_allowed_to_register.max_people;
amount_currently_registered >= amount_allowed_to_register
amount_currently_registered >= self.max_people
}
}

View File

@ -34,6 +34,7 @@ impl UserTrip {
return Err(UserTripError::CantRegisterAtOwnEvent);
}
//TODO: can probably move to trip.rs?
//check if cox if planned_event
let is_cox = sqlx::query!(
"SELECT count(*) as amount