Added setting for closing days.
This commit is contained in:
+26
-1
@@ -348,7 +348,8 @@ impl ResProperty {
|
||||
pub struct PublicFormData {
|
||||
pub property: ResProperty,
|
||||
pub hours: Vec<DayHour>,
|
||||
pub reservations: Vec<Reservation>
|
||||
pub reservations: Vec<Reservation>,
|
||||
pub closing_days: Option<ClosingTime>
|
||||
}
|
||||
|
||||
fn empty_slots() -> Vec<String> {
|
||||
@@ -637,3 +638,27 @@ impl Appearance {
|
||||
self.id
|
||||
}
|
||||
}
|
||||
|
||||
pub fn validate_closing_time(time: &ClosingTime) -> Result<(), ValidationError> {
|
||||
if time.from_date > time.to_date {
|
||||
let mut err = ValidationError::new("to date in past");
|
||||
err.message = Some("To date must be after from date".into());
|
||||
Err(err)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, Serialize, Deserialize, Validate, Eq, PartialEq)]
|
||||
#[cfg_attr(feature = "ssr", derive(sqlx::FromRow))]
|
||||
#[validate(schema(function = "validate_closing_time"))]
|
||||
pub struct ClosingTime {
|
||||
id: i32,
|
||||
pub from_date: NaiveDate,
|
||||
pub to_date: NaiveDate
|
||||
}
|
||||
impl ClosingTime {
|
||||
pub fn id(&self) -> i32 {
|
||||
self.id
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ use cfg_if::cfg_if;
|
||||
use chrono::Weekday;
|
||||
use leptos::*;
|
||||
use validator::Validate;
|
||||
use crate::backend::data::{ApiResponse, DayHour, WeekHours};
|
||||
use crate::backend::data::{ApiResponse, ClosingTime, DayHour, WeekHours};
|
||||
use crate::components::data_form::ForValidation;
|
||||
|
||||
cfg_if! { if #[cfg(feature = "ssr")] {
|
||||
@@ -89,4 +89,70 @@ impl ForValidation for UpdateHours {
|
||||
fn entity(&self) -> &dyn Validate {
|
||||
&self.hours
|
||||
}
|
||||
}
|
||||
|
||||
#[server]
|
||||
pub async fn get_closing_time() -> Result<Option<ClosingTime>, ServerFnError> {
|
||||
use crate::backend::get_pool;
|
||||
use sqlx::{Error, query_as};
|
||||
|
||||
let pool = get_pool().await?;
|
||||
let ct = query_as::<_, ClosingTime>("SELECT * FROM closing_time")
|
||||
.fetch_one(&pool)
|
||||
.await;
|
||||
|
||||
Ok(if let Err(ref e) = ct {
|
||||
if matches!(e, Error::RowNotFound) {
|
||||
None
|
||||
} else {
|
||||
Some(ct?)
|
||||
}
|
||||
} else {
|
||||
Some(ct?)
|
||||
})
|
||||
}
|
||||
|
||||
#[server]
|
||||
pub async fn update_closing_time(time: ClosingTime) -> Result<ApiResponse<()>, ServerFnError> {
|
||||
use crate::perm_check;
|
||||
use crate::backend::get_pool;
|
||||
|
||||
perm_check!(is_admin);
|
||||
|
||||
let pool = get_pool().await?;
|
||||
let mut tx = pool.begin().await?;
|
||||
|
||||
sqlx::query("DELETE FROM closing_time")
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
sqlx::query("INSERT INTO closing_time(from_date, to_date) VALUES($1, $2)")
|
||||
.bind(time.from_date)
|
||||
.bind(time.to_date)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
|
||||
tx.commit().await?;
|
||||
|
||||
Ok(ApiResponse::Data(()))
|
||||
}
|
||||
|
||||
impl ForValidation for UpdateClosingTime {
|
||||
fn entity(&self) -> &dyn Validate {
|
||||
&self.time
|
||||
}
|
||||
}
|
||||
|
||||
#[server]
|
||||
pub async fn delete_closing_time() -> Result<ApiResponse<()>, ServerFnError> {
|
||||
use crate::perm_check;
|
||||
use crate::backend::get_pool;
|
||||
|
||||
perm_check!(is_admin);
|
||||
|
||||
let pool = get_pool().await?;
|
||||
sqlx::query("DELETE FROM closing_time")
|
||||
.execute(&pool)
|
||||
.await?;
|
||||
|
||||
Ok(ApiResponse::Data(()))
|
||||
}
|
||||
@@ -26,6 +26,7 @@ cfg_if! { if #[cfg(feature = "ssr")] {
|
||||
use crate::backend::user::emails_for_notify;
|
||||
use crate::locales::trl;
|
||||
use rust_decimal::prelude::ToPrimitive;
|
||||
use crate::backend::opening_hours::get_closing_time;
|
||||
|
||||
async fn find_sum_by_uuid(uuid: &Uuid, tx: &mut Transaction<'_, Postgres>) -> Result<ReservationSum, Error> {
|
||||
let reservation = query_as::<_, ReservationSum>("SELECT * FROM reservation_sum WHERE uuid = $1")
|
||||
@@ -240,13 +241,15 @@ pub async fn get_public_form_data(day: NaiveDate) -> Result<ApiResponse<Vec<Publ
|
||||
let hours = hours_for_day(day.weekday()).await?;
|
||||
let props = get_props(Some("active = true")).await?;
|
||||
let reservations = reservations_for_day(&day).await?;
|
||||
let closing_days = get_closing_time().await?;
|
||||
|
||||
info!("Loading public form data");
|
||||
|
||||
Ok(ApiResponse::Data(props.into_iter().map(|p| PublicFormData {
|
||||
property: p,
|
||||
hours: hours.clone(),
|
||||
reservations: reservations.clone()
|
||||
reservations: reservations.clone(),
|
||||
closing_days: closing_days.clone()
|
||||
}).collect::<Vec<_>>()))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user