Implemented settings for properties of reservation. Fixed bugs on empty database.
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
use leptos::*;
|
||||
use validator::Validate;
|
||||
use crate::backend::data::{ApiResponse, ResProperty};
|
||||
use crate::components::data_form::ForValidation;
|
||||
|
||||
#[server]
|
||||
pub async fn get_properties() -> Result<ApiResponse<Vec<ResProperty>>, ServerFnError> {
|
||||
use crate::backend::get_pool;
|
||||
|
||||
let pool = get_pool().await?;
|
||||
let props = sqlx::query_as::<_, ResProperty>("SELECT * FROM property").fetch_all(&pool).await?;
|
||||
|
||||
Ok(ApiResponse::Data(props))
|
||||
}
|
||||
|
||||
#[server]
|
||||
pub async fn create_property(property: ResProperty) ->Result<ApiResponse<()>, ServerFnError> {
|
||||
use crate::backend::get_pool;
|
||||
use crate::perm_check;
|
||||
|
||||
perm_check!(is_admin);
|
||||
|
||||
let pool = get_pool().await?;
|
||||
sqlx::query("INSERT INTO property(name, description, price, slot) VALUES($1, $2, $3, $4)")
|
||||
.bind(&property.name)
|
||||
.bind(&property.description)
|
||||
.bind(&property.price)
|
||||
.bind(&property.slot)
|
||||
.execute(&pool)
|
||||
.await?;
|
||||
|
||||
Ok(ApiResponse::Data(()))
|
||||
}
|
||||
|
||||
#[server]
|
||||
pub async fn edit_property(property: ResProperty) -> Result<ApiResponse<()>, ServerFnError> {
|
||||
use crate::backend::get_pool;
|
||||
use crate::perm_check;
|
||||
|
||||
perm_check!(is_admin);
|
||||
|
||||
let pool = get_pool().await?;
|
||||
sqlx::query("UPDATE property SET name = $1, description = $2, price = $3, active = $4, slot = $5 WHERE id = $6")
|
||||
.bind(&property.name)
|
||||
.bind(&property.description)
|
||||
.bind(&property.price)
|
||||
.bind(property.active)
|
||||
.bind(&property.slot)
|
||||
.bind(property.id())
|
||||
.execute(&pool)
|
||||
.await?;
|
||||
|
||||
Ok(ApiResponse::Data(()))
|
||||
}
|
||||
|
||||
#[server]
|
||||
pub async fn delete_property(id: i32) -> Result<ApiResponse<()>, ServerFnError> {
|
||||
use crate::backend::get_pool;
|
||||
use crate::perm_check;
|
||||
|
||||
perm_check!(is_admin);
|
||||
|
||||
let pool = get_pool().await?;
|
||||
sqlx::query("DELETE FROM property WHERE id = $1")
|
||||
.bind(id)
|
||||
.execute(&pool)
|
||||
.await?;
|
||||
|
||||
Ok(ApiResponse::Data(()))
|
||||
}
|
||||
|
||||
impl ForValidation for CreateProperty {
|
||||
fn entity(&self) -> &dyn Validate {
|
||||
&self.property
|
||||
}
|
||||
}
|
||||
|
||||
impl ForValidation for EditProperty {
|
||||
fn entity(&self) -> &dyn Validate {
|
||||
&self.property
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user