2023-04-04 15:16:21 +02:00
|
|
|
use sqlx::SqlitePool;
|
|
|
|
|
2023-04-26 12:21:30 +02:00
|
|
|
use super::tripdetails::TripDetails;
|
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,
|
|
|
|
user_id: i64,
|
|
|
|
trip_details_id: i64,
|
|
|
|
) -> Result<(), UserTripError> {
|
2023-04-26 12:52:19 +02:00
|
|
|
let trip_details = TripDetails::find_by_id(db, trip_details_id)
|
2023-04-26 12:21:30 +02:00
|
|
|
.await
|
|
|
|
.ok_or(UserTripError::TripDetailsNotFound)?;
|
|
|
|
|
|
|
|
if trip_details.is_full(db).await {
|
2023-04-05 22:01:50 +02:00
|
|
|
return Err(UserTripError::EventAlreadyFull);
|
|
|
|
}
|
|
|
|
|
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 = ?",
|
|
|
|
trip_details_id,
|
|
|
|
user_id
|
|
|
|
)
|
|
|
|
.fetch_one(db)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
if is_cox.amount > 0 {
|
|
|
|
return Err(UserTripError::AlreadyRegisteredAsCox);
|
|
|
|
}
|
|
|
|
|
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 = ?",
|
|
|
|
trip_details_id,
|
|
|
|
user_id
|
|
|
|
)
|
|
|
|
.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(?, ?)",
|
|
|
|
user_id,
|
|
|
|
trip_details_id
|
|
|
|
)
|
|
|
|
.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
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn delete(db: &SqlitePool, user_id: i64, trip_details_id: i64) {
|
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 = ?",
|
|
|
|
user_id,
|
|
|
|
trip_details_id
|
|
|
|
)
|
|
|
|
.execute(db)
|
|
|
|
.await
|
|
|
|
.is_ok();
|
|
|
|
}
|
|
|
|
}
|
2023-04-05 22:01:50 +02:00
|
|
|
|
|
|
|
pub enum UserTripError {
|
|
|
|
AlreadyRegistered,
|
|
|
|
AlreadyRegisteredAsCox,
|
|
|
|
EventAlreadyFull,
|
2023-04-26 12:21:30 +02:00
|
|
|
TripDetailsNotFound,
|
2023-04-05 22:01:50 +02:00
|
|
|
}
|
2023-04-26 16:54:53 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2023-04-28 18:08:01 +02:00
|
|
|
//use crate::testdb;
|
2023-04-26 16:54:53 +02:00
|
|
|
|
|
|
|
//use super::User;
|
|
|
|
//use sqlx::SqlitePool;
|
|
|
|
|
|
|
|
//#[sqlx::test]
|
|
|
|
//fn test_find_correct_id() {
|
|
|
|
// let pool = testdb!();
|
|
|
|
// let user = User::find_by_id(&pool, 1).await.unwrap();
|
|
|
|
// assert_eq!(user.id, 1);
|
|
|
|
//}
|
|
|
|
}
|