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,3 +1,4 @@
use rocket::FromForm;
use serde::{Deserialize, Serialize};
use sqlx::{FromRow, SqlitePool};
@ -18,36 +19,46 @@ pub struct Boat {
external: bool,
}
#[derive(FromForm)]
pub struct BoatToAdd<'r> {
pub name: &'r str,
pub amount_seats: i64,
pub year_built: Option<i64>,
pub boatbuilder: Option<&'r str>,
pub default_shipmaster_only_steering: bool,
pub skull: bool,
pub external: bool,
pub location_id: Option<i64>,
pub owner: Option<i64>,
}
#[derive(FromForm)]
pub struct BoatToUpdate<'r> {
pub id: i32,
pub name: &'r str,
pub amount_seats: i64,
pub year_built: Option<i64>,
pub boatbuilder: Option<&'r str>,
pub default_shipmaster_only_steering: bool,
pub skull: bool,
pub external: bool,
pub location_id: Option<i64>,
pub owner: Option<i64>,
}
impl Boat {
pub async fn find_by_id(db: &SqlitePool, id: i32) -> Option<Self> {
sqlx::query_as!(
Self,
"
SELECT id, name, amount_seats, location_id, owner, year_built, boatbuilder, default_shipmaster_only_steering, skull, external
FROM boat
WHERE id like ?
",
"SELECT id, name, amount_seats, location_id, owner, year_built, boatbuilder, default_shipmaster_only_steering, skull, external
FROM boat
WHERE id like ?",
id
)
.fetch_one(db)
.await
.ok()
}
//
// pub async fn find_by_name(db: &SqlitePool, name: &str) -> Option<Self> {
// sqlx::query_as!(
// User,
// "
//SELECT id, name, pw, is_cox, is_admin, is_guest, deleted, last_access
//FROM user
//WHERE name like ?
// ",
// name
// )
// .fetch_one(db)
// .await
// .ok()
// }
pub async fn is_locked(&self, db: &SqlitePool) -> bool {
sqlx::query!("SELECT * FROM boat_damage WHERE boat_id=? AND lock_boat=true AND user_id_verified is null", self.id).fetch_optional(db).await.unwrap().is_some()
@ -78,58 +89,35 @@ ORDER BY amount_seats DESC
.unwrap() //TODO: fixme
}
pub async fn create(
db: &SqlitePool,
name: &str,
amount_seats: i64,
year_built: Option<i64>,
boatbuilder: Option<&str>,
default_shipmaster_only_steering: bool,
skull: bool,
external: bool,
location_id: Option<i64>,
owner: Option<i64>,
) -> bool {
pub async fn create(db: &SqlitePool, boat: BoatToAdd<'_>) -> bool {
sqlx::query!(
"INSERT INTO boat(name, amount_seats, year_built, boatbuilder, default_shipmaster_only_steering, skull, external, location_id, owner) VALUES (?,?,?,?,?,?,?,?,?)",
name,
amount_seats,
year_built,
boatbuilder,
default_shipmaster_only_steering,
skull,
external,
location_id,
owner
boat.name,
boat.amount_seats,
boat.year_built,
boat.boatbuilder,
boat.default_shipmaster_only_steering,
boat.skull,
boat.external,
boat.location_id,
boat.owner
)
.execute(db)
.await.is_ok()
}
pub async fn update(
&self,
db: &SqlitePool,
name: &str,
amount_seats: i64,
year_built: Option<i64>,
boatbuilder: Option<&str>,
default_shipmaster_only_steering: bool,
skull: bool,
external: bool,
location_id: Option<i64>,
owner: Option<i64>,
) -> bool {
pub async fn update(&self, db: &SqlitePool, boat: BoatToUpdate<'_>) -> bool {
sqlx::query!(
"UPDATE boat SET name=?, amount_seats=?, year_built=?, boatbuilder=?, default_shipmaster_only_steering=?, skull=?, external=?, location_id=?, owner=? WHERE id=?",
name,
amount_seats,
year_built,
boatbuilder,
default_shipmaster_only_steering,
skull,
external,
location_id,
owner,
boat.name,
boat.amount_seats,
boat.year_built,
boat.boatbuilder,
boat.default_shipmaster_only_steering,
boat.skull,
boat.external,
boat.location_id,
boat.owner,
self.id
)
.execute(db)
@ -141,13 +129,16 @@ ORDER BY amount_seats DESC
sqlx::query!("DELETE FROM boat WHERE id=?", self.id)
.execute(db)
.await
.unwrap(); //Okay, because we can only create a User of a valid id
.unwrap(); //Okay, because we can only create a Boat of a valid id
}
}
#[cfg(test)]
mod test {
use crate::{model::boat::Boat, testdb};
use crate::{
model::boat::{Boat, BoatToAdd},
testdb,
};
use sqlx::SqlitePool;
@ -179,15 +170,17 @@ mod test {
assert_eq!(
Boat::create(
&pool,
"new-boat-name".into(),
42,
None,
"Best Boatbuilder".into(),
true,
true,
false,
Some(1),
None
BoatToAdd {
name: "new-boat-name".into(),
amount_seats: 42,
year_built: None,
boatbuilder: "Best Boatbuilder".into(),
default_shipmaster_only_steering: true,
skull: true,
external: false,
location_id: Some(1),
owner: None
}
)
.await,
true
@ -201,15 +194,17 @@ mod test {
assert_eq!(
Boat::create(
&pool,
"Haichenbach".into(),
42,
None,
"Best Boatbuilder".into(),
true,
true,
false,
Some(1),
None
BoatToAdd {
name: "Haichenbach".into(),
amount_seats: 42,
year_built: None,
boatbuilder: "Best Boatbuilder".into(),
default_shipmaster_only_steering: true,
skull: true,
external: false,
location_id: Some(1),
owner: None
}
)
.await,
false