This commit is contained in:
philipp 2023-05-03 13:32:23 +02:00
parent 7981751f18
commit 913d0ac78e
7 changed files with 40 additions and 33 deletions

4
Cargo.lock generated
View File

@ -1655,9 +1655,9 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]] [[package]]
name = "pkg-config" name = "pkg-config"
version = "0.3.26" version = "0.3.27"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ac9a59f73473f1b8d852421e59e64809f025994837ef743615c6d0c5b305160" checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964"
[[package]] [[package]]
name = "polyval" name = "polyval"

View File

@ -13,6 +13,7 @@
- [] Allow sign-outs only >2h before event - [] Allow sign-outs only >2h before event
- [] add `always_show` to `planned_trips` (e.g. for wanderfahrten) - [] add `always_show` to `planned_trips` (e.g. for wanderfahrten)
- [] `planned_events` -> ICS feed to be [integrated](https://icscalendar.com/shortcode-overview/) in homepage calendar - [] `planned_events` -> ICS feed to be [integrated](https://icscalendar.com/shortcode-overview/) in homepage calendar
- [] Notification system (-> signal msgs?) e.g. if new event; somebody unregistered
# Nice to have # Nice to have
## Frontend ## Frontend

View File

@ -2,7 +2,7 @@ use chrono::NaiveDate;
use serde::Serialize; use serde::Serialize;
use sqlx::{FromRow, SqlitePool}; use sqlx::{FromRow, SqlitePool};
use super::{tripdetails::TripDetails, triptype::TripType}; use super::{tripdetails::TripDetails, triptype::TripType, user::User};
#[derive(Serialize, Clone, FromRow)] #[derive(Serialize, Clone, FromRow)]
pub struct PlannedEvent { pub struct PlannedEvent {
@ -90,6 +90,7 @@ WHERE day=?",
} }
async fn get_all_cox(&self, db: &SqlitePool) -> Vec<Registration> { async fn get_all_cox(&self, db: &SqlitePool) -> Vec<Registration> {
//TODO: switch to join
sqlx::query_as!( sqlx::query_as!(
Registration, Registration,
" "
@ -107,6 +108,7 @@ FROM trip WHERE planned_event_id = ?
} }
async fn get_all_rower(&self, db: &SqlitePool) -> Vec<Registration> { async fn get_all_rower(&self, db: &SqlitePool) -> Vec<Registration> {
//TODO: switch to join
sqlx::query_as!( sqlx::query_as!(
Registration, Registration,
" "
@ -123,6 +125,23 @@ FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM planned_even
.unwrap() //Okay, as PlannedEvent can only be created with proper DB backing .unwrap() //Okay, as PlannedEvent can only be created with proper DB backing
} }
//TODO: add tests
pub async fn is_rower_registered(&self, db: &SqlitePool, user: &User) -> bool {
let is_rower = sqlx::query!(
"SELECT count(*) as amount
FROM user_trip
WHERE trip_details_id =
(SELECT trip_details_id FROM planned_event WHERE id = ?)
AND user_id = ?",
self.id,
user.id
)
.fetch_one(db)
.await
.unwrap(); //Okay, bc planned_event can only be created with proper DB backing
is_rower.amount > 0
}
pub async fn create( pub async fn create(
db: &SqlitePool, db: &SqlitePool,
name: String, name: String,

View File

@ -67,19 +67,7 @@ WHERE trip.id=?
cox: &CoxUser, cox: &CoxUser,
planned_event: &PlannedEvent, planned_event: &PlannedEvent,
) -> Result<(), CoxHelpError> { ) -> Result<(), CoxHelpError> {
let is_rower = sqlx::query!( if planned_event.is_rower_registered(db, &cox).await {
"SELECT count(*) as amount
FROM user_trip
WHERE trip_details_id =
(SELECT trip_details_id FROM planned_event WHERE id = ?)
AND user_id = ?",
planned_event.id,
cox.id
)
.fetch_one(db)
.await
.unwrap(); //Okay, bc planned_event can only be created with proper DB backing
if is_rower.amount > 0 {
return Err(CoxHelpError::AlreadyRegisteredAsRower); return Err(CoxHelpError::AlreadyRegisteredAsRower);
} }
@ -112,6 +100,7 @@ WHERE day=?
.fetch_all(db) .fetch_all(db)
.await .await
.unwrap(); //TODO: fixme .unwrap(); //TODO: fixme
let mut ret = Vec::new(); let mut ret = Vec::new();
for trip in trips { for trip in trips {
let mut trip_type = None; let mut trip_type = None;

View File

@ -63,14 +63,7 @@ WHERE id like ?
.unwrap(); //TODO: fixme .unwrap(); //TODO: fixme
let amount_currently_registered = i64::from(amount_currently_registered.count); let amount_currently_registered = i64::from(amount_currently_registered.count);
let amount_allowed_to_register = amount_currently_registered >= self.max_people
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
} }
} }

View File

@ -34,6 +34,7 @@ impl UserTrip {
return Err(UserTripError::CantRegisterAtOwnEvent); return Err(UserTripError::CantRegisterAtOwnEvent);
} }
//TODO: can probably move to trip.rs?
//check if cox if planned_event //check if cox if planned_event
let is_cox = sqlx::query!( let is_cox = sqlx::query!(
"SELECT count(*) as amount "SELECT count(*) as amount

View File

@ -23,26 +23,30 @@ mod admin;
mod auth; mod auth;
mod cox; mod cox;
fn amount_days_to_show(is_cox: bool) -> i64 {
if is_cox {
let end_of_year = NaiveDate::from_ymd_opt(Local::now().year(), 12, 31).unwrap();
end_of_year
.signed_duration_since(Local::now().date_naive())
.num_days()
+ 1
} else {
6
}
}
#[get("/")] #[get("/")]
async fn index(db: &State<SqlitePool>, user: User, flash: Option<FlashMessage<'_>>) -> Template { async fn index(db: &State<SqlitePool>, user: User, flash: Option<FlashMessage<'_>>) -> Template {
let mut days = Vec::new(); let mut days = Vec::new();
let mut context = Context::new(); let mut context = Context::new();
let mut show_next_n_days = 6;
if user.is_cox {
let end_of_year = NaiveDate::from_ymd_opt(Local::now().year(), 12, 31).unwrap();
show_next_n_days = end_of_year
.signed_duration_since(Local::now().date_naive())
.num_days()
+ 1;
}
if user.is_cox || user.is_admin { if user.is_cox || user.is_admin {
let triptypes = TripType::all(db).await; let triptypes = TripType::all(db).await;
context.insert("trip_types", &triptypes); context.insert("trip_types", &triptypes);
} }
let show_next_n_days = amount_days_to_show(user.is_cox);
for i in 0..show_next_n_days { for i in 0..show_next_n_days {
let date = (Local::now() + Duration::days(i)).date_naive(); let date = (Local::now() + Duration::days(i)).date_naive();