diff --git a/src/model/boat.rs b/src/model/boat.rs index 6808650..8ec7ff2 100644 --- a/src/model/boat.rs +++ b/src/model/boat.rs @@ -172,10 +172,7 @@ ORDER BY amount_seats DESC #[cfg(test)] mod test { use crate::{ - model::{ - boat::{Boat, BoatToAdd}, - location::Location, - }, + model::boat::{Boat, BoatToAdd}, testdb, }; @@ -333,7 +330,6 @@ mod test { fn test_succ_update() { let pool = testdb!(); let boat = Boat::find_by_id(&pool, 1).await.unwrap(); - let location = Location::find_by_id(&pool, 1).await.unwrap(); let update = BoatToUpdate { name: "my-new-boat-name", amount_seats: 3, @@ -356,7 +352,6 @@ mod test { fn test_failed_update() { let pool = testdb!(); let boat = Boat::find_by_id(&pool, 1).await.unwrap(); - let location = Location::find_by_id(&pool, 1).await.unwrap(); let update = BoatToUpdate { name: "my-new-boat-name", amount_seats: 3, diff --git a/src/model/location.rs b/src/model/location.rs index 00ebcd7..a66d810 100644 --- a/src/model/location.rs +++ b/src/model/location.rs @@ -1,4 +1,3 @@ -use rocket::form::FromFormField; use serde::{Deserialize, Serialize}; use sqlx::{FromRow, SqlitePool}; diff --git a/src/model/rower.rs b/src/model/rower.rs index f5048cc..333acf3 100644 --- a/src/model/rower.rs +++ b/src/model/rower.rs @@ -1,10 +1,7 @@ use serde::{Deserialize, Serialize}; use sqlx::{FromRow, Sqlite, SqlitePool, Transaction}; -use super::{ - logbook::Logbook, - user::{MyNaiveDateTime, User}, -}; +use super::{logbook::Logbook, user::User}; #[derive(FromRow, Debug, Serialize, Deserialize)] pub struct Rower { @@ -14,7 +11,8 @@ pub struct Rower { impl Rower { pub async fn for_log(db: &SqlitePool, log: &Logbook) -> Vec { - sqlx::query!( + sqlx::query_as!( + User, " SELECT id, name, pw, is_cox, is_admin, is_guest, deleted, last_access FROM user @@ -25,18 +23,6 @@ WHERE id in (SELECT rower_id FROM rower WHERE logbook_id=?) .fetch_all(db) .await .unwrap() - .into_iter() - .map(|row| User { - id: row.id, - name: row.name, - pw: row.pw, - is_cox: row.is_cox, - is_admin: row.is_admin, - is_guest: row.is_guest, - deleted: row.deleted, - last_access: row.last_access.map(MyNaiveDateTime), - }) - .collect() } pub async fn create(db: &mut Transaction<'_, Sqlite>, logbook_id: i64, rower_id: i64) { diff --git a/src/model/user.rs b/src/model/user.rs index 0394e37..c6233ae 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -1,44 +1,21 @@ use std::ops::Deref; use argon2::{password_hash::SaltString, Argon2, PasswordHasher}; -use chrono::{Datelike, Local, NaiveDate, NaiveDateTime}; +use chrono::{Datelike, Local, NaiveDate}; use log::info; use rocket::{ async_trait, - form::{self, FromFormField, ValueField}, http::{Cookie, Status}, request::{self, FromRequest, Outcome}, time::{Duration, OffsetDateTime}, - FromForm, Request, + Request, }; use serde::{Deserialize, Serialize}; -use sqlx::{sqlite::SqliteValueRef, Decode, FromRow, SqlitePool}; +use sqlx::{FromRow, SqlitePool}; use super::{log::Log, tripdetails::TripDetails, Day}; -#[derive(Debug, Serialize, Deserialize)] -pub struct MyNaiveDateTime(pub chrono::NaiveDateTime); - -impl<'r> Decode<'r, sqlx::Sqlite> for MyNaiveDateTime { - fn decode(value: SqliteValueRef<'r>) -> Result> { - let dt: NaiveDateTime = Decode::decode(value)?; - Ok(MyNaiveDateTime(dt)) - } -} - -#[rocket::async_trait] -impl<'r> FromFormField<'r> for MyNaiveDateTime { - fn from_value(field: ValueField<'r>) -> form::Result<'r, Self> { - let dt = NaiveDateTime::parse_from_str(field.value, "%Y-%m-%d %H:%M:%S"); - - match dt { - Ok(parsed_dt) => Ok(MyNaiveDateTime(parsed_dt)), - Err(_) => Err(rocket::form::Error::validation("Invalid date/time format.").into()), - } - } -} - -#[derive(FromRow, Debug, Serialize, Deserialize, FromForm)] +#[derive(FromRow, Debug, Serialize, Deserialize)] pub struct User { pub id: i64, pub name: String, @@ -47,7 +24,7 @@ pub struct User { pub is_admin: bool, pub is_guest: bool, pub deleted: bool, - pub last_access: Option, + pub last_access: Option, } #[derive(Debug)] @@ -87,7 +64,8 @@ impl User { } pub async fn find_by_id(db: &SqlitePool, id: i32) -> Option { - let row = sqlx::query!( + sqlx::query_as!( + Self, " SELECT id, name, pw, is_cox, is_admin, is_guest, deleted, last_access FROM user @@ -97,22 +75,12 @@ WHERE id like ? ) .fetch_one(db) .await - .ok()?; - - Some(User { - id: row.id, - name: row.name, - pw: row.pw, - is_cox: row.is_cox, - is_admin: row.is_admin, - is_guest: row.is_guest, - deleted: row.deleted, - last_access: row.last_access.map(MyNaiveDateTime), - }) + .ok() } pub async fn find_by_name(db: &SqlitePool, name: &str) -> Option { - let row = sqlx::query!( + sqlx::query_as!( + Self, " SELECT id, name, pw, is_cox, is_admin, is_guest, deleted, last_access FROM user @@ -122,18 +90,7 @@ WHERE name like ? ) .fetch_one(db) .await - .ok()?; - - Some(User { - id: row.id, - name: row.name, - pw: row.pw, - is_cox: row.is_cox, - is_admin: row.is_admin, - is_guest: row.is_guest, - deleted: row.deleted, - last_access: row.last_access.map(MyNaiveDateTime), - }) + .ok() } pub async fn on_water(&self, db: &SqlitePool) -> bool { @@ -164,7 +121,8 @@ WHERE name like ? } pub async fn all(db: &SqlitePool) -> Vec { - sqlx::query!( + sqlx::query_as!( + Self, " SELECT id, name, pw, is_cox, is_admin, is_guest, deleted, last_access FROM user @@ -175,22 +133,11 @@ ORDER BY last_access DESC .fetch_all(db) .await .unwrap() - .into_iter() - .map(|row| User { - id: row.id, - name: row.name, - pw: row.pw, - is_cox: row.is_cox, - is_admin: row.is_admin, - is_guest: row.is_guest, - deleted: row.deleted, - last_access: row.last_access.map(MyNaiveDateTime), - }) - .collect() } pub async fn cox(db: &SqlitePool) -> Vec { - sqlx::query!( + sqlx::query_as!( + Self, " SELECT id, name, pw, is_cox, is_admin, is_guest, deleted, last_access FROM user @@ -201,18 +148,6 @@ ORDER BY last_access DESC .fetch_all(db) .await .unwrap() - .into_iter() - .map(|row| User { - id: row.id, - name: row.name, - pw: row.pw, - is_cox: row.is_cox, - is_admin: row.is_admin, - is_guest: row.is_guest, - deleted: row.deleted, - last_access: row.last_access.map(MyNaiveDateTime), - }) - .collect() } pub async fn create(db: &SqlitePool, name: &str, is_guest: bool) -> bool {