implement is_locked for trip_details
This commit is contained in:
@ -23,6 +23,7 @@ pub struct PlannedEvent {
|
||||
pub allow_guests: bool,
|
||||
trip_type_id: Option<i64>,
|
||||
always_show: bool,
|
||||
is_locked: bool,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
@ -49,7 +50,7 @@ impl PlannedEvent {
|
||||
Self,
|
||||
"
|
||||
SELECT
|
||||
planned_event.id, planned_event.name, planned_amount_cox, trip_details_id, planned_starting_time, max_people, day, notes, allow_guests, trip_type_id, always_show
|
||||
planned_event.id, planned_event.name, planned_amount_cox, trip_details_id, planned_starting_time, max_people, day, notes, allow_guests, trip_type_id, always_show, is_locked
|
||||
FROM planned_event
|
||||
INNER JOIN trip_details ON planned_event.trip_details_id = trip_details.id
|
||||
WHERE planned_event.id like ?
|
||||
@ -77,7 +78,7 @@ WHERE planned_event.id like ?
|
||||
let day = format!("{day}");
|
||||
let events = sqlx::query_as!(
|
||||
PlannedEvent,
|
||||
"SELECT planned_event.id, planned_event.name, planned_amount_cox, trip_details_id, planned_starting_time, always_show, max_people, day, notes, allow_guests, trip_type_id
|
||||
"SELECT planned_event.id, planned_event.name, planned_amount_cox, trip_details_id, planned_starting_time, always_show, max_people, day, notes, allow_guests, trip_type_id, is_locked
|
||||
FROM planned_event
|
||||
INNER JOIN trip_details ON planned_event.trip_details_id = trip_details.id
|
||||
WHERE day=?",
|
||||
@ -108,7 +109,7 @@ WHERE day=?",
|
||||
pub async fn all(db: &SqlitePool) -> Vec<PlannedEvent> {
|
||||
sqlx::query_as!(
|
||||
PlannedEvent,
|
||||
"SELECT planned_event.id, planned_event.name, planned_amount_cox, trip_details_id, planned_starting_time, always_show, max_people, day, notes, allow_guests, trip_type_id
|
||||
"SELECT planned_event.id, planned_event.name, planned_amount_cox, trip_details_id, planned_starting_time, always_show, max_people, day, notes, allow_guests, trip_type_id, is_locked
|
||||
FROM planned_event
|
||||
INNER JOIN trip_details ON planned_event.trip_details_id = trip_details.id",
|
||||
)
|
||||
@ -195,6 +196,7 @@ FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM planned_even
|
||||
max_people: i32,
|
||||
notes: Option<&str>,
|
||||
always_show: bool,
|
||||
is_locked: bool,
|
||||
) {
|
||||
sqlx::query!(
|
||||
"UPDATE planned_event SET planned_amount_cox = ? WHERE id = ?",
|
||||
@ -206,10 +208,11 @@ FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM planned_even
|
||||
.unwrap(); //Okay, as planned_event can only be created with proper DB backing
|
||||
|
||||
sqlx::query!(
|
||||
"UPDATE trip_details SET max_people = ?, notes = ?, always_show=? WHERE id = ?",
|
||||
"UPDATE trip_details SET max_people = ?, notes = ?, always_show = ?, is_locked = ? WHERE id = ?",
|
||||
max_people,
|
||||
notes,
|
||||
always_show,
|
||||
is_locked,
|
||||
self.trip_details_id
|
||||
)
|
||||
.execute(db)
|
||||
@ -242,6 +245,12 @@ FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM planned_even
|
||||
write!(&mut buf, "{}", calendar).unwrap();
|
||||
String::from_utf8(buf).unwrap()
|
||||
}
|
||||
|
||||
pub async fn trip_details(&self, db: &SqlitePool) -> TripDetails {
|
||||
TripDetails::find_by_id(db, self.trip_details_id)
|
||||
.await
|
||||
.unwrap() //ok, not null in db
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -22,6 +22,7 @@ pub struct Trip {
|
||||
pub allow_guests: bool,
|
||||
trip_type_id: Option<i64>,
|
||||
always_show: bool,
|
||||
is_locked: bool,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
@ -48,7 +49,7 @@ impl Trip {
|
||||
sqlx::query_as!(
|
||||
Self,
|
||||
"
|
||||
SELECT trip.id, cox_id, user.name as cox_name, trip_details_id, planned_starting_time, max_people, day, notes, allow_guests, trip_type_id, always_show
|
||||
SELECT trip.id, cox_id, user.name as cox_name, trip_details_id, planned_starting_time, max_people, day, notes, allow_guests, trip_type_id, always_show, is_locked
|
||||
FROM trip
|
||||
INNER JOIN trip_details ON trip.trip_details_id = trip_details.id
|
||||
INNER JOIN user ON trip.cox_id = user.id
|
||||
@ -71,6 +72,14 @@ WHERE trip.id=?
|
||||
return Err(CoxHelpError::AlreadyRegisteredAsRower);
|
||||
}
|
||||
|
||||
if planned_event.trip_details(db).await.is_locked {
|
||||
return Err(CoxHelpError::DetailsLocked);
|
||||
}
|
||||
|
||||
if planned_event.is_rower_registered(db, cox).await {
|
||||
return Err(CoxHelpError::AlreadyRegisteredAsRower);
|
||||
}
|
||||
|
||||
match sqlx::query!(
|
||||
"INSERT INTO trip (cox_id, planned_event_id) VALUES(?, ?)",
|
||||
cox.id,
|
||||
@ -89,7 +98,7 @@ WHERE trip.id=?
|
||||
let trips = sqlx::query_as!(
|
||||
Trip,
|
||||
"
|
||||
SELECT trip.id, cox_id, user.name as cox_name, trip_details_id, planned_starting_time, max_people, day, notes, allow_guests, trip_type_id, always_show
|
||||
SELECT trip.id, cox_id, user.name as cox_name, trip_details_id, planned_starting_time, max_people, day, notes, allow_guests, trip_type_id, always_show, is_locked
|
||||
FROM trip
|
||||
INNER JOIN trip_details ON trip.trip_details_id = trip_details.id
|
||||
INNER JOIN user ON trip.cox_id = user.id
|
||||
@ -141,6 +150,7 @@ FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM trip WHERE i
|
||||
notes: Option<&str>,
|
||||
trip_type: Option<i64>, //TODO: Move to `TripType`
|
||||
always_show: bool,
|
||||
is_locked: bool,
|
||||
) -> Result<(), TripUpdateError> {
|
||||
if !trip.is_trip_from_user(cox.id) {
|
||||
return Err(TripUpdateError::NotYourTrip);
|
||||
@ -158,11 +168,12 @@ FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM trip WHERE i
|
||||
};
|
||||
|
||||
sqlx::query!(
|
||||
"UPDATE trip_details SET max_people = ?, notes = ?, trip_type_id = ?, always_show = ? WHERE id = ?",
|
||||
"UPDATE trip_details SET max_people = ?, notes = ?, trip_type_id = ?, always_show = ?, is_locked = ? WHERE id = ?",
|
||||
max_people,
|
||||
notes,
|
||||
trip_type,
|
||||
always_show,
|
||||
is_locked,
|
||||
trip_details_id
|
||||
)
|
||||
.execute(db)
|
||||
@ -172,12 +183,23 @@ FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM trip WHERE i
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn trip_details(&self, db: &SqlitePool) -> Option<TripDetails> {
|
||||
if let Some(trip_details_id) = self.trip_type_id {
|
||||
return TripDetails::find_by_id(db, trip_details_id).await;
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub async fn delete_by_planned_event(
|
||||
db: &SqlitePool,
|
||||
cox: &CoxUser,
|
||||
planned_event: &PlannedEvent,
|
||||
) -> bool {
|
||||
sqlx::query!(
|
||||
) -> Result<(), TripHelpDeleteError> {
|
||||
if planned_event.trip_details(db).await.is_locked {
|
||||
return Err(TripHelpDeleteError::DetailsLocked);
|
||||
}
|
||||
|
||||
let affected_rows = sqlx::query!(
|
||||
"DELETE FROM trip WHERE cox_id = ? AND planned_event_id = ?",
|
||||
cox.id,
|
||||
planned_event.id
|
||||
@ -185,8 +207,13 @@ FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM trip WHERE i
|
||||
.execute(db)
|
||||
.await
|
||||
.unwrap()
|
||||
.rows_affected()
|
||||
> 0
|
||||
.rows_affected();
|
||||
|
||||
if affected_rows == 0 {
|
||||
return Err(TripHelpDeleteError::CoxNotHelping);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) async fn delete(
|
||||
@ -233,6 +260,13 @@ FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM trip WHERE i
|
||||
pub enum CoxHelpError {
|
||||
AlreadyRegisteredAsRower,
|
||||
AlreadyRegisteredAsCox,
|
||||
DetailsLocked,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum TripHelpDeleteError {
|
||||
DetailsLocked,
|
||||
CoxNotHelping,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
@ -333,9 +367,11 @@ mod test {
|
||||
|
||||
let trip = Trip::find_by_id(&pool, 1).await.unwrap();
|
||||
|
||||
assert!(Trip::update_own(&pool, &cox, &trip, 10, None, None, false)
|
||||
.await
|
||||
.is_ok());
|
||||
assert!(
|
||||
Trip::update_own(&pool, &cox, &trip, 10, None, None, false, false)
|
||||
.await
|
||||
.is_ok()
|
||||
);
|
||||
|
||||
let trip = Trip::find_by_id(&pool, 1).await.unwrap();
|
||||
assert_eq!(trip.max_people, 10);
|
||||
@ -354,7 +390,7 @@ mod test {
|
||||
let trip = Trip::find_by_id(&pool, 1).await.unwrap();
|
||||
|
||||
assert!(
|
||||
Trip::update_own(&pool, &cox, &trip, 10, None, Some(1), false)
|
||||
Trip::update_own(&pool, &cox, &trip, 10, None, Some(1), false, false)
|
||||
.await
|
||||
.is_ok()
|
||||
);
|
||||
@ -376,9 +412,11 @@ mod test {
|
||||
|
||||
let trip = Trip::find_by_id(&pool, 1).await.unwrap();
|
||||
|
||||
assert!(Trip::update_own(&pool, &cox, &trip, 10, None, None, false)
|
||||
.await
|
||||
.is_err());
|
||||
assert!(
|
||||
Trip::update_own(&pool, &cox, &trip, 10, None, None, false, false)
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
assert_eq!(trip.max_people, 1);
|
||||
}
|
||||
|
||||
@ -398,7 +436,9 @@ mod test {
|
||||
|
||||
//TODO: check why following assert fails
|
||||
//assert!(Trip::find_by_id(&pool, 2).await.is_some());
|
||||
Trip::delete_by_planned_event(&pool, &cox, &planned_event).await;
|
||||
Trip::delete_by_planned_event(&pool, &cox, &planned_event)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(Trip::find_by_id(&pool, 2).await.is_none());
|
||||
}
|
||||
|
||||
|
@ -13,6 +13,7 @@ pub struct TripDetails {
|
||||
pub allow_guests: bool,
|
||||
pub trip_type_id: Option<i64>,
|
||||
pub always_show: bool,
|
||||
pub is_locked: bool,
|
||||
}
|
||||
|
||||
#[derive(FromForm, Serialize)]
|
||||
@ -33,7 +34,7 @@ impl TripDetails {
|
||||
sqlx::query_as!(
|
||||
TripDetails,
|
||||
"
|
||||
SELECT id, planned_starting_time, max_people, day, notes, allow_guests, trip_type_id, always_show
|
||||
SELECT id, planned_starting_time, max_people, day, notes, allow_guests, trip_type_id, always_show, is_locked
|
||||
FROM trip_details
|
||||
WHERE id like ?
|
||||
",
|
||||
|
@ -14,6 +14,10 @@ impl UserTrip {
|
||||
return Err(UserTripError::EventAlreadyFull);
|
||||
}
|
||||
|
||||
if trip_details.is_locked {
|
||||
return Err(UserTripError::DetailsLocked);
|
||||
}
|
||||
|
||||
if user.is_guest && !trip_details.allow_guests {
|
||||
return Err(UserTripError::GuestNotAllowedForThisEvent);
|
||||
}
|
||||
@ -68,8 +72,15 @@ impl UserTrip {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn delete(db: &SqlitePool, user: &User, trip_details: &TripDetails) {
|
||||
//TODO: Check if > 2 hrs to event
|
||||
pub async fn delete(
|
||||
db: &SqlitePool,
|
||||
user: &User,
|
||||
trip_details: &TripDetails,
|
||||
) -> Result<(), UserTripDeleteError> {
|
||||
if trip_details.is_locked {
|
||||
return Err(UserTripDeleteError::DetailsLocked);
|
||||
}
|
||||
|
||||
let _ = sqlx::query!(
|
||||
"DELETE FROM user_trip WHERE user_id = ? AND trip_details_id = ?",
|
||||
user.id,
|
||||
@ -77,7 +88,9 @@ impl UserTrip {
|
||||
)
|
||||
.execute(db)
|
||||
.await
|
||||
.is_ok();
|
||||
.unwrap();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@ -86,10 +99,16 @@ pub enum UserTripError {
|
||||
AlreadyRegistered,
|
||||
AlreadyRegisteredAsCox,
|
||||
EventAlreadyFull,
|
||||
DetailsLocked,
|
||||
CantRegisterAtOwnEvent,
|
||||
GuestNotAllowedForThisEvent,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum UserTripDeleteError {
|
||||
DetailsLocked,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::{
|
||||
|
Reference in New Issue
Block a user