use query_as instead of manual mapping
This commit is contained in:
parent
dc4b4b3499
commit
32e6148844
@ -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,
|
||||
|
@ -1,4 +1,3 @@
|
||||
use rocket::form::FromFormField;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::{FromRow, SqlitePool};
|
||||
|
||||
|
@ -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<User> {
|
||||
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) {
|
||||
|
@ -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<Self, Box<dyn std::error::Error + Send + Sync>> {
|
||||
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<MyNaiveDateTime>,
|
||||
pub last_access: Option<chrono::NaiveDateTime>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
@ -87,7 +64,8 @@ impl User {
|
||||
}
|
||||
|
||||
pub async fn find_by_id(db: &SqlitePool, id: i32) -> Option<Self> {
|
||||
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<Self> {
|
||||
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<Self> {
|
||||
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<Self> {
|
||||
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 {
|
||||
|
Loading…
Reference in New Issue
Block a user