Added database connection & add participant function
This commit is contained in:
parent
1ceb513228
commit
5dce2e3ca9
@ -4,11 +4,16 @@ use leptos::*;
|
||||
#[component]
|
||||
pub fn HomePage() -> impl IntoView {
|
||||
// Creates a reactive value to update the button
|
||||
let (count, set_count) = create_signal(0);
|
||||
let on_click = move |_| set_count.update(|count| *count += 1);
|
||||
let on_click = move |_| {
|
||||
spawn_local(async {
|
||||
let _ =
|
||||
crate::util::surrealdb::add_participant(String::from("Test"), String::from("A1"))
|
||||
.await;
|
||||
})
|
||||
};
|
||||
|
||||
view! {
|
||||
<h1>"Welcome to Leptos!"</h1>
|
||||
<button on:click=on_click>"Click Me: " {count}</button>
|
||||
<button on:click=on_click>"Click Me:"</button>
|
||||
}
|
||||
}
|
||||
|
@ -1,18 +1,15 @@
|
||||
use leptos::{logging, server, ServerFnError};
|
||||
use once_cell::sync::Lazy;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::json;
|
||||
use std::borrow::Cow;
|
||||
|
||||
pub mod schemes;
|
||||
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(feature = "ssr")] {
|
||||
use once_cell::sync::Lazy;
|
||||
use surrealdb::engine::remote::ws::{Client, Ws};
|
||||
use surrealdb::opt::auth::Root;
|
||||
use surrealdb::Surreal;
|
||||
|
||||
pub static DB: Lazy<Surreal<Client>> = Lazy::new(Surreal::init);
|
||||
|
||||
pub mod schemas;
|
||||
}
|
||||
}
|
||||
|
||||
@ -30,3 +27,25 @@ pub async fn connect() -> Result<(), ServerFnError> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[server]
|
||||
pub async fn add_participant(name: String, group: String) -> Result<(), ServerFnError> {
|
||||
let created: Vec<schemas::Participant> = DB
|
||||
.create("participant")
|
||||
.content(schemas::NewParticipant { name, group })
|
||||
.await?;
|
||||
|
||||
match created.first() {
|
||||
Some(participant) => {
|
||||
logging::log!(
|
||||
"Succesfully created participant: {} ({})",
|
||||
participant.name,
|
||||
participant.group
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
None => Err(ServerFnError::ServerError(String::from(
|
||||
"Could not create participant",
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
23
application/src/util/surrealdb/schemas.rs
Normal file
23
application/src/util/surrealdb/schemas.rs
Normal file
@ -0,0 +1,23 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use surrealdb::sql::Thing;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct Events {
|
||||
lifesaver: String,
|
||||
hindernis: String,
|
||||
popduiken: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct Participant {
|
||||
pub id: Thing,
|
||||
pub name: String,
|
||||
pub group: String,
|
||||
pub events: Option<Events>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct NewParticipant {
|
||||
pub name: String,
|
||||
pub group: String,
|
||||
}
|
@ -1 +0,0 @@
|
||||
|
Loading…
Reference in New Issue
Block a user