Merge branch 'allow-reservation-edits' into staging
# Conflicts: # src/tera/boatreservation.rs
This commit is contained in:
@ -1,3 +1,5 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::model::{boat::Boat, user::User};
|
||||
use crate::tera::boatreservation::ReservationEditForm;
|
||||
use chrono::NaiveDate;
|
||||
@ -25,7 +27,7 @@ pub struct BoatReservation {
|
||||
#[derive(FromRow, Debug, Serialize, Deserialize)]
|
||||
pub struct BoatReservationWithDetails {
|
||||
#[serde(flatten)]
|
||||
boat_reservation: BoatReservation,
|
||||
reservation: BoatReservation,
|
||||
boat: Boat,
|
||||
user_applicant: User,
|
||||
user_confirmation: Option<User>,
|
||||
@ -85,7 +87,7 @@ WHERE end_date >= CURRENT_DATE ORDER BY end_date
|
||||
.unwrap();
|
||||
|
||||
res.push(BoatReservationWithDetails {
|
||||
boat_reservation: reservation,
|
||||
reservation,
|
||||
boat,
|
||||
user_applicant,
|
||||
user_confirmation,
|
||||
@ -93,6 +95,31 @@ WHERE end_date >= CURRENT_DATE ORDER BY end_date
|
||||
}
|
||||
res
|
||||
}
|
||||
pub async fn all_future_with_groups(
|
||||
db: &SqlitePool,
|
||||
) -> HashMap<String, Vec<BoatReservationWithDetails>> {
|
||||
let mut grouped_reservations: HashMap<String, Vec<BoatReservationWithDetails>> =
|
||||
HashMap::new();
|
||||
|
||||
let reservations = Self::all_future(db).await;
|
||||
for reservation in reservations {
|
||||
let key = format!(
|
||||
"{}-{}-{}-{}-{}",
|
||||
reservation.reservation.start_date,
|
||||
reservation.reservation.end_date,
|
||||
reservation.reservation.time_desc,
|
||||
reservation.reservation.usage,
|
||||
reservation.user_applicant.name
|
||||
);
|
||||
|
||||
grouped_reservations
|
||||
.entry(key)
|
||||
.or_insert_with(Vec::new)
|
||||
.push(reservation);
|
||||
}
|
||||
|
||||
grouped_reservations
|
||||
}
|
||||
|
||||
pub async fn create(
|
||||
db: &SqlitePool,
|
||||
|
@ -26,7 +26,7 @@ const REGULAR: i32 = 22000;
|
||||
const UNTERSTUETZEND: i32 = 2500;
|
||||
const FOERDERND: i32 = 8500;
|
||||
|
||||
#[derive(FromRow, Serialize, Deserialize, Clone, Debug)]
|
||||
#[derive(FromRow, Serialize, Deserialize, Clone, Debug, Eq, Hash)]
|
||||
pub struct User {
|
||||
pub id: i64,
|
||||
pub name: String,
|
||||
@ -47,16 +47,18 @@ pub struct User {
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct UserWithRoles {
|
||||
pub struct UserWithRolesAndNotificationCount {
|
||||
#[serde(flatten)]
|
||||
pub user: User,
|
||||
pub amount_unread_notifications: i32,
|
||||
pub roles: Vec<String>,
|
||||
}
|
||||
|
||||
impl UserWithRoles {
|
||||
impl UserWithRolesAndNotificationCount {
|
||||
pub async fn from_user(user: User, db: &SqlitePool) -> Self {
|
||||
Self {
|
||||
roles: user.roles(db).await,
|
||||
amount_unread_notifications: user.amount_unread_notifications(db).await,
|
||||
user,
|
||||
}
|
||||
}
|
||||
@ -237,6 +239,17 @@ impl User {
|
||||
.count
|
||||
}
|
||||
|
||||
pub async fn amount_unread_notifications(&self, db: &SqlitePool) -> i32 {
|
||||
sqlx::query!(
|
||||
"SELECT COUNT(*) as count FROM notification WHERE user_id = ? AND read_at IS NULL",
|
||||
self.id
|
||||
)
|
||||
.fetch_one(db)
|
||||
.await
|
||||
.unwrap()
|
||||
.count
|
||||
}
|
||||
|
||||
pub async fn has_role(&self, db: &SqlitePool, role: &str) -> bool {
|
||||
if sqlx::query!(
|
||||
"SELECT * FROM user_role WHERE user_id=? AND role_id = (SELECT id FROM role WHERE name = ?)",
|
||||
@ -977,8 +990,8 @@ pub struct UserWithRolesAndMembershipPdf {
|
||||
impl UserWithRolesAndMembershipPdf {
|
||||
pub(crate) async fn from_user(db: &SqlitePool, user: User) -> Self {
|
||||
let membership_pdf: bool =
|
||||
sqlx::query_scalar!("SELECT membership_pdf FROM user WHERE id = $1", user.id)
|
||||
.fetch_optional(db)
|
||||
sqlx::query_scalar!("SELECT membership_pdf FROM user WHERE id = ?", user.id)
|
||||
.fetch_one(db)
|
||||
.await
|
||||
.unwrap()
|
||||
.is_some();
|
||||
|
Reference in New Issue
Block a user