Added settings for company info. Added code for entity validation.
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
use crate::backend::data::Company;
|
||||
use leptos::*;
|
||||
|
||||
#[server(GetCompany, "/api", "Url", "get_company")]
|
||||
pub async fn get_company(cx: Scope) -> Result<Company, ServerFnError> {
|
||||
use crate::backend::AppData;
|
||||
use actix_web::web::Data;
|
||||
use leptos_actix::extract;
|
||||
|
||||
let pool = extract(
|
||||
cx,
|
||||
|data: Data<AppData>| async move { data.db_pool().clone() },
|
||||
)
|
||||
.await?;
|
||||
|
||||
let cmp = sqlx::query_as::<_, Company>("SELECT * FROM company")
|
||||
.fetch_one(&pool)
|
||||
.await?;
|
||||
|
||||
Ok(cmp)
|
||||
}
|
||||
|
||||
#[server(UpdateCompany, "/api", "Url", "update_company")]
|
||||
pub async fn update_company(cx: Scope, company: Company) -> Result<(), ServerFnError> {
|
||||
use crate::backend::AppData;
|
||||
use actix_web::web::Data;
|
||||
use leptos_actix::extract;
|
||||
|
||||
let pool = extract(
|
||||
cx,
|
||||
|data: Data<AppData>| async move { data.db_pool().clone() },
|
||||
)
|
||||
.await?;
|
||||
|
||||
sqlx::query(
|
||||
"UPDATE company SET name = $1, street = $2, house_number = $3, zip_code = $4, city = $5 \
|
||||
WHERE id = $6")
|
||||
.bind(company.name.clone())
|
||||
.bind(company.street.clone())
|
||||
.bind(company.house_number.clone())
|
||||
.bind(company.zip_code.clone())
|
||||
.bind(company.city.clone())
|
||||
.bind(company.id())
|
||||
.execute(&pool)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
+19
-6
@@ -1,14 +1,27 @@
|
||||
use chrono::{NaiveDate, NaiveTime, Weekday};
|
||||
use rust_decimal::Decimal;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
use validator::Validate;
|
||||
|
||||
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq, Validate)]
|
||||
#[cfg_attr(feature = "ssr", derive(sqlx::FromRow))]
|
||||
pub struct Company {
|
||||
id: u16,
|
||||
name: String,
|
||||
street: String,
|
||||
house_number: String,
|
||||
zip_code: String,
|
||||
city: String,
|
||||
id: i32,
|
||||
#[validate(length(min = 1,message = "Name cannot be empty"))]
|
||||
pub name: String,
|
||||
#[validate(length(min = 1,message = "Street cannot be empty"))]
|
||||
pub street: String,
|
||||
#[validate(length(min = 1,message = "House number cannot be empty"))]
|
||||
pub house_number: String,
|
||||
pub zip_code: String,
|
||||
pub city: String,
|
||||
}
|
||||
|
||||
impl Company {
|
||||
pub fn id(&self) -> i32 {
|
||||
self.id
|
||||
}
|
||||
}
|
||||
|
||||
pub struct User {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use cfg_if::cfg_if;
|
||||
|
||||
pub mod data;
|
||||
pub mod company;
|
||||
|
||||
cfg_if!{
|
||||
if #[cfg(feature = "ssr")] {
|
||||
|
||||
Reference in New Issue
Block a user