diff --git a/application/src/pages/index.rs b/application/src/pages/index.rs index 9a7c754..e774d2b 100644 --- a/application/src/pages/index.rs +++ b/application/src/pages/index.rs @@ -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! {

"Welcome to Leptos!"

- + } } diff --git a/application/src/util/surrealdb.rs b/application/src/util/surrealdb.rs index 71e62c9..3a031be 100644 --- a/application/src/util/surrealdb.rs +++ b/application/src/util/surrealdb.rs @@ -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> = 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 = 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", + ))), + } +} diff --git a/application/src/util/surrealdb/schemas.rs b/application/src/util/surrealdb/schemas.rs new file mode 100644 index 0000000..ca01bc5 --- /dev/null +++ b/application/src/util/surrealdb/schemas.rs @@ -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, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct NewParticipant { + pub name: String, + pub group: String, +} diff --git a/application/src/util/surrealdb/schemes.rs b/application/src/util/surrealdb/schemes.rs deleted file mode 100644 index 8b13789..0000000 --- a/application/src/util/surrealdb/schemes.rs +++ /dev/null @@ -1 +0,0 @@ - diff --git a/flake.nix b/flake.nix index 9e11bbb..5da69b7 100644 --- a/flake.nix +++ b/flake.nix @@ -30,9 +30,9 @@ cargo-insta llvmPackages_latest.llvm llvmPackages_latest.bintools + llvmPackages_latest.clangNoLibc zlib.out dart-sass - clang llvmPackages_latest.lld (rust-bin.stable.latest.default.override { extensions= [ "rust-src" "rust-analyzer" ];