clean code with clippy

This commit is contained in:
2023-07-25 13:22:11 +02:00
parent 3765541674
commit a7789af713
10 changed files with 243 additions and 463 deletions

View File

@ -1,17 +1,31 @@
use chrono::NaiveDate;
use rocket::FromForm;
use serde::{Deserialize, Serialize};
use sqlx::{FromRow, SqlitePool};
#[derive(FromRow, Debug, Serialize, Deserialize)]
pub struct TripDetails {
pub id: i64,
planned_starting_time: String,
max_people: i64,
day: String,
notes: Option<String>,
pub planned_starting_time: String,
pub max_people: i64,
pub day: String,
pub notes: Option<String>,
pub allow_guests: bool,
trip_type_id: Option<i64>,
always_show: bool,
pub trip_type_id: Option<i64>,
pub always_show: bool,
}
#[derive(FromForm, Serialize)]
pub struct TripDetailsToAdd<'r> {
//TODO: properly parse `planned_starting_time`
pub planned_starting_time: &'r str,
pub max_people: i32,
pub day: String,
//#[field(validate = range(1..))] TODO: fixme
pub notes: Option<&'r str>,
pub trip_type: Option<i64>,
pub allow_guests: bool,
pub always_show: bool,
}
impl TripDetails {
@ -31,25 +45,16 @@ WHERE id like ?
}
/// Creates a new entry in `trip_details` and returns its id.
pub async fn create(
db: &SqlitePool,
planned_starting_time: &str,
max_people: i32,
day: &str,
notes: Option<&str>,
allow_guests: bool,
trip_type_id: Option<i64>,
always_show: bool,
) -> i64 {
pub async fn create(db: &SqlitePool, tripdetails: TripDetailsToAdd<'_>) -> i64 {
let query = sqlx::query!(
"INSERT INTO trip_details(planned_starting_time, max_people, day, notes, allow_guests, trip_type_id, always_show) VALUES(?, ?, ?, ?, ?, ?, ?)" ,
planned_starting_time,
max_people,
day,
notes,
allow_guests,
trip_type_id,
always_show
tripdetails.planned_starting_time,
tripdetails.max_people,
tripdetails.day,
tripdetails.notes,
tripdetails.allow_guests,
tripdetails.trip_type,
tripdetails.always_show
)
.execute(db)
.await
@ -87,7 +92,7 @@ ORDER BY day;",
#[cfg(test)]
mod test {
use crate::testdb;
use crate::{model::tripdetails::TripDetailsToAdd, testdb};
use super::TripDetails;
use sqlx::SqlitePool;
@ -113,13 +118,15 @@ mod test {
assert_eq!(
TripDetails::create(
&pool,
"10:00".into(),
2,
"1970-01-01".into(),
None,
false,
None,
false
TripDetailsToAdd {
planned_starting_time: "10:00".into(),
max_people: 2,
day: "1970-01-01".into(),
notes: None,
allow_guests: false,
trip_type: None,
always_show: false
}
)
.await,
3,
@ -127,13 +134,15 @@ mod test {
assert_eq!(
TripDetails::create(
&pool,
"10:00".into(),
2,
"1970-01-01".into(),
None,
false,
None,
false
TripDetailsToAdd {
planned_starting_time: "10:00".into(),
max_people: 2,
day: "1970-01-01".into(),
notes: None,
allow_guests: false,
trip_type: None,
always_show: false
}
)
.await,
4,