add full CRUD for boats

This commit is contained in:
2023-07-22 15:34:42 +02:00
parent c0bb6d51de
commit 524d1acee2
8 changed files with 277 additions and 171 deletions

View File

@ -88,19 +88,37 @@ ORDER BY amount_seats DESC
.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 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 {
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,
self.id
)
.execute(db)
.await
.is_ok()
}
pub async fn delete(&self, db: &SqlitePool) {
sqlx::query!("DELETE FROM boat WHERE id=?", self.id)
.execute(db)
@ -109,124 +127,70 @@ ORDER BY amount_seats DESC
}
}
//#[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();
// }
//}
#[cfg(test)]
mod test {
use crate::{model::boat::Boat, testdb};
use sqlx::SqlitePool;
#[sqlx::test]
fn test_find_correct_id() {
let pool = testdb!();
let boat = Boat::find_by_id(&pool, 1).await.unwrap();
assert_eq!(boat.id, 1);
}
#[sqlx::test]
fn test_find_wrong_id() {
let pool = testdb!();
let boat = Boat::find_by_id(&pool, 1337).await;
assert!(boat.is_none());
}
#[sqlx::test]
fn test_all() {
let pool = testdb!();
let res = Boat::all(&pool).await;
assert!(res.len() > 3);
}
#[sqlx::test]
fn test_succ_create() {
let pool = testdb!();
assert_eq!(
Boat::create(
&pool,
"new-boat-name".into(),
42,
None,
"Best Boatbuilder".into(),
true,
true,
false
)
.await,
true
);
}
#[sqlx::test]
fn test_duplicate_name_create() {
let pool = testdb!();
assert_eq!(
Boat::create(
&pool,
"Haichenbach".into(),
42,
None,
"Best Boatbuilder".into(),
true,
true,
false
)
.await,
false
);
}
}