rowt/src/model/boat.rs

233 lines
6.2 KiB
Rust
Raw Normal View History

use serde::{Deserialize, Serialize};
use sqlx::{FromRow, SqlitePool};
#[derive(FromRow, Debug, Serialize, Deserialize)]
pub struct Boat {
pub id: i64,
pub name: String,
pub amount_seats: i64,
pub location_id: i64,
pub owner: Option<i64>,
pub year_built: Option<i64>,
pub boatbuilder: Option<String>,
#[serde(default = "bool::default")]
default_shipmaster_only_steering: bool,
#[serde(default = "bool::default")]
skull: bool,
#[serde(default = "bool::default")]
external: bool,
}
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 ?
",
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 all(db: &SqlitePool) -> Vec<Self> {
sqlx::query_as!(
Boat,
"
SELECT id, name, amount_seats, location_id, owner, year_built, boatbuilder, default_shipmaster_only_steering, skull, external
FROM boat
ORDER BY amount_seats DESC
"
)
.fetch_all(db)
.await
.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,
) -> bool {
sqlx::query!(
"INSERT INTO boat(name, amount_seats, year_built, boatbuilder, default_shipmaster_only_steering, skull, external) VALUES (?,?,?,?,?,?,?)",
name,
amount_seats,
year_built,
boatbuilder,
default_shipmaster_only_steering,
skull,
external
)
.execute(db)
.await
.is_ok()
}
// pub async fn update(&self, db: &SqlitePool, is_cox: bool, is_admin: bool, is_guest: bool) {
// sqlx::query!(
// "UPDATE user SET is_cox = ?, is_admin = ?, is_guest = ? where id = ?",
// is_cox,
// is_admin,
// is_guest,
// self.id
// )
// .execute(db)
// .await
// .unwrap(); //Okay, because we can only create a User of a valid id
// }
//
pub async fn delete(&self, db: &SqlitePool) {
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
}
}
//#[cfg(test)]
//mod test {
// use crate::testdb;
//
// use super::User;
// use sqlx::SqlitePool;
//
// #[sqlx::test]
// fn test_find_correct_id() {
// let pool = testdb!();
// let user = User::find_by_id(&pool, 1).await.unwrap();
// assert_eq!(user.id, 1);
// }
//
// #[sqlx::test]
// fn test_find_wrong_id() {
// let pool = testdb!();
// let user = User::find_by_id(&pool, 1337).await;
// assert!(user.is_none());
// }
//
// #[sqlx::test]
// fn test_find_correct_name() {
// let pool = testdb!();
// let user = User::find_by_name(&pool, "admin".into()).await.unwrap();
// assert_eq!(user.id, 1);
// }
//
// #[sqlx::test]
// fn test_find_wrong_name() {
// let pool = testdb!();
// let user = User::find_by_name(&pool, "name-does-not-exist".into()).await;
// assert!(user.is_none());
// }
//
// #[sqlx::test]
// fn test_all() {
// let pool = testdb!();
// let res = User::all(&pool).await;
// assert!(res.len() > 3);
// }
//
// #[sqlx::test]
// fn test_succ_create() {
// let pool = testdb!();
//
// assert_eq!(
// User::create(&pool, "new-user-name".into(), false).await,
// true
// );
// }
//
// #[sqlx::test]
// fn test_duplicate_name_create() {
// let pool = testdb!();
//
// assert_eq!(User::create(&pool, "admin".into(), false).await, false);
// }
//
// #[sqlx::test]
// fn test_update() {
// let pool = testdb!();
//
// let user = User::find_by_id(&pool, 1).await.unwrap();
// user.update(&pool, false, false, false).await;
//
// let user = User::find_by_id(&pool, 1).await.unwrap();
// assert_eq!(user.is_admin, false);
// }
//
// #[sqlx::test]
// fn succ_login_with_test_db() {
// let pool = testdb!();
// User::login(&pool, "admin".into(), "admin".into())
// .await
// .unwrap();
// }
//
// #[sqlx::test]
// fn wrong_pw() {
// let pool = testdb!();
// assert!(User::login(&pool, "admin".into(), "admi".into())
// .await
// .is_err());
// }
//
// #[sqlx::test]
// fn wrong_username() {
// let pool = testdb!();
// assert!(User::login(&pool, "admi".into(), "admin".into())
// .await
// .is_err());
// }
//
// #[sqlx::test]
// fn reset() {
// let pool = testdb!();
// let user = User::find_by_id(&pool, 1).await.unwrap();
//
// user.reset_pw(&pool).await;
//
// let user = User::find_by_id(&pool, 1).await.unwrap();
// assert_eq!(user.pw, None);
// }
//
// #[sqlx::test]
// fn update_pw() {
// let pool = testdb!();
// let user = User::find_by_id(&pool, 1).await.unwrap();
//
// assert!(User::login(&pool, "admin".into(), "abc".into())
// .await
// .is_err());
//
// user.update_pw(&pool, "abc".into()).await;
//
// User::login(&pool, "admin".into(), "abc".into())
// .await
// .unwrap();
// }
//}