clean code with clippy
This commit is contained in:
@ -118,7 +118,7 @@ pub enum LogbookCreateError {
|
||||
BoatLocked,
|
||||
BoatNotFound,
|
||||
TooManyRowers(usize, usize),
|
||||
RowerAlreadyOnWater(User),
|
||||
RowerAlreadyOnWater(Box<User>),
|
||||
RowerCreateError(i64, String),
|
||||
ArrivalNotAfterDeparture,
|
||||
SteeringPersonNotInRowers,
|
||||
@ -386,7 +386,7 @@ ORDER BY departure DESC
|
||||
let user = User::find_by_id(db, *rower as i32).await.unwrap();
|
||||
|
||||
if user.on_water(db).await {
|
||||
return Err(LogbookCreateError::RowerAlreadyOnWater(user));
|
||||
return Err(LogbookCreateError::RowerAlreadyOnWater(Box::new(user)));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -98,6 +98,15 @@ FROM trip WHERE planned_event_id = ?
|
||||
}
|
||||
}
|
||||
|
||||
pub struct EventUpdate<'a> {
|
||||
pub name: &'a str,
|
||||
pub planned_amount_cox: i32,
|
||||
pub max_people: i32,
|
||||
pub notes: Option<&'a str>,
|
||||
pub always_show: bool,
|
||||
pub is_locked: bool,
|
||||
}
|
||||
|
||||
impl PlannedEvent {
|
||||
pub async fn find_by_id(db: &SqlitePool, id: i64) -> Option<Self> {
|
||||
sqlx::query_as!(
|
||||
@ -207,20 +216,11 @@ INNER JOIN trip_details ON planned_event.trip_details_id = trip_details.id",
|
||||
}
|
||||
|
||||
//TODO: create unit test
|
||||
pub async fn update(
|
||||
&self,
|
||||
db: &SqlitePool,
|
||||
name: &str,
|
||||
planned_amount_cox: i32,
|
||||
max_people: i32,
|
||||
notes: Option<&str>,
|
||||
always_show: bool,
|
||||
is_locked: bool,
|
||||
) {
|
||||
pub async fn update(&self, db: &SqlitePool, update: &EventUpdate<'_>) {
|
||||
sqlx::query!(
|
||||
"UPDATE planned_event SET name = ?, planned_amount_cox = ? WHERE id = ?",
|
||||
name,
|
||||
planned_amount_cox,
|
||||
update.name,
|
||||
update.planned_amount_cox,
|
||||
self.id
|
||||
)
|
||||
.execute(db)
|
||||
@ -229,10 +229,10 @@ INNER JOIN trip_details ON planned_event.trip_details_id = trip_details.id",
|
||||
|
||||
sqlx::query!(
|
||||
"UPDATE trip_details SET max_people = ?, notes = ?, always_show = ?, is_locked = ? WHERE id = ?",
|
||||
max_people,
|
||||
notes,
|
||||
always_show,
|
||||
is_locked,
|
||||
update.max_people,
|
||||
update.notes,
|
||||
update.always_show,
|
||||
update.is_locked,
|
||||
self.trip_details_id
|
||||
)
|
||||
.execute(db)
|
||||
@ -241,16 +241,18 @@ INNER JOIN trip_details ON planned_event.trip_details_id = trip_details.id",
|
||||
}
|
||||
|
||||
pub async fn delete(&self, db: &SqlitePool) -> Result<(), String> {
|
||||
if Registration::all_rower(db, self.trip_details_id)
|
||||
if !Registration::all_rower(db, self.trip_details_id)
|
||||
.await
|
||||
.len()
|
||||
> 0
|
||||
.is_empty()
|
||||
{
|
||||
return Err(
|
||||
"Event kann nicht gelöscht werden, weil mind. 1 Ruderer angemeldet ist.".into(),
|
||||
);
|
||||
}
|
||||
if Registration::all_cox(db, self.trip_details_id).await.len() > 0 {
|
||||
if !Registration::all_cox(db, self.trip_details_id)
|
||||
.await
|
||||
.is_empty()
|
||||
{
|
||||
return Err(
|
||||
"Event kann nicht gelöscht werden, weil mind. 1 Steuerperson angemeldet ist."
|
||||
.into(),
|
||||
@ -326,7 +328,7 @@ mod test {
|
||||
let pool = testdb!();
|
||||
let planned_event = PlannedEvent::find_by_id(&pool, 1).await.unwrap();
|
||||
|
||||
planned_event.delete(&pool).await;
|
||||
planned_event.delete(&pool).await.unwrap();
|
||||
|
||||
let res =
|
||||
PlannedEvent::get_for_day(&pool, NaiveDate::from_ymd_opt(1970, 1, 1).unwrap()).await;
|
||||
|
@ -34,6 +34,16 @@ pub struct TripWithUserAndType {
|
||||
trip_type: Option<TripType>,
|
||||
}
|
||||
|
||||
pub struct TripUpdate<'a> {
|
||||
pub cox: &'a CoxUser,
|
||||
pub trip: &'a Trip,
|
||||
pub max_people: i32,
|
||||
pub notes: Option<&'a str>,
|
||||
pub trip_type: Option<i64>, //TODO: Move to `TripType`
|
||||
pub always_show: bool,
|
||||
pub is_locked: bool,
|
||||
}
|
||||
|
||||
impl TripWithUserAndType {
|
||||
pub async fn from(db: &SqlitePool, trip: Trip) -> Self {
|
||||
let mut trip_type = None;
|
||||
@ -177,40 +187,36 @@ WHERE day=?
|
||||
/// Cox decides to update own trip.
|
||||
pub async fn update_own(
|
||||
db: &SqlitePool,
|
||||
cox: &CoxUser,
|
||||
trip: &Trip,
|
||||
max_people: i32,
|
||||
notes: Option<&str>,
|
||||
trip_type: Option<i64>, //TODO: Move to `TripType`
|
||||
always_show: bool,
|
||||
is_locked: bool,
|
||||
update: &TripUpdate<'_>,
|
||||
) -> Result<(), TripUpdateError> {
|
||||
if !trip.is_trip_from_user(cox.id) {
|
||||
if !update.trip.is_trip_from_user(update.cox.id) {
|
||||
return Err(TripUpdateError::NotYourTrip);
|
||||
}
|
||||
|
||||
let Some(trip_details_id) = trip.trip_details_id else {
|
||||
let Some(trip_details_id) = update.trip.trip_details_id else {
|
||||
return Err(TripUpdateError::TripDetailsDoesNotExist); //TODO: Remove?
|
||||
};
|
||||
|
||||
sqlx::query!(
|
||||
"UPDATE trip_details SET max_people = ?, notes = ?, trip_type_id = ?, always_show = ?, is_locked = ? WHERE id = ?",
|
||||
max_people,
|
||||
notes,
|
||||
trip_type,
|
||||
always_show,
|
||||
is_locked,
|
||||
update.max_people,
|
||||
update.notes,
|
||||
update.trip_type,
|
||||
update.always_show,
|
||||
update.is_locked,
|
||||
trip_details_id
|
||||
)
|
||||
.execute(db)
|
||||
.await
|
||||
.unwrap(); //Okay, as trip_details can only be created with proper DB backing
|
||||
|
||||
if max_people == 0 {
|
||||
let rowers = TripWithUserAndType::from(db, trip.clone()).await.rower;
|
||||
if update.max_people == 0 {
|
||||
let rowers = TripWithUserAndType::from(db, update.trip.clone())
|
||||
.await
|
||||
.rower;
|
||||
for user in rowers {
|
||||
if let Some(user) = User::find_by_name(db, &user.name).await {
|
||||
let notes = match notes {
|
||||
let notes = match update.notes {
|
||||
Some(n) if !n.is_empty() => n,
|
||||
_ => ".",
|
||||
};
|
||||
@ -220,7 +226,10 @@ WHERE day=?
|
||||
&user,
|
||||
&format!(
|
||||
"Die Ausfahrt von {} am {} um {} wurde abgesagt{}",
|
||||
cox.user.name, trip.day, trip.planned_starting_time, notes
|
||||
update.cox.user.name,
|
||||
update.trip.day,
|
||||
update.trip.planned_starting_time,
|
||||
notes
|
||||
),
|
||||
"Absage Ausfahrt",
|
||||
None,
|
||||
@ -349,7 +358,7 @@ mod test {
|
||||
use crate::{
|
||||
model::{
|
||||
planned_event::PlannedEvent,
|
||||
trip::TripDeleteError,
|
||||
trip::{self, TripDeleteError},
|
||||
tripdetails::TripDetails,
|
||||
user::{CoxUser, User},
|
||||
usertrip::UserTrip,
|
||||
@ -434,11 +443,17 @@ mod test {
|
||||
|
||||
let trip = Trip::find_by_id(&pool, 1).await.unwrap();
|
||||
|
||||
assert!(
|
||||
Trip::update_own(&pool, &cox, &trip, 10, None, None, false, false)
|
||||
.await
|
||||
.is_ok()
|
||||
);
|
||||
let update = trip::TripUpdate {
|
||||
cox: &cox,
|
||||
trip: &trip,
|
||||
max_people: 10,
|
||||
notes: None,
|
||||
trip_type: None,
|
||||
always_show: false,
|
||||
is_locked: false,
|
||||
};
|
||||
|
||||
assert!(Trip::update_own(&pool, &update).await.is_ok());
|
||||
|
||||
let trip = Trip::find_by_id(&pool, 1).await.unwrap();
|
||||
assert_eq!(trip.max_people, 10);
|
||||
@ -457,11 +472,16 @@ mod test {
|
||||
|
||||
let trip = Trip::find_by_id(&pool, 1).await.unwrap();
|
||||
|
||||
assert!(
|
||||
Trip::update_own(&pool, &cox, &trip, 10, None, Some(1), false, false)
|
||||
.await
|
||||
.is_ok()
|
||||
);
|
||||
let update = trip::TripUpdate {
|
||||
cox: &cox,
|
||||
trip: &trip,
|
||||
max_people: 10,
|
||||
notes: None,
|
||||
trip_type: Some(1),
|
||||
always_show: false,
|
||||
is_locked: false,
|
||||
};
|
||||
assert!(Trip::update_own(&pool, &update).await.is_ok());
|
||||
|
||||
let trip = Trip::find_by_id(&pool, 1).await.unwrap();
|
||||
assert_eq!(trip.max_people, 10);
|
||||
@ -481,11 +501,16 @@ mod test {
|
||||
|
||||
let trip = Trip::find_by_id(&pool, 1).await.unwrap();
|
||||
|
||||
assert!(
|
||||
Trip::update_own(&pool, &cox, &trip, 10, None, None, false, false)
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
let update = trip::TripUpdate {
|
||||
cox: &cox,
|
||||
trip: &trip,
|
||||
max_people: 10,
|
||||
notes: None,
|
||||
trip_type: None,
|
||||
always_show: false,
|
||||
is_locked: false,
|
||||
};
|
||||
assert!(Trip::update_own(&pool, &update).await.is_err());
|
||||
assert_eq!(trip.max_people, 1);
|
||||
}
|
||||
|
||||
|
@ -80,7 +80,7 @@ pub enum LoginError {
|
||||
NotACox,
|
||||
NotATech,
|
||||
GuestNotAllowed,
|
||||
NoPasswordSet(User),
|
||||
NoPasswordSet(Box<User>),
|
||||
DeserializationError,
|
||||
}
|
||||
|
||||
@ -706,7 +706,7 @@ ORDER BY last_access DESC
|
||||
Err(LoginError::InvalidAuthenticationCombo)
|
||||
} else {
|
||||
info!("User {name} has no PW set");
|
||||
Err(LoginError::NoPasswordSet(user))
|
||||
Err(LoginError::NoPasswordSet(Box::new(user)))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -17,6 +17,17 @@ pub struct Waterlevel {
|
||||
pub tumittel: i64,
|
||||
}
|
||||
|
||||
pub struct Create {
|
||||
pub day: NaiveDate,
|
||||
pub time: String,
|
||||
pub max: i64,
|
||||
pub min: i64,
|
||||
pub mittel: i64,
|
||||
pub tumax: i64,
|
||||
pub tumin: i64,
|
||||
pub tumittel: i64,
|
||||
}
|
||||
|
||||
impl Waterlevel {
|
||||
pub async fn find_by_id(db: &SqlitePool, id: i32) -> Option<Self> {
|
||||
sqlx::query_as!(Self, "SELECT * FROM waterlevel WHERE id like ?", id)
|
||||
@ -31,20 +42,10 @@ impl Waterlevel {
|
||||
.ok()
|
||||
}
|
||||
|
||||
pub async fn create(
|
||||
db: &mut Transaction<'_, Sqlite>,
|
||||
day: NaiveDate,
|
||||
time: String,
|
||||
max: i64,
|
||||
min: i64,
|
||||
mittel: i64,
|
||||
tumax: i64,
|
||||
tumin: i64,
|
||||
tumittel: i64,
|
||||
) -> Result<(), String> {
|
||||
pub async fn create(db: &mut Transaction<'_, Sqlite>, create: &Create) -> Result<(), String> {
|
||||
sqlx::query!(
|
||||
"INSERT INTO waterlevel(day, time, max, min, mittel, tumax, tumin, tumittel) VALUES (?,?,?,?,?,?,?,?)",
|
||||
day, time, max, min, mittel, tumax, tumin, tumittel
|
||||
create.day, create.time, create.max, create.min, create.mittel, create.tumax, create.tumin, create.tumittel
|
||||
)
|
||||
.execute(db.deref_mut())
|
||||
.await
|
||||
|
Reference in New Issue
Block a user