allow cox to edit own trips

This commit is contained in:
2023-04-07 11:54:56 +02:00
parent eeb7af4fc1
commit 1d03dd59b2
5 changed files with 105 additions and 24 deletions

View File

@ -112,6 +112,45 @@ FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM trip WHERE i
}
}
/// Cox decides to update own trip.
pub async fn update_own(
db: &SqlitePool,
cox_id: i64,
trip_id: i64,
max_people: i32,
notes: Option<String>,
) -> Result<(), TripUpdateError> {
if !Self::is_trip_from_user(db, cox_id, trip_id).await {
return Err(TripUpdateError::NotYourTrip);
}
let trip_details = sqlx::query!(
"SELECT trip_details_id as id FROM trip WHERE id = ?",
trip_id
)
.fetch_one(db)
.await
.unwrap(); //TODO: fixme
let trip_details_id = match trip_details.id {
Some(id) => id,
None => {
return Err(TripUpdateError::TripDoesNotExist);
}
};
sqlx::query!(
"UPDATE trip_details SET max_people = ?, notes = ? WHERE id = ?",
max_people,
notes,
trip_details_id
)
.execute(db)
.await
.unwrap(); //TODO: fixme
Ok(())
}
pub async fn delete_by_planned_event_id(db: &SqlitePool, user_id: i64, planned_event_id: i64) {
let _ = sqlx::query!(
"DELETE FROM trip WHERE cox_id = ? AND planned_event_id = ?",
@ -133,11 +172,7 @@ FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM trip WHERE i
return Err(TripDeleteError::SomebodyAlreadyRegistered);
}
let trip_cox = sqlx::query!("SELECT cox_id FROM trip WHERE id = ?", trip_id)
.fetch_one(db)
.await
.unwrap(); //TODO: fixme
if trip_cox.cox_id != user_id {
if !Self::is_trip_from_user(db, user_id, trip_id).await {
return Err(TripDeleteError::NotYourTrip);
}
@ -152,6 +187,14 @@ FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM trip WHERE i
Ok(())
}
async fn is_trip_from_user(db: &SqlitePool, user_id: i64, trip_id: i64) -> bool {
let trip_cox = sqlx::query!("SELECT cox_id FROM trip WHERE id = ?", trip_id)
.fetch_one(db)
.await
.unwrap(); //TODO: fixme
trip_cox.cox_id == user_id
}
}
pub enum CoxHelpError {
@ -163,3 +206,8 @@ pub enum TripDeleteError {
SomebodyAlreadyRegistered,
NotYourTrip,
}
pub enum TripUpdateError {
NotYourTrip,
TripDoesNotExist,
}