forked from Ruderverein-Donau-Linz/rowt
don't allow to join as user if already cox
This commit is contained in:
parent
9ab1572b15
commit
8f05d1bdf4
@ -56,7 +56,7 @@ WHERE day=?",
|
|||||||
ret
|
ret
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn rower_can_register(db: &SqlitePool, trip_details_id: i64) -> bool {
|
pub async fn is_full(db: &SqlitePool, trip_details_id: i64) -> bool {
|
||||||
let amount_currently_registered = sqlx::query!(
|
let amount_currently_registered = sqlx::query!(
|
||||||
"SELECT COUNT(*) as count FROM user_trip WHERE trip_details_id = ?",
|
"SELECT COUNT(*) as count FROM user_trip WHERE trip_details_id = ?",
|
||||||
trip_details_id
|
trip_details_id
|
||||||
@ -75,7 +75,7 @@ WHERE day=?",
|
|||||||
.unwrap(); //TODO: fixme
|
.unwrap(); //TODO: fixme
|
||||||
let amount_allowed_to_register = amount_allowed_to_register.max_people;
|
let amount_allowed_to_register = amount_allowed_to_register.max_people;
|
||||||
|
|
||||||
amount_currently_registered < amount_allowed_to_register
|
amount_currently_registered >= amount_allowed_to_register
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_all_cox_for_id(db: &SqlitePool, id: i64) -> Vec<Registration> {
|
async fn get_all_cox_for_id(db: &SqlitePool, id: i64) -> Vec<Registration> {
|
||||||
|
@ -1,17 +1,45 @@
|
|||||||
use sqlx::SqlitePool;
|
use sqlx::SqlitePool;
|
||||||
|
|
||||||
|
use super::planned_event::PlannedEvent;
|
||||||
|
|
||||||
pub struct UserTrip {}
|
pub struct UserTrip {}
|
||||||
|
|
||||||
impl UserTrip {
|
impl UserTrip {
|
||||||
pub async fn create(db: &SqlitePool, user_id: i64, trip_details_id: i64) -> bool {
|
pub async fn create(
|
||||||
sqlx::query!(
|
db: &SqlitePool,
|
||||||
|
user_id: i64,
|
||||||
|
trip_details_id: i64,
|
||||||
|
) -> Result<(), UserTripError> {
|
||||||
|
if PlannedEvent::is_full(db, trip_details_id).await {
|
||||||
|
return Err(UserTripError::EventAlreadyFull);
|
||||||
|
}
|
||||||
|
|
||||||
|
let is_cox = sqlx::query!(
|
||||||
|
"SELECT count(*) as amount
|
||||||
|
FROM trip
|
||||||
|
WHERE trip_details_id = ?
|
||||||
|
AND cox_id = ?",
|
||||||
|
trip_details_id,
|
||||||
|
user_id
|
||||||
|
)
|
||||||
|
.fetch_one(db)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
if is_cox.amount > 0 {
|
||||||
|
return Err(UserTripError::AlreadyRegisteredAsCox);
|
||||||
|
}
|
||||||
|
|
||||||
|
match sqlx::query!(
|
||||||
"INSERT INTO user_trip (user_id, trip_details_id) VALUES(?, ?)",
|
"INSERT INTO user_trip (user_id, trip_details_id) VALUES(?, ?)",
|
||||||
user_id,
|
user_id,
|
||||||
trip_details_id
|
trip_details_id
|
||||||
)
|
)
|
||||||
.execute(db)
|
.execute(db)
|
||||||
.await
|
.await
|
||||||
.is_ok()
|
{
|
||||||
|
Ok(_) => Ok(()),
|
||||||
|
Err(_) => Err(UserTripError::AlreadyRegistered),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn delete(db: &SqlitePool, user_id: i64, trip_details_id: i64) {
|
pub async fn delete(db: &SqlitePool, user_id: i64, trip_details_id: i64) {
|
||||||
@ -25,3 +53,9 @@ impl UserTrip {
|
|||||||
.is_ok();
|
.is_ok();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub enum UserTripError {
|
||||||
|
AlreadyRegistered,
|
||||||
|
AlreadyRegisteredAsCox,
|
||||||
|
EventAlreadyFull,
|
||||||
|
}
|
||||||
|
@ -10,7 +10,11 @@ use rocket::{
|
|||||||
use rocket_dyn_templates::{tera::Context, Template};
|
use rocket_dyn_templates::{tera::Context, Template};
|
||||||
use sqlx::SqlitePool;
|
use sqlx::SqlitePool;
|
||||||
|
|
||||||
use crate::model::{planned_event::PlannedEvent, user::User, usertrip::UserTrip, Day};
|
use crate::model::{
|
||||||
|
user::User,
|
||||||
|
usertrip::{UserTrip, UserTripError},
|
||||||
|
Day,
|
||||||
|
};
|
||||||
|
|
||||||
mod admin;
|
mod admin;
|
||||||
mod auth;
|
mod auth;
|
||||||
@ -45,14 +49,17 @@ async fn index(db: &State<SqlitePool>, user: User, flash: Option<FlashMessage<'_
|
|||||||
|
|
||||||
#[get("/join/<trip_details_id>")]
|
#[get("/join/<trip_details_id>")]
|
||||||
async fn join(db: &State<SqlitePool>, trip_details_id: i64, user: User) -> Flash<Redirect> {
|
async fn join(db: &State<SqlitePool>, trip_details_id: i64, user: User) -> Flash<Redirect> {
|
||||||
if !PlannedEvent::rower_can_register(db, trip_details_id).await {
|
match UserTrip::create(db, user.id, trip_details_id).await {
|
||||||
return Flash::error(Redirect::to("/"), "Bereits ausgebucht!");
|
Ok(_) => Flash::success(Redirect::to("/"), "Erfolgreich angemeldet!"),
|
||||||
}
|
Err(UserTripError::EventAlreadyFull) => {
|
||||||
|
Flash::error(Redirect::to("/"), "Event bereits ausgebucht!")
|
||||||
if UserTrip::create(db, user.id, trip_details_id).await {
|
}
|
||||||
Flash::success(Redirect::to("/"), "Erfolgreich angemeldet!")
|
Err(UserTripError::AlreadyRegistered) => {
|
||||||
} else {
|
Flash::error(Redirect::to("/"), "Du nimmst bereits teil!")
|
||||||
Flash::error(Redirect::to("/"), "Du nimmst bereits teil!")
|
}
|
||||||
|
Err(UserTripError::AlreadyRegisteredAsCox) => {
|
||||||
|
Flash::error(Redirect::to("/"), "Du hilfst bereits als Steuerperson aus!")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user