Added setting for closing days.
This commit is contained in:
@@ -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(()))
|
||||
}
|
||||
Reference in New Issue
Block a user