diff --git a/src/model/planned_event.rs b/src/model/planned_event.rs index 7b08ae2..497c615 100644 --- a/src/model/planned_event.rs +++ b/src/model/planned_event.rs @@ -25,15 +25,13 @@ pub struct PlannedEventWithUser { impl PlannedEvent { pub async fn get_for_day(db: &SqlitePool, day: NaiveDate) -> Vec { - let day = format!("{}", day); + let day = format!("{day}"); let events = sqlx::query_as!( PlannedEvent, - " -SELECT planned_event.id, name, planned_amount_cox, allow_guests, trip_details_id, planned_starting_time, max_people, day, notes + "SELECT planned_event.id, name, planned_amount_cox, allow_guests, trip_details_id, planned_starting_time, max_people, day, notes FROM planned_event INNER JOIN trip_details ON planned_event.trip_details_id = trip_details.id -WHERE day=? - ", +WHERE day=?", day ) .fetch_all(db) @@ -46,22 +44,20 @@ WHERE day=? planned_event: event.clone(), cox: Self::get_all_cox_for_id(db, event.id).await, rower: Self::get_all_rower_for_id(db, event.id).await, - }) + }); } ret } pub async fn rower_can_register(db: &SqlitePool, trip_details_id: i64) -> bool { let amount_currently_registered = sqlx::query!( - " - SELECT COUNT(*) as count FROM user_trip WHERE trip_details_id = ? - ", + "SELECT COUNT(*) as count FROM user_trip WHERE trip_details_id = ?", trip_details_id ) .fetch_one(db) .await .unwrap(); //TODO: fixme - let amount_currently_registered = amount_currently_registered.count as i64; + let amount_currently_registered = i64::from(amount_currently_registered.count); let amount_allowed_to_register = sqlx::query!( " @@ -87,11 +83,8 @@ SELECT (SELECT name FROM user WHERE cox_id = id) as name FROM trip WHERE planned .fetch_all(db) .await .unwrap(); //TODO: fixme - let mut ret = Vec::new(); - for r in res { - ret.push(r.name); - } - ret + + res.into_iter().map(|x| x.name).collect() } async fn get_all_rower_for_id(db: &SqlitePool, id: i64) -> Vec { @@ -104,14 +97,11 @@ SELECT (SELECT name FROM user WHERE user_trip.user_id = user.id) as name FROM us .fetch_all(db) .await .unwrap(); //TODO: fixme - let mut ret = Vec::new(); - for r in res { - ret.push(r.name); - } - ret + + res.into_iter().map(|x| x.name).collect() } - pub async fn new( + pub async fn create( db: &SqlitePool, name: String, planned_amount_cox: i32, diff --git a/src/model/trip.rs b/src/model/trip.rs index 7e9245b..77383c8 100644 --- a/src/model/trip.rs +++ b/src/model/trip.rs @@ -23,7 +23,7 @@ pub struct TripWithUser { impl Trip { pub async fn get_for_day(db: &SqlitePool, day: NaiveDate) -> Vec { - let day = format!("{}", day); + let day = format!("{day}"); let trips = sqlx::query_as!( Trip, " @@ -43,7 +43,7 @@ WHERE day=? ret.push(TripWithUser { trip: trip.clone(), rower: Self::get_all_rower_for_id(db, trip.id).await, - }) + }); } ret } @@ -58,11 +58,8 @@ SELECT (SELECT name FROM user WHERE user_trip.user_id = user.id) as name FROM us .fetch_all(db) .await .unwrap(); //TODO: fixme - let mut ret = Vec::new(); - for r in res { - ret.push(r.name); - } - ret + + res.into_iter().map(|x| x.name).collect() } pub async fn new_own(db: &SqlitePool, cox_id: i64, trip_details_id: i64) { diff --git a/src/model/tripdetails.rs b/src/model/tripdetails.rs index 9ea0d56..e26c88b 100644 --- a/src/model/tripdetails.rs +++ b/src/model/tripdetails.rs @@ -11,7 +11,7 @@ pub struct TripDetails { } impl TripDetails { - pub async fn new( + pub async fn create( db: &SqlitePool, planned_starting_time: String, max_people: i32, diff --git a/src/model/user.rs b/src/model/user.rs index b74031f..8d73a0b 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -121,11 +121,11 @@ WHERE name like ? Ok(user) } - fn get_hashed_pw(pw: String) -> String { + fn get_hashed_pw(pw: &str) -> String { let salt = SaltString::from_b64("dS/X5/sPEKTj4Rzs/CuvzQ").unwrap(); let argon2 = Argon2::default(); argon2 - .hash_password(&pw.as_bytes(), &salt) + .hash_password(pw.as_bytes(), &salt) .unwrap() .to_string() } @@ -135,7 +135,7 @@ WHERE name like ? match user.pw.clone() { Some(user_pw) => { - let password_hash = Self::get_hashed_pw(pw); + let password_hash = Self::get_hashed_pw(&pw); if password_hash == user_pw { return Ok(user); } @@ -167,7 +167,7 @@ FROM user } pub async fn update_pw(&self, db: &SqlitePool, pw: String) { - let pw = Self::get_hashed_pw(pw); + let pw = Self::get_hashed_pw(&pw); sqlx::query!("UPDATE user SET pw = ? where id = ?", pw, self.id) .execute(db) .await @@ -182,7 +182,7 @@ impl<'r> FromRequest<'r> for User { async fn from_request(req: &'r Request<'_>) -> request::Outcome { match req.cookies().get_private("loggedin_user") { Some(user) => { - let user: User = serde_json::from_str(&user.value()).unwrap(); //TODO: fixme + let user: User = serde_json::from_str(user.value()).unwrap(); //TODO: fixme Outcome::Success(user) } None => Outcome::Failure((Status::Unauthorized, LoginError::NotLoggedIn)), @@ -197,7 +197,7 @@ impl<'r> FromRequest<'r> for AdminUser { async fn from_request(req: &'r Request<'_>) -> request::Outcome { match req.cookies().get_private("loggedin_user") { Some(user) => { - let user: User = serde_json::from_str(&user.value()).unwrap(); //TODO: fixme + let user: User = serde_json::from_str(user.value()).unwrap(); //TODO: fixme match user.try_into() { Ok(user) => Outcome::Success(user), Err(_) => Outcome::Failure((Status::Unauthorized, LoginError::NotAnAdmin)), @@ -215,7 +215,7 @@ impl<'r> FromRequest<'r> for CoxUser { async fn from_request(req: &'r Request<'_>) -> request::Outcome { match req.cookies().get_private("loggedin_user") { Some(user) => { - let user: User = serde_json::from_str(&user.value()).unwrap(); //TODO: fixme + let user: User = serde_json::from_str(user.value()).unwrap(); //TODO: fixme match user.try_into() { Ok(user) => Outcome::Success(user), Err(_) => Outcome::Failure((Status::Unauthorized, LoginError::NotAnAdmin)), diff --git a/src/model/usertrip.rs b/src/model/usertrip.rs index dec0ff3..ba0897a 100644 --- a/src/model/usertrip.rs +++ b/src/model/usertrip.rs @@ -3,7 +3,7 @@ use sqlx::SqlitePool; pub struct UserTrip {} impl UserTrip { - pub async fn new(db: &SqlitePool, user_id: i64, trip_details_id: i64) -> bool { + pub async fn create(db: &SqlitePool, user_id: i64, trip_details_id: i64) -> bool { sqlx::query!( "INSERT INTO user_trip (user_id, trip_details_id) VALUES(?, ?)", user_id, diff --git a/src/rest/admin/planned_event.rs b/src/rest/admin/planned_event.rs index 941d739..b3d678b 100644 --- a/src/rest/admin/planned_event.rs +++ b/src/rest/admin/planned_event.rs @@ -27,7 +27,7 @@ async fn create( _admin: AdminUser, ) -> Flash { //TODO: fix clones() - let trip_details_id = TripDetails::new( + let trip_details_id = TripDetails::create( db, data.planned_starting_time.clone(), data.max_people, @@ -37,7 +37,7 @@ async fn create( .await; //TODO: fix clone() - PlannedEvent::new( + PlannedEvent::create( db, data.name.clone(), data.planned_amount_cox, diff --git a/src/rest/admin/user.rs b/src/rest/admin/user.rs index f345e7f..6512891 100644 --- a/src/rest/admin/user.rs +++ b/src/rest/admin/user.rs @@ -44,14 +44,11 @@ async fn update( _admin: AdminUser, ) -> Flash { let user = User::find_by_id(db, data.id).await; - let user = match user { - Ok(user) => user, - Err(_) => { + let Ok(user) = user else { return Flash::error( Redirect::to("/admin/user"), format!("User with ID {} does not exist!", data.id), ) - } }; user.update(db, data.is_cox, data.is_admin, data.is_guest) diff --git a/src/rest/auth.rs b/src/rest/auth.rs index 8f403af..2b149a3 100644 --- a/src/rest/auth.rs +++ b/src/rest/auth.rs @@ -14,7 +14,7 @@ use sqlx::SqlitePool; use crate::model::user::{LoginError, User}; #[get("/")] -async fn index(flash: Option>) -> Template { +fn index(flash: Option>) -> Template { let mut context = tera::Context::new(); if let Some(msg) = flash { @@ -59,7 +59,7 @@ async fn login( } #[get("/set-pw/")] -async fn setpw(userid: i32) -> Template { +fn setpw(userid: i32) -> Template { Template::render("auth/set-pw", context!(userid)) } @@ -77,14 +77,11 @@ async fn updatepw( cookies: &CookieJar<'_>, ) -> Flash { let user = User::find_by_id(db, updatepw.userid).await; - let user = match user { - Ok(user) => user, - Err(_) => { + let Ok(user) = user else{ return Flash::error( Redirect::to("/auth"), format!("User with ID {} does not exist!", updatepw.userid), ) - } }; if updatepw.password != updatepw.password_confirm { @@ -106,7 +103,7 @@ async fn updatepw( } #[get("/logout")] -async fn logout(cookies: &CookieJar<'_>, _user: User) -> Flash { +fn logout(cookies: &CookieJar<'_>, _user: User) -> Flash { cookies.remove_private(Cookie::named("loggedin_user")); Flash::success(Redirect::to("/auth"), "Logout erfolgreich") diff --git a/src/rest/cox.rs b/src/rest/cox.rs index 5ac43f3..8054cd6 100644 --- a/src/rest/cox.rs +++ b/src/rest/cox.rs @@ -20,7 +20,7 @@ struct AddTripForm { #[post("/trip", data = "")] async fn create(db: &State, data: Form, cox: CoxUser) -> Flash { //TODO: fix clones() - let trip_details_id = TripDetails::new( + let trip_details_id = TripDetails::create( db, data.planned_starting_time.clone(), data.max_people, diff --git a/src/rest/mod.rs b/src/rest/mod.rs index 1075400..732e40b 100644 --- a/src/rest/mod.rs +++ b/src/rest/mod.rs @@ -47,7 +47,7 @@ async fn join(db: &State, trip_details_id: i64, user: User) -> Flash return Flash::error(Redirect::to("/"), "Bereits ausgebucht!"); } - if UserTrip::new(db, user.id, trip_details_id).await { + if UserTrip::create(db, user.id, trip_details_id).await { Flash::success(Redirect::to("/"), "Erfolgreich angemeldet!") } else { Flash::error(Redirect::to("/"), "Du nimmst bereits teil!")