fix many clones()
This commit is contained in:
parent
cf45036642
commit
99296d4060
@ -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<String>,
|
||||
notes: Option<&str>,
|
||||
) {
|
||||
sqlx::query!(
|
||||
"UPDATE planned_event SET planned_amount_cox = ? WHERE id = ?",
|
||||
|
@ -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<String>,
|
||||
notes: Option<&str>,
|
||||
trip_type: Option<i64>, //TODO: Move to `TripType`
|
||||
) -> Result<(), TripUpdateError> {
|
||||
if !trip.is_trip_from_user(cox.id).await {
|
||||
|
@ -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<String>,
|
||||
day: &str,
|
||||
notes: Option<&str>,
|
||||
allow_guests: bool,
|
||||
trip_type_id: Option<i64>,
|
||||
) -> i64 {
|
||||
|
@ -49,7 +49,7 @@ WHERE id like ?
|
||||
.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!(
|
||||
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<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 {
|
||||
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)
|
||||
|
@ -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")
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -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));
|
||||
|
@ -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!")
|
||||
|
Loading…
x
Reference in New Issue
Block a user