fix many clones()

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

View File

@ -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<String>,
notes: Option<&'r str>,
trip_type: Option<i64>,
}
#[post("/planned-event", data = "<data>")]
async fn create(
db: &State<SqlitePool>,
data: Form<AddPlannedEventForm>,
data: Form<AddPlannedEventForm<'_>>,
_admin: AdminUser,
) -> Flash<Redirect> {
//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<String>,
notes: Option<&'r str>,
}
#[put("/planned-event", data = "<data>")]
async fn update(
db: &State<SqlitePool>,
data: Form<UpdatePlannedEventForm>,
data: Form<UpdatePlannedEventForm<'_>>,
_admin: AdminUser,
) -> Flash<Redirect> {
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")
}

View File

@ -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 = "<data>")]
async fn create(
db: &State<SqlitePool>,
data: Form<UserAddForm>,
data: Form<UserAddForm<'_>>,
_admin: AdminUser,
) -> 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
Flash::success(Redirect::to("/admin/user"), "Successfully created user")
} else {

View File

@ -28,18 +28,18 @@ fn index(flash: Option<FlashMessage<'_>>) -> Template {
}
#[derive(FromForm)]
struct LoginForm {
name: String,
password: String,
struct LoginForm<'r> {
name: &'r str,
password: &'r str,
}
#[post("/", data = "<login>")]
async fn login(
login: Form<LoginForm>,
login: Form<LoginForm<'_>>,
db: &State<SqlitePool>,
cookies: &CookieJar<'_>,
) -> 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 {
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 = "<updatepw>")]
async fn updatepw(
db: &State<SqlitePool>,
updatepw: Form<UpdatePw>,
updatepw: Form<UpdatePw<'_>>,
cookies: &CookieJar<'_>,
) -> Flash<Redirect> {
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));

View File

@ -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<String>,
notes: Option<&'r str>,
trip_type: Option<i64>,
allow_guests: bool,
}
#[post("/trip", data = "<data>")]
async fn create(db: &State<SqlitePool>, data: Form<AddTripForm>, cox: CoxUser) -> Flash<Redirect> {
//TODO: fix clones()
async fn create(
db: &State<SqlitePool>,
data: Form<AddTripForm<'_>>,
cox: CoxUser,
) -> Flash<Redirect> {
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<SqlitePool>, data: Form<AddTripForm>, 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<SqlitePool>, data: Form<AddTripForm>, cox: CoxUser) -
}
#[derive(FromForm)]
struct EditTripForm {
struct EditTripForm<'r> {
max_people: i32,
notes: Option<String>,
notes: Option<&'r str>,
trip_type: Option<i64>,
}
#[post("/trip/<trip_id>", data = "<data>")]
async fn update(
db: &State<SqlitePool>,
data: Form<EditTripForm>,
data: Form<EditTripForm<'_>>,
trip_id: i64,
cox: CoxUser,
) -> Flash<Redirect> {
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!")