2023-04-04 15:16:21 +02:00
|
|
|
use sqlx::SqlitePool;
|
|
|
|
|
2023-04-28 19:08:17 +02:00
|
|
|
use super::{tripdetails::TripDetails, user::User};
|
2023-04-05 22:01:50 +02:00
|
|
|
|
2023-04-04 15:16:21 +02:00
|
|
|
pub struct UserTrip {}
|
|
|
|
|
|
|
|
impl UserTrip {
|
2023-04-05 22:01:50 +02:00
|
|
|
pub async fn create(
|
|
|
|
db: &SqlitePool,
|
2023-04-28 19:08:17 +02:00
|
|
|
user: &User,
|
|
|
|
trip_details: &TripDetails,
|
2023-04-05 22:01:50 +02:00
|
|
|
) -> Result<(), UserTripError> {
|
2023-04-26 12:21:30 +02:00
|
|
|
if trip_details.is_full(db).await {
|
2023-04-05 22:01:50 +02:00
|
|
|
return Err(UserTripError::EventAlreadyFull);
|
|
|
|
}
|
|
|
|
|
2023-04-29 18:57:01 +02:00
|
|
|
if user.is_guest && !trip_details.allow_guests {
|
|
|
|
return Err(UserTripError::GuestNotAllowedForThisEvent);
|
|
|
|
}
|
|
|
|
|
2023-04-06 18:48:18 +02:00
|
|
|
//check if cox if own event
|
2023-04-05 22:01:50 +02:00
|
|
|
let is_cox = sqlx::query!(
|
|
|
|
"SELECT count(*) as amount
|
|
|
|
FROM trip
|
|
|
|
WHERE trip_details_id = ?
|
|
|
|
AND cox_id = ?",
|
2023-04-28 19:08:17 +02:00
|
|
|
trip_details.id,
|
|
|
|
user.id
|
2023-04-05 22:01:50 +02:00
|
|
|
)
|
|
|
|
.fetch_one(db)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
if is_cox.amount > 0 {
|
2023-04-28 19:08:17 +02:00
|
|
|
return Err(UserTripError::CantRegisterAtOwnEvent);
|
2023-04-05 22:01:50 +02:00
|
|
|
}
|
|
|
|
|
2023-05-03 13:32:23 +02:00
|
|
|
//TODO: can probably move to trip.rs?
|
2023-04-06 18:48:18 +02:00
|
|
|
//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 = ?",
|
2023-04-28 19:08:17 +02:00
|
|
|
trip_details.id,
|
|
|
|
user.id
|
2023-04-06 18:48:18 +02:00
|
|
|
)
|
|
|
|
.fetch_one(db)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
if is_cox.amount > 0 {
|
|
|
|
return Err(UserTripError::AlreadyRegisteredAsCox);
|
|
|
|
}
|
|
|
|
|
2023-04-05 22:01:50 +02:00
|
|
|
match sqlx::query!(
|
2023-04-04 15:16:21 +02:00
|
|
|
"INSERT INTO user_trip (user_id, trip_details_id) VALUES(?, ?)",
|
2023-04-28 19:08:17 +02:00
|
|
|
user.id,
|
|
|
|
trip_details.id
|
2023-04-04 15:16:21 +02:00
|
|
|
)
|
|
|
|
.execute(db)
|
|
|
|
.await
|
2023-04-05 22:01:50 +02:00
|
|
|
{
|
|
|
|
Ok(_) => Ok(()),
|
|
|
|
Err(_) => Err(UserTripError::AlreadyRegistered),
|
|
|
|
}
|
2023-04-04 15:16:21 +02:00
|
|
|
}
|
|
|
|
|
2023-04-28 19:08:17 +02:00
|
|
|
pub async fn delete(db: &SqlitePool, user: &User, trip_details: &TripDetails) {
|
2023-04-05 22:04:05 +02:00
|
|
|
//TODO: Check if > 2 hrs to event
|
2023-04-04 15:16:21 +02:00
|
|
|
let _ = sqlx::query!(
|
|
|
|
"DELETE FROM user_trip WHERE user_id = ? AND trip_details_id = ?",
|
2023-04-28 19:08:17 +02:00
|
|
|
user.id,
|
|
|
|
trip_details.id
|
2023-04-04 15:16:21 +02:00
|
|
|
)
|
|
|
|
.execute(db)
|
|
|
|
.await
|
|
|
|
.is_ok();
|
|
|
|
}
|
|
|
|
}
|
2023-04-05 22:01:50 +02:00
|
|
|
|
2023-04-28 19:08:17 +02:00
|
|
|
#[derive(Debug, PartialEq)]
|
2023-04-05 22:01:50 +02:00
|
|
|
pub enum UserTripError {
|
|
|
|
AlreadyRegistered,
|
|
|
|
AlreadyRegisteredAsCox,
|
|
|
|
EventAlreadyFull,
|
2023-04-28 19:08:17 +02:00
|
|
|
CantRegisterAtOwnEvent,
|
2023-04-29 18:57:01 +02:00
|
|
|
GuestNotAllowedForThisEvent,
|
2023-04-05 22:01:50 +02:00
|
|
|
}
|
2023-04-26 16:54:53 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2023-04-28 19:08:17 +02:00
|
|
|
use crate::{
|
|
|
|
model::{
|
|
|
|
planned_event::PlannedEvent, trip::Trip, tripdetails::TripDetails, user::CoxUser,
|
|
|
|
usertrip::UserTripError,
|
|
|
|
},
|
|
|
|
testdb,
|
|
|
|
};
|
|
|
|
|
|
|
|
use super::{User, UserTrip};
|
|
|
|
use sqlx::SqlitePool;
|
|
|
|
|
|
|
|
#[sqlx::test]
|
|
|
|
fn test_succ_create() {
|
|
|
|
let pool = testdb!();
|
|
|
|
|
|
|
|
let user = User::find_by_name(&pool, "rower".into()).await.unwrap();
|
|
|
|
|
|
|
|
let trip_details = TripDetails::find_by_id(&pool, 1).await.unwrap();
|
|
|
|
|
|
|
|
UserTrip::create(&pool, &user, &trip_details).await.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[sqlx::test]
|
|
|
|
fn test_fail_create_full() {
|
|
|
|
let pool = testdb!();
|
|
|
|
|
|
|
|
let user = User::find_by_name(&pool, "rower".into()).await.unwrap();
|
|
|
|
let user2 = User::find_by_name(&pool, "cox".into()).await.unwrap();
|
|
|
|
let user3 = User::find_by_name(&pool, "rower2".into()).await.unwrap();
|
|
|
|
|
|
|
|
let trip_details = TripDetails::find_by_id(&pool, 1).await.unwrap();
|
|
|
|
|
|
|
|
UserTrip::create(&pool, &user, &trip_details).await.unwrap();
|
|
|
|
UserTrip::create(&pool, &user2, &trip_details)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let result = UserTrip::create(&pool, &user3, &trip_details)
|
|
|
|
.await
|
|
|
|
.expect_err("Expect registration to fail because trip is already full");
|
|
|
|
|
|
|
|
assert_eq!(result, UserTripError::EventAlreadyFull);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[sqlx::test]
|
|
|
|
fn test_fail_create_already_registered() {
|
|
|
|
let pool = testdb!();
|
|
|
|
|
|
|
|
let user = User::find_by_name(&pool, "cox".into()).await.unwrap();
|
|
|
|
let trip_details = TripDetails::find_by_id(&pool, 1).await.unwrap();
|
2023-04-26 16:54:53 +02:00
|
|
|
|
2023-04-28 19:08:17 +02:00
|
|
|
UserTrip::create(&pool, &user, &trip_details).await.unwrap();
|
2023-04-26 16:54:53 +02:00
|
|
|
|
2023-04-28 19:08:17 +02:00
|
|
|
let result = UserTrip::create(&pool, &user, &trip_details)
|
|
|
|
.await
|
|
|
|
.expect_err("Expect registration to fail because user is same as responsible cox");
|
|
|
|
|
|
|
|
assert_eq!(result, UserTripError::AlreadyRegistered);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[sqlx::test]
|
|
|
|
fn test_fail_create_is_cox_own_trip() {
|
|
|
|
let pool = testdb!();
|
|
|
|
|
|
|
|
let user = User::find_by_name(&pool, "cox".into()).await.unwrap();
|
|
|
|
|
|
|
|
let trip_details = TripDetails::find_by_id(&pool, 2).await.unwrap();
|
|
|
|
|
|
|
|
let result = UserTrip::create(&pool, &user, &trip_details)
|
|
|
|
.await
|
|
|
|
.expect_err("Expect registration to fail because user is same as responsible cox");
|
|
|
|
|
|
|
|
assert_eq!(result, UserTripError::CantRegisterAtOwnEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[sqlx::test]
|
|
|
|
fn test_fail_create_is_cox_planned_event() {
|
|
|
|
let pool = testdb!();
|
|
|
|
|
|
|
|
let cox: CoxUser = User::find_by_name(&pool, "cox".into())
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.try_into()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let trip_details = TripDetails::find_by_id(&pool, 1).await.unwrap();
|
|
|
|
let planned_event = PlannedEvent::find_by_id(&pool, 1).await.unwrap();
|
|
|
|
|
|
|
|
Trip::new_join(&pool, &cox, &planned_event).await.unwrap();
|
|
|
|
|
|
|
|
let result = UserTrip::create(&pool, &cox, &trip_details)
|
|
|
|
.await
|
|
|
|
.expect_err("Expect registration to fail because user is already registered as cox");
|
|
|
|
|
|
|
|
assert_eq!(result, UserTripError::AlreadyRegisteredAsCox);
|
|
|
|
}
|
2023-04-29 18:57:01 +02:00
|
|
|
|
|
|
|
#[sqlx::test]
|
|
|
|
fn test_fail_create_guest() {
|
|
|
|
let pool = testdb!();
|
|
|
|
|
|
|
|
let user = User::find_by_name(&pool, "guest".into()).await.unwrap();
|
|
|
|
|
|
|
|
let trip_details = TripDetails::find_by_id(&pool, 1).await.unwrap();
|
|
|
|
|
|
|
|
let result = UserTrip::create(&pool, &user, &trip_details)
|
|
|
|
.await
|
|
|
|
.expect_err("Not allowed for guests");
|
|
|
|
|
|
|
|
assert_eq!(result, UserTripError::GuestNotAllowedForThisEvent);
|
|
|
|
}
|
2023-04-26 16:54:53 +02:00
|
|
|
}
|