move always_show to tripdetails

This commit is contained in:
2023-07-23 19:45:48 +02:00
parent 61961b24e2
commit c32c8bb643
12 changed files with 84 additions and 52 deletions

View File

@ -21,6 +21,7 @@ pub struct Trip {
pub notes: Option<String>,
pub allow_guests: bool,
trip_type_id: Option<i64>,
always_show: bool,
}
#[derive(Serialize)]
@ -47,7 +48,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
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
FROM trip
INNER JOIN trip_details ON trip.trip_details_id = trip_details.id
INNER JOIN user ON trip.cox_id = user.id
@ -88,7 +89,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
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
FROM trip
INNER JOIN trip_details ON trip.trip_details_id = trip_details.id
INNER JOIN user ON trip.cox_id = user.id
@ -139,6 +140,7 @@ FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM trip WHERE i
max_people: i32,
notes: Option<&str>,
trip_type: Option<i64>, //TODO: Move to `TripType`
always_show: bool,
) -> Result<(), TripUpdateError> {
if !trip.is_trip_from_user(cox.id) {
return Err(TripUpdateError::NotYourTrip);
@ -156,10 +158,11 @@ 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 = ? WHERE id = ?",
"UPDATE trip_details SET max_people = ?, notes = ?, trip_type_id = ?, always_show = ? WHERE id = ?",
max_people,
notes,
trip_type,
always_show,
trip_details_id
)
.execute(db)
@ -213,6 +216,15 @@ FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM trip WHERE i
fn is_trip_from_user(&self, user_id: i64) -> bool {
self.cox_id == user_id
}
pub(crate) async fn get_pinned_for_day(
db: &sqlx::Pool<sqlx::Sqlite>,
day: NaiveDate,
) -> Vec<TripWithUserAndType> {
let mut trips = Self::get_for_day(db, day).await;
trips.retain(|e| e.trip.always_show);
trips
}
}
#[derive(Debug)]
@ -319,7 +331,7 @@ mod test {
let trip = Trip::find_by_id(&pool, 1).await.unwrap();
assert!(Trip::update_own(&pool, &cox, &trip, 10, None, None)
assert!(Trip::update_own(&pool, &cox, &trip, 10, None, None, false)
.await
.is_ok());
@ -339,9 +351,11 @@ mod test {
let trip = Trip::find_by_id(&pool, 1).await.unwrap();
assert!(Trip::update_own(&pool, &cox, &trip, 10, None, Some(1))
.await
.is_ok());
assert!(
Trip::update_own(&pool, &cox, &trip, 10, None, Some(1), false)
.await
.is_ok()
);
let trip = Trip::find_by_id(&pool, 1).await.unwrap();
assert_eq!(trip.max_people, 10);
@ -360,7 +374,7 @@ mod test {
let trip = Trip::find_by_id(&pool, 1).await.unwrap();
assert!(Trip::update_own(&pool, &cox, &trip, 10, None, None)
assert!(Trip::update_own(&pool, &cox, &trip, 10, None, None, false)
.await
.is_err());
assert_eq!(trip.max_people, 1);