From 99296d40607f0dd51b8ca2e251d345033e4a814e Mon Sep 17 00:00:00 2001 From: philipp Date: Wed, 24 May 2023 12:11:55 +0200 Subject: [PATCH] fix many clones() --- src/model/planned_event.rs | 4 ++-- src/model/trip.rs | 2 +- src/model/tripdetails.rs | 6 ++--- src/model/user.rs | 8 +++---- src/rest/admin/planned_event.rs | 35 +++++++++++---------------- src/rest/admin/user.rs | 8 +++---- src/rest/auth.rs | 20 ++++++++-------- src/rest/cox.rs | 42 +++++++++++++-------------------- 8 files changed, 54 insertions(+), 71 deletions(-) diff --git a/src/model/planned_event.rs b/src/model/planned_event.rs index 171b2f5..5a024e8 100644 --- a/src/model/planned_event.rs +++ b/src/model/planned_event.rs @@ -144,7 +144,7 @@ FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM planned_even pub async fn create( db: &SqlitePool, - name: String, + name: &str, planned_amount_cox: i32, trip_details: TripDetails, ) { @@ -165,7 +165,7 @@ FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM planned_even db: &SqlitePool, planned_amount_cox: i32, max_people: i32, - notes: Option, + notes: Option<&str>, ) { sqlx::query!( "UPDATE planned_event SET planned_amount_cox = ? WHERE id = ?", diff --git a/src/model/trip.rs b/src/model/trip.rs index d378c88..3b7d163 100644 --- a/src/model/trip.rs +++ b/src/model/trip.rs @@ -138,7 +138,7 @@ FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM trip WHERE i cox: &CoxUser, trip: &Trip, max_people: i32, - notes: Option, + notes: Option<&str>, trip_type: Option, //TODO: Move to `TripType` ) -> Result<(), TripUpdateError> { if !trip.is_trip_from_user(cox.id).await { diff --git a/src/model/tripdetails.rs b/src/model/tripdetails.rs index 0026b2b..0585679 100644 --- a/src/model/tripdetails.rs +++ b/src/model/tripdetails.rs @@ -31,10 +31,10 @@ WHERE id like ? /// Creates a new entry in `trip_details` and returns its id. pub async fn create( db: &SqlitePool, - planned_starting_time: String, + planned_starting_time: &str, max_people: i32, - day: String, - notes: Option, + day: &str, + notes: Option<&str>, allow_guests: bool, trip_type_id: Option, ) -> i64 { diff --git a/src/model/user.rs b/src/model/user.rs index 97870b2..d5fdc2b 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -49,7 +49,7 @@ WHERE id like ? .ok() } - pub async fn find_by_name(db: &SqlitePool, name: String) -> Option { + pub async fn find_by_name(db: &SqlitePool, name: &str) -> Option { sqlx::query_as!( User, " @@ -79,7 +79,7 @@ ORDER BY name .unwrap() //TODO: fixme } - pub async fn create(db: &SqlitePool, name: String, is_guest: bool) -> bool { + pub async fn create(db: &SqlitePool, name: &str, is_guest: bool) -> bool { sqlx::query!( "INSERT INTO USER(name, is_guest) VALUES (?,?)", name, @@ -103,7 +103,7 @@ ORDER BY name .unwrap(); //Okay, because we can only create a User of a valid id } - pub async fn login(db: &SqlitePool, name: String, pw: String) -> Result { + pub async fn login(db: &SqlitePool, name: &str, pw: &str) -> Result { let Some(user) = User::find_by_name(db, name).await else { return Err(LoginError::InvalidAuthenticationCombo); // Username not found }; @@ -133,7 +133,7 @@ ORDER BY name .unwrap(); //Okay, because we can only create a User of a valid id } - pub async fn update_pw(&self, db: &SqlitePool, pw: String) { + pub async fn update_pw(&self, db: &SqlitePool, pw: &str) { let pw = Self::get_hashed_pw(&pw); sqlx::query!("UPDATE user SET pw = ? where id = ?", pw, self.id) .execute(db) diff --git a/src/rest/admin/planned_event.rs b/src/rest/admin/planned_event.rs index 12e1f17..67297a9 100644 --- a/src/rest/admin/planned_event.rs +++ b/src/rest/admin/planned_event.rs @@ -10,30 +10,29 @@ use crate::model::{planned_event::PlannedEvent, tripdetails::TripDetails, user:: //TODO: add constraints (e.g. planned_amount_cox > 0) #[derive(FromForm)] -struct AddPlannedEventForm { - day: String, - name: String, +struct AddPlannedEventForm<'r> { + day: &'r str, + name: &'r str, planned_amount_cox: i32, allow_guests: bool, - planned_starting_time: String, + planned_starting_time: &'r str, max_people: i32, - notes: Option, + notes: Option<&'r str>, trip_type: Option, } #[post("/planned-event", data = "")] async fn create( db: &State, - data: Form, + data: Form>, _admin: AdminUser, ) -> Flash { - //TODO: fix clones() let trip_details_id = TripDetails::create( db, - data.planned_starting_time.clone(), + data.planned_starting_time, data.max_people, - data.day.clone(), - data.notes.clone(), + data.day, + data.notes, data.allow_guests, data.trip_type, ) @@ -43,36 +42,30 @@ async fn create( //just created //the object - //TODO: fix clone() - PlannedEvent::create(db, data.name.clone(), data.planned_amount_cox, trip_details).await; + PlannedEvent::create(db, data.name, data.planned_amount_cox, trip_details).await; Flash::success(Redirect::to("/"), "Successfully planned the event") } //TODO: add constraints (e.g. planned_amount_cox > 0) #[derive(FromForm)] -struct UpdatePlannedEventForm { +struct UpdatePlannedEventForm<'r> { id: i64, planned_amount_cox: i32, max_people: i32, - notes: Option, + notes: Option<&'r str>, } #[put("/planned-event", data = "")] async fn update( db: &State, - data: Form, + data: Form>, _admin: AdminUser, ) -> Flash { match PlannedEvent::find_by_id(db, data.id).await { Some(planned_event) => { planned_event - .update( - db, - data.planned_amount_cox, - data.max_people, - data.notes.clone(), - ) + .update(db, data.planned_amount_cox, data.max_people, data.notes) .await; Flash::success(Redirect::to("/"), "Successfully edited the event") } diff --git a/src/rest/admin/user.rs b/src/rest/admin/user.rs index dd48715..ec8f2a4 100644 --- a/src/rest/admin/user.rs +++ b/src/rest/admin/user.rs @@ -86,18 +86,18 @@ async fn update( } #[derive(FromForm)] -struct UserAddForm { - name: String, +struct UserAddForm<'r> { + name: &'r str, is_guest: bool, } #[post("/user/new", data = "")] async fn create( db: &State, - data: Form, + data: Form>, _admin: AdminUser, ) -> Flash { - if User::create(db, data.name.clone(), data.is_guest).await { + if User::create(db, data.name, data.is_guest).await { //TODO: fix clone() above Flash::success(Redirect::to("/admin/user"), "Successfully created user") } else { diff --git a/src/rest/auth.rs b/src/rest/auth.rs index e42f80a..b98650a 100644 --- a/src/rest/auth.rs +++ b/src/rest/auth.rs @@ -28,18 +28,18 @@ fn index(flash: Option>) -> Template { } #[derive(FromForm)] -struct LoginForm { - name: String, - password: String, +struct LoginForm<'r> { + name: &'r str, + password: &'r str, } #[post("/", data = "")] async fn login( - login: Form, + login: Form>, db: &State, cookies: &CookieJar<'_>, ) -> Flash { - let user = User::login(db, login.name.clone(), login.password.clone()).await; + let user = User::login(db, login.name, login.password).await; let user = match user { Ok(user) => user, @@ -66,16 +66,16 @@ fn setpw(userid: i32) -> Template { } #[derive(FromForm)] -struct UpdatePw { +struct UpdatePw<'r> { userid: i32, - password: String, - password_confirm: String, + password: &'r str, + password_confirm: &'r str, } #[post("/set-pw", data = "")] async fn updatepw( db: &State, - updatepw: Form, + updatepw: Form>, cookies: &CookieJar<'_>, ) -> Flash { let user = User::find_by_id(db, updatepw.userid).await; @@ -93,7 +93,7 @@ async fn updatepw( ); } - user.update_pw(db, updatepw.password.clone()).await; + user.update_pw(db, updatepw.password).await; let user_json: String = format!("{}", json!(user)); cookies.add_private(Cookie::new("loggedin_user", user_json)); diff --git a/src/rest/cox.rs b/src/rest/cox.rs index 683db58..6684566 100644 --- a/src/rest/cox.rs +++ b/src/rest/cox.rs @@ -15,26 +15,29 @@ use crate::model::{ }; #[derive(FromForm)] -struct AddTripForm { +struct AddTripForm<'r> { day: String, //TODO: properly parse `planned_starting_time` - planned_starting_time: String, + planned_starting_time: &'r str, #[field(validate = range(1..))] max_people: i32, - notes: Option, + notes: Option<&'r str>, trip_type: Option, allow_guests: bool, } #[post("/trip", data = "")] -async fn create(db: &State, data: Form, cox: CoxUser) -> Flash { - //TODO: fix clones() +async fn create( + db: &State, + data: Form>, + cox: CoxUser, +) -> Flash { let trip_details_id = TripDetails::create( db, - data.planned_starting_time.clone(), + data.planned_starting_time, data.max_people, - data.day.clone(), - data.notes.clone(), + &data.day, + data.notes, data.allow_guests, data.trip_type, ) @@ -43,15 +46,11 @@ async fn create(db: &State, data: Form, cox: CoxUser) - //created Trip::new_own(db, &cox, trip_details).await; - //TODO: fix clone() Log::create( db, format!( "Cox {} created trip on {} @ {} for {} rower", - cox.name, - data.day.clone(), - data.planned_starting_time.clone(), - data.max_people, + cox.name, data.day, data.planned_starting_time, data.max_people, ), ) .await; @@ -60,30 +59,21 @@ async fn create(db: &State, data: Form, cox: CoxUser) - } #[derive(FromForm)] -struct EditTripForm { +struct EditTripForm<'r> { max_people: i32, - notes: Option, + notes: Option<&'r str>, trip_type: Option, } #[post("/trip/", data = "")] async fn update( db: &State, - data: Form, + data: Form>, trip_id: i64, cox: CoxUser, ) -> Flash { if let Some(trip) = Trip::find_by_id(db, trip_id).await { - match Trip::update_own( - db, - &cox, - &trip, - data.max_people, - data.notes.clone(), - data.trip_type, - ) - .await - { + match Trip::update_own(db, &cox, &trip, data.max_people, data.notes, data.trip_type).await { Ok(_) => Flash::success(Redirect::to("/"), "Ausfahrt erfolgreich aktualisiert."), Err(TripUpdateError::NotYourTrip) => { Flash::error(Redirect::to("/"), "Nicht deine Ausfahrt!")