forked from Ruderverein-Donau-Linz/rowt
don't allow cox to help if cox is already registered as rower for event
This commit is contained in:
parent
e2f5436790
commit
9ab1572b15
@ -65,6 +65,7 @@ FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM trip WHERE i
|
||||
.unwrap() //TODO: fixme
|
||||
}
|
||||
|
||||
/// Cox decides to create own trip.
|
||||
pub async fn new_own(db: &SqlitePool, cox_id: i64, trip_details_id: i64) {
|
||||
sqlx::query!(
|
||||
"INSERT INTO trip (cox_id, trip_details_id) VALUES(?, ?)",
|
||||
@ -76,17 +77,39 @@ FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM trip WHERE i
|
||||
.unwrap(); //TODO: fixme
|
||||
}
|
||||
|
||||
/// Returns true if successfully inserted; false if not (e.g. because user is already
|
||||
/// participant
|
||||
pub async fn new_join(db: &SqlitePool, cox_id: i64, planned_event_id: i64) -> bool {
|
||||
sqlx::query!(
|
||||
/// Cox decides to help in a planned event.
|
||||
pub async fn new_join(
|
||||
db: &SqlitePool,
|
||||
cox_id: i64,
|
||||
planned_event_id: i64,
|
||||
) -> 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();
|
||||
if is_rower.amount > 0 {
|
||||
return Err(CoxHelpError::AlreadyRegisteredAsRower);
|
||||
}
|
||||
|
||||
match sqlx::query!(
|
||||
"INSERT INTO trip (cox_id, planned_event_id) VALUES(?, ?)",
|
||||
cox_id,
|
||||
planned_event_id
|
||||
)
|
||||
.execute(db)
|
||||
.await
|
||||
.is_ok()
|
||||
{
|
||||
Ok(_) => Ok(()),
|
||||
Err(_) => Err(CoxHelpError::AlreadyRegisteredAsCox),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn delete(db: &SqlitePool, user_id: i64, planned_event_id: i64) {
|
||||
@ -99,11 +122,9 @@ FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM trip WHERE i
|
||||
.await
|
||||
.is_ok();
|
||||
}
|
||||
|
||||
//pub async fn delete(db: &SqlitePool, id: i64) {
|
||||
// sqlx::query!("DELETE FROM planned_event WHERE id = ?", id)
|
||||
// .execute(db)
|
||||
// .await
|
||||
// .unwrap(); //TODO: fixme
|
||||
//}
|
||||
}
|
||||
|
||||
pub enum CoxHelpError {
|
||||
AlreadyRegisteredAsRower,
|
||||
AlreadyRegisteredAsCox,
|
||||
}
|
||||
|
@ -6,7 +6,11 @@ use rocket::{
|
||||
};
|
||||
use sqlx::SqlitePool;
|
||||
|
||||
use crate::model::{trip::Trip, tripdetails::TripDetails, user::CoxUser};
|
||||
use crate::model::{
|
||||
trip::{CoxHelpError, Trip},
|
||||
tripdetails::TripDetails,
|
||||
user::CoxUser,
|
||||
};
|
||||
|
||||
//TODO: add constraints (e.g. planned_amount_cox > 0)
|
||||
#[derive(FromForm)]
|
||||
@ -37,10 +41,15 @@ async fn create(db: &State<SqlitePool>, data: Form<AddTripForm>, cox: CoxUser) -
|
||||
|
||||
#[get("/join/<planned_event_id>")]
|
||||
async fn join(db: &State<SqlitePool>, planned_event_id: i64, cox: CoxUser) -> Flash<Redirect> {
|
||||
if Trip::new_join(db, cox.id, planned_event_id).await {
|
||||
Flash::success(Redirect::to("/"), "Danke für's helfen!")
|
||||
} else {
|
||||
Flash::error(Redirect::to("/"), "Du nimmst bereits teil!")
|
||||
match Trip::new_join(db, cox.id, planned_event_id).await {
|
||||
Ok(_) => Flash::success(Redirect::to("/"), "Danke für's helfen!"),
|
||||
Err(CoxHelpError::AlreadyRegisteredAsCox) => {
|
||||
Flash::error(Redirect::to("/"), "Du hilfst bereits aus!")
|
||||
}
|
||||
Err(CoxHelpError::AlreadyRegisteredAsRower) => Flash::error(
|
||||
Redirect::to("/"),
|
||||
"Du hast dich bereits als Ruderer angemeldet!",
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user