fix many clones()

This commit is contained in:
philipp 2023-05-24 12:11:55 +02:00
parent cf45036642
commit 99296d4060
8 changed files with 54 additions and 71 deletions

View File

@ -144,7 +144,7 @@ FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM planned_even
pub async fn create( pub async fn create(
db: &SqlitePool, db: &SqlitePool,
name: String, name: &str,
planned_amount_cox: i32, planned_amount_cox: i32,
trip_details: TripDetails, trip_details: TripDetails,
) { ) {
@ -165,7 +165,7 @@ FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM planned_even
db: &SqlitePool, db: &SqlitePool,
planned_amount_cox: i32, planned_amount_cox: i32,
max_people: i32, max_people: i32,
notes: Option<String>, notes: Option<&str>,
) { ) {
sqlx::query!( sqlx::query!(
"UPDATE planned_event SET planned_amount_cox = ? WHERE id = ?", "UPDATE planned_event SET planned_amount_cox = ? WHERE id = ?",

View File

@ -138,7 +138,7 @@ FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM trip WHERE i
cox: &CoxUser, cox: &CoxUser,
trip: &Trip, trip: &Trip,
max_people: i32, max_people: i32,
notes: Option<String>, notes: Option<&str>,
trip_type: Option<i64>, //TODO: Move to `TripType` trip_type: Option<i64>, //TODO: Move to `TripType`
) -> Result<(), TripUpdateError> { ) -> Result<(), TripUpdateError> {
if !trip.is_trip_from_user(cox.id).await { if !trip.is_trip_from_user(cox.id).await {

View File

@ -31,10 +31,10 @@ WHERE id like ?
/// Creates a new entry in `trip_details` and returns its id. /// Creates a new entry in `trip_details` and returns its id.
pub async fn create( pub async fn create(
db: &SqlitePool, db: &SqlitePool,
planned_starting_time: String, planned_starting_time: &str,
max_people: i32, max_people: i32,
day: String, day: &str,
notes: Option<String>, notes: Option<&str>,
allow_guests: bool, allow_guests: bool,
trip_type_id: Option<i64>, trip_type_id: Option<i64>,
) -> i64 { ) -> i64 {

View File

@ -49,7 +49,7 @@ WHERE id like ?
.ok() .ok()
} }
pub async fn find_by_name(db: &SqlitePool, name: String) -> Option<Self> { pub async fn find_by_name(db: &SqlitePool, name: &str) -> Option<Self> {
sqlx::query_as!( sqlx::query_as!(
User, User,
" "
@ -79,7 +79,7 @@ ORDER BY name
.unwrap() //TODO: fixme .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!( sqlx::query!(
"INSERT INTO USER(name, is_guest) VALUES (?,?)", "INSERT INTO USER(name, is_guest) VALUES (?,?)",
name, name,
@ -103,7 +103,7 @@ ORDER BY name
.unwrap(); //Okay, because we can only create a User of a valid id .unwrap(); //Okay, because we can only create a User of a valid id
} }
pub async fn login(db: &SqlitePool, name: String, pw: String) -> Result<Self, LoginError> { pub async fn login(db: &SqlitePool, name: &str, pw: &str) -> Result<Self, LoginError> {
let Some(user) = User::find_by_name(db, name).await else { let Some(user) = User::find_by_name(db, name).await else {
return Err(LoginError::InvalidAuthenticationCombo); // Username not found 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 .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); let pw = Self::get_hashed_pw(&pw);
sqlx::query!("UPDATE user SET pw = ? where id = ?", pw, self.id) sqlx::query!("UPDATE user SET pw = ? where id = ?", pw, self.id)
.execute(db) .execute(db)

View File

@ -10,30 +10,29 @@ use crate::model::{planned_event::PlannedEvent, tripdetails::TripDetails, user::
//TODO: add constraints (e.g. planned_amount_cox > 0) //TODO: add constraints (e.g. planned_amount_cox > 0)
#[derive(FromForm)] #[derive(FromForm)]
struct AddPlannedEventForm { struct AddPlannedEventForm<'r> {
day: String, day: &'r str,
name: String, name: &'r str,
planned_amount_cox: i32, planned_amount_cox: i32,
allow_guests: bool, allow_guests: bool,
planned_starting_time: String, planned_starting_time: &'r str,
max_people: i32, max_people: i32,
notes: Option<String>, notes: Option<&'r str>,
trip_type: Option<i64>, trip_type: Option<i64>,
} }
#[post("/planned-event", data = "<data>")] #[post("/planned-event", data = "<data>")]
async fn create( async fn create(
db: &State<SqlitePool>, db: &State<SqlitePool>,
data: Form<AddPlannedEventForm>, data: Form<AddPlannedEventForm<'_>>,
_admin: AdminUser, _admin: AdminUser,
) -> Flash<Redirect> { ) -> Flash<Redirect> {
//TODO: fix clones()
let trip_details_id = TripDetails::create( let trip_details_id = TripDetails::create(
db, db,
data.planned_starting_time.clone(), data.planned_starting_time,
data.max_people, data.max_people,
data.day.clone(), data.day,
data.notes.clone(), data.notes,
data.allow_guests, data.allow_guests,
data.trip_type, data.trip_type,
) )
@ -43,36 +42,30 @@ async fn create(
//just created //just created
//the object //the object
//TODO: fix clone() PlannedEvent::create(db, data.name, data.planned_amount_cox, trip_details).await;
PlannedEvent::create(db, data.name.clone(), data.planned_amount_cox, trip_details).await;
Flash::success(Redirect::to("/"), "Successfully planned the event") Flash::success(Redirect::to("/"), "Successfully planned the event")
} }
//TODO: add constraints (e.g. planned_amount_cox > 0) //TODO: add constraints (e.g. planned_amount_cox > 0)
#[derive(FromForm)] #[derive(FromForm)]
struct UpdatePlannedEventForm { struct UpdatePlannedEventForm<'r> {
id: i64, id: i64,
planned_amount_cox: i32, planned_amount_cox: i32,
max_people: i32, max_people: i32,
notes: Option<String>, notes: Option<&'r str>,
} }
#[put("/planned-event", data = "<data>")] #[put("/planned-event", data = "<data>")]
async fn update( async fn update(
db: &State<SqlitePool>, db: &State<SqlitePool>,
data: Form<UpdatePlannedEventForm>, data: Form<UpdatePlannedEventForm<'_>>,
_admin: AdminUser, _admin: AdminUser,
) -> Flash<Redirect> { ) -> Flash<Redirect> {
match PlannedEvent::find_by_id(db, data.id).await { match PlannedEvent::find_by_id(db, data.id).await {
Some(planned_event) => { Some(planned_event) => {
planned_event planned_event
.update( .update(db, data.planned_amount_cox, data.max_people, data.notes)
db,
data.planned_amount_cox,
data.max_people,
data.notes.clone(),
)
.await; .await;
Flash::success(Redirect::to("/"), "Successfully edited the event") Flash::success(Redirect::to("/"), "Successfully edited the event")
} }

View File

@ -86,18 +86,18 @@ async fn update(
} }
#[derive(FromForm)] #[derive(FromForm)]
struct UserAddForm { struct UserAddForm<'r> {
name: String, name: &'r str,
is_guest: bool, is_guest: bool,
} }
#[post("/user/new", data = "<data>")] #[post("/user/new", data = "<data>")]
async fn create( async fn create(
db: &State<SqlitePool>, db: &State<SqlitePool>,
data: Form<UserAddForm>, data: Form<UserAddForm<'_>>,
_admin: AdminUser, _admin: AdminUser,
) -> Flash<Redirect> { ) -> Flash<Redirect> {
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 //TODO: fix clone() above
Flash::success(Redirect::to("/admin/user"), "Successfully created user") Flash::success(Redirect::to("/admin/user"), "Successfully created user")
} else { } else {

View File

@ -28,18 +28,18 @@ fn index(flash: Option<FlashMessage<'_>>) -> Template {
} }
#[derive(FromForm)] #[derive(FromForm)]
struct LoginForm { struct LoginForm<'r> {
name: String, name: &'r str,
password: String, password: &'r str,
} }
#[post("/", data = "<login>")] #[post("/", data = "<login>")]
async fn login( async fn login(
login: Form<LoginForm>, login: Form<LoginForm<'_>>,
db: &State<SqlitePool>, db: &State<SqlitePool>,
cookies: &CookieJar<'_>, cookies: &CookieJar<'_>,
) -> Flash<Redirect> { ) -> Flash<Redirect> {
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 { let user = match user {
Ok(user) => user, Ok(user) => user,
@ -66,16 +66,16 @@ fn setpw(userid: i32) -> Template {
} }
#[derive(FromForm)] #[derive(FromForm)]
struct UpdatePw { struct UpdatePw<'r> {
userid: i32, userid: i32,
password: String, password: &'r str,
password_confirm: String, password_confirm: &'r str,
} }
#[post("/set-pw", data = "<updatepw>")] #[post("/set-pw", data = "<updatepw>")]
async fn updatepw( async fn updatepw(
db: &State<SqlitePool>, db: &State<SqlitePool>,
updatepw: Form<UpdatePw>, updatepw: Form<UpdatePw<'_>>,
cookies: &CookieJar<'_>, cookies: &CookieJar<'_>,
) -> Flash<Redirect> { ) -> Flash<Redirect> {
let user = User::find_by_id(db, updatepw.userid).await; 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)); let user_json: String = format!("{}", json!(user));
cookies.add_private(Cookie::new("loggedin_user", user_json)); cookies.add_private(Cookie::new("loggedin_user", user_json));

View File

@ -15,26 +15,29 @@ use crate::model::{
}; };
#[derive(FromForm)] #[derive(FromForm)]
struct AddTripForm { struct AddTripForm<'r> {
day: String, day: String,
//TODO: properly parse `planned_starting_time` //TODO: properly parse `planned_starting_time`
planned_starting_time: String, planned_starting_time: &'r str,
#[field(validate = range(1..))] #[field(validate = range(1..))]
max_people: i32, max_people: i32,
notes: Option<String>, notes: Option<&'r str>,
trip_type: Option<i64>, trip_type: Option<i64>,
allow_guests: bool, allow_guests: bool,
} }
#[post("/trip", data = "<data>")] #[post("/trip", data = "<data>")]
async fn create(db: &State<SqlitePool>, data: Form<AddTripForm>, cox: CoxUser) -> Flash<Redirect> { async fn create(
//TODO: fix clones() db: &State<SqlitePool>,
data: Form<AddTripForm<'_>>,
cox: CoxUser,
) -> Flash<Redirect> {
let trip_details_id = TripDetails::create( let trip_details_id = TripDetails::create(
db, db,
data.planned_starting_time.clone(), data.planned_starting_time,
data.max_people, data.max_people,
data.day.clone(), &data.day,
data.notes.clone(), data.notes,
data.allow_guests, data.allow_guests,
data.trip_type, data.trip_type,
) )
@ -43,15 +46,11 @@ async fn create(db: &State<SqlitePool>, data: Form<AddTripForm>, cox: CoxUser) -
//created //created
Trip::new_own(db, &cox, trip_details).await; Trip::new_own(db, &cox, trip_details).await;
//TODO: fix clone()
Log::create( Log::create(
db, db,
format!( format!(
"Cox {} created trip on {} @ {} for {} rower", "Cox {} created trip on {} @ {} for {} rower",
cox.name, cox.name, data.day, data.planned_starting_time, data.max_people,
data.day.clone(),
data.planned_starting_time.clone(),
data.max_people,
), ),
) )
.await; .await;
@ -60,30 +59,21 @@ async fn create(db: &State<SqlitePool>, data: Form<AddTripForm>, cox: CoxUser) -
} }
#[derive(FromForm)] #[derive(FromForm)]
struct EditTripForm { struct EditTripForm<'r> {
max_people: i32, max_people: i32,
notes: Option<String>, notes: Option<&'r str>,
trip_type: Option<i64>, trip_type: Option<i64>,
} }
#[post("/trip/<trip_id>", data = "<data>")] #[post("/trip/<trip_id>", data = "<data>")]
async fn update( async fn update(
db: &State<SqlitePool>, db: &State<SqlitePool>,
data: Form<EditTripForm>, data: Form<EditTripForm<'_>>,
trip_id: i64, trip_id: i64,
cox: CoxUser, cox: CoxUser,
) -> Flash<Redirect> { ) -> Flash<Redirect> {
if let Some(trip) = Trip::find_by_id(db, trip_id).await { if let Some(trip) = Trip::find_by_id(db, trip_id).await {
match Trip::update_own( match Trip::update_own(db, &cox, &trip, data.max_people, data.notes, data.trip_type).await {
db,
&cox,
&trip,
data.max_people,
data.notes.clone(),
data.trip_type,
)
.await
{
Ok(_) => Flash::success(Redirect::to("/"), "Ausfahrt erfolgreich aktualisiert."), Ok(_) => Flash::success(Redirect::to("/"), "Ausfahrt erfolgreich aktualisiert."),
Err(TripUpdateError::NotYourTrip) => { Err(TripUpdateError::NotYourTrip) => {
Flash::error(Redirect::to("/"), "Nicht deine Ausfahrt!") Flash::error(Redirect::to("/"), "Nicht deine Ausfahrt!")