even more tests!
This commit is contained in:
@ -32,6 +32,21 @@ pub struct Registration {
|
||||
}
|
||||
|
||||
impl PlannedEvent {
|
||||
pub async fn find_by_id(db: &SqlitePool, id: i64) -> Option<Self> {
|
||||
sqlx::query_as!(
|
||||
Self,
|
||||
"
|
||||
SELECT planned_event.id, name, planned_amount_cox, allow_guests, trip_details_id, planned_starting_time, max_people, day, notes
|
||||
FROM planned_event
|
||||
INNER JOIN trip_details ON planned_event.trip_details_id = trip_details.id
|
||||
WHERE planned_event.id like ?
|
||||
",
|
||||
id
|
||||
)
|
||||
.fetch_one(db)
|
||||
.await
|
||||
.ok()
|
||||
}
|
||||
pub async fn get_for_day(db: &SqlitePool, day: NaiveDate) -> Vec<PlannedEventWithUser> {
|
||||
let day = format!("{day}");
|
||||
let events = sqlx::query_as!(
|
||||
@ -48,53 +63,31 @@ WHERE day=?",
|
||||
|
||||
let mut ret = Vec::new();
|
||||
for event in events {
|
||||
let cox = Self::get_all_cox_for_id(db, event.id).await;
|
||||
let cox = event.get_all_cox(db).await;
|
||||
ret.push(PlannedEventWithUser {
|
||||
planned_event: event.clone(),
|
||||
cox_needed: event.planned_amount_cox > cox.len() as i64,
|
||||
cox,
|
||||
rower: Self::get_all_rower_for_id(db, event.id).await,
|
||||
rower: event.get_all_rower(db).await,
|
||||
});
|
||||
}
|
||||
ret
|
||||
}
|
||||
|
||||
pub async fn is_full(db: &SqlitePool, trip_details_id: i64) -> bool {
|
||||
let amount_currently_registered = sqlx::query!(
|
||||
"SELECT COUNT(*) as count FROM user_trip WHERE trip_details_id = ?",
|
||||
trip_details_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 = ?",
|
||||
trip_details_id
|
||||
)
|
||||
.fetch_one(db)
|
||||
.await
|
||||
.unwrap(); //TODO: fixme
|
||||
let amount_allowed_to_register = amount_allowed_to_register.max_people;
|
||||
|
||||
amount_currently_registered >= amount_allowed_to_register
|
||||
}
|
||||
|
||||
async fn get_all_cox_for_id(db: &SqlitePool, id: i64) -> Vec<Registration> {
|
||||
async fn get_all_cox(&self, db: &SqlitePool) -> Vec<Registration> {
|
||||
sqlx::query_as!(
|
||||
Registration,
|
||||
"
|
||||
SELECT (SELECT name FROM user WHERE cox_id = id) as name, (SELECT created_at FROM user WHERE cox_id = id) as registered_at FROM trip WHERE planned_event_id = ?
|
||||
",
|
||||
id
|
||||
self.id
|
||||
)
|
||||
.fetch_all(db)
|
||||
.await
|
||||
.unwrap() //TODO: fixme
|
||||
.unwrap() //Okay, as PlannedEvent can only be created with proper DB backing
|
||||
}
|
||||
|
||||
async fn get_all_rower_for_id(db: &SqlitePool, id: i64) -> Vec<Registration> {
|
||||
async fn get_all_rower(&self, db: &SqlitePool) -> Vec<Registration> {
|
||||
sqlx::query_as!(
|
||||
Registration,
|
||||
"
|
||||
@ -103,11 +96,11 @@ SELECT
|
||||
(SELECT created_at FROM user WHERE user_trip.user_id = user.id) as registered_at
|
||||
FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM planned_event WHERE id = ?)
|
||||
",
|
||||
id
|
||||
self.id
|
||||
)
|
||||
.fetch_all(db)
|
||||
.await
|
||||
.unwrap() //TODO: fixme
|
||||
.unwrap() //Okay, as PlannedEvent can only be created with proper DB backing
|
||||
}
|
||||
|
||||
pub async fn create(
|
||||
@ -126,10 +119,51 @@ FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM planned_even
|
||||
.unwrap(); //TODO: fixme
|
||||
}
|
||||
|
||||
pub async fn delete(db: &SqlitePool, id: i64) {
|
||||
sqlx::query!("DELETE FROM planned_event WHERE id = ?", id)
|
||||
pub async fn delete(&self, db: &SqlitePool) {
|
||||
sqlx::query!("DELETE FROM planned_event WHERE id = ?", self.id)
|
||||
.execute(db)
|
||||
.await
|
||||
.unwrap(); //TODO: fixme
|
||||
.unwrap(); //Okay, as PlannedEvent can only be created with proper DB backing
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::testdb;
|
||||
|
||||
use super::PlannedEvent;
|
||||
use chrono::NaiveDate;
|
||||
use sqlx::SqlitePool;
|
||||
|
||||
#[sqlx::test]
|
||||
fn test_get_day() {
|
||||
let pool = testdb!();
|
||||
|
||||
let res =
|
||||
PlannedEvent::get_for_day(&pool, NaiveDate::from_ymd_opt(1970, 1, 1).unwrap()).await;
|
||||
assert_eq!(res.len(), 1);
|
||||
}
|
||||
|
||||
#[sqlx::test]
|
||||
fn test_create() {
|
||||
let pool = testdb!();
|
||||
|
||||
PlannedEvent::create(&pool, "new-event".into(), 2, false, 1).await;
|
||||
|
||||
let res =
|
||||
PlannedEvent::get_for_day(&pool, NaiveDate::from_ymd_opt(1970, 1, 1).unwrap()).await;
|
||||
assert_eq!(res.len(), 2);
|
||||
}
|
||||
|
||||
#[sqlx::test]
|
||||
fn test_delete() {
|
||||
let pool = testdb!();
|
||||
let planned_event = PlannedEvent::find_by_id(&pool, 1).await.unwrap();
|
||||
|
||||
planned_event.delete(&pool).await;
|
||||
|
||||
let res =
|
||||
PlannedEvent::get_for_day(&pool, NaiveDate::from_ymd_opt(1970, 1, 1).unwrap()).await;
|
||||
assert_eq!(res.len(), 0);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user