hacky-ruadat/src/model/tripdetails.rs

119 lines
3.1 KiB
Rust
Raw Normal View History

2023-04-04 12:19:56 +02:00
use serde::{Deserialize, Serialize};
use sqlx::{FromRow, SqlitePool};
#[derive(FromRow, Debug, Serialize, Deserialize)]
pub struct TripDetails {
pub id: i64,
planned_starting_time: String,
2023-04-26 12:21:30 +02:00
max_people: i64,
2023-04-04 12:19:56 +02:00
day: String,
notes: Option<String>,
}
impl TripDetails {
2023-04-26 12:21:30 +02:00
pub async fn find_by_id(db: &SqlitePool, id: i64) -> Option<Self> {
sqlx::query_as!(
TripDetails,
"
SELECT id, planned_starting_time, max_people, day, notes
FROM trip_details
WHERE id like ?
",
id
)
.fetch_one(db)
.await
.ok()
}
2023-04-26 11:31:02 +02:00
/// Creates a new entry in `trip_details` and returns its id.
2023-04-04 19:49:27 +02:00
pub async fn create(
2023-04-04 12:19:56 +02:00
db: &SqlitePool,
planned_starting_time: String,
max_people: i32,
day: String,
notes: Option<String>,
) -> i64 {
let query = sqlx::query!(
"INSERT INTO trip_details(planned_starting_time, max_people, day, notes) VALUES(?, ?, ?, ?)" ,
planned_starting_time,
max_people,
day,
notes
)
.execute(db)
.await
2023-04-26 12:21:30 +02:00
.unwrap(); //Okay, TripDetails can only be created if self.id exists
2023-04-04 12:19:56 +02:00
query.last_insert_rowid()
}
2023-04-26 12:21:30 +02:00
pub async fn is_full(&self, db: &SqlitePool) -> bool {
let amount_currently_registered = sqlx::query!(
"SELECT COUNT(*) as count FROM user_trip WHERE trip_details_id = ?",
self.id
)
.fetch_one(db)
.await
.unwrap(); //TODO: fixme
let amount_currently_registered = i64::from(amount_currently_registered.count);
let amount_allowed_to_register =
sqlx::query!("SELECT max_people FROM trip_details WHERE id = ?", self.id)
.fetch_one(db)
.await
.unwrap(); //Okay, TripDetails can only be created if self.id exists
let amount_allowed_to_register = amount_allowed_to_register.max_people;
amount_currently_registered >= amount_allowed_to_register
}
2023-04-04 12:19:56 +02:00
}
2023-04-26 11:31:02 +02:00
#[cfg(test)]
mod test {
use crate::testdb;
use super::TripDetails;
use sqlx::SqlitePool;
#[sqlx::test]
2023-04-26 12:21:30 +02:00
fn test_find_true() {
let pool = testdb!();
assert!(TripDetails::find_by_id(&pool, 1).await.is_some());
}
#[sqlx::test]
fn test_find_false() {
let pool = testdb!();
assert!(TripDetails::find_by_id(&pool, 1337).await.is_none());
}
#[sqlx::test]
fn test_create() {
2023-04-26 11:31:02 +02:00
let pool = testdb!();
assert_eq!(
TripDetails::create(&pool, "10:00".into(), 2, "1970-01-01".into(), None).await,
2023-04-26 12:21:30 +02:00
2,
2023-04-26 11:31:02 +02:00
);
assert_eq!(
TripDetails::create(&pool, "10:00".into(), 2, "1970-01-01".into(), None).await,
2023-04-26 12:21:30 +02:00
3,
2023-04-26 11:31:02 +02:00
);
}
2023-04-26 12:21:30 +02:00
#[sqlx::test]
fn test_false_full() {
let pool = testdb!();
let trip_details = TripDetails::find_by_id(&pool, 1).await.unwrap();
assert_eq!(trip_details.is_full(&pool).await, false);
}
#[sqlx::test]
fn test_true_full() {
//TODO: register user for trip_details = 1; check if is_full returns true
}
2023-04-26 11:31:02 +02:00
}