Added database connection & add participant function

This commit is contained in:
xeovalyte 2024-04-04 17:10:58 +02:00
parent 1ceb513228
commit 5dce2e3ca9
No known key found for this signature in database
5 changed files with 57 additions and 11 deletions

View File

@ -4,11 +4,16 @@ use leptos::*;
#[component] #[component]
pub fn HomePage() -> impl IntoView { pub fn HomePage() -> impl IntoView {
// Creates a reactive value to update the button // Creates a reactive value to update the button
let (count, set_count) = create_signal(0); let on_click = move |_| {
let on_click = move |_| set_count.update(|count| *count += 1); spawn_local(async {
let _ =
crate::util::surrealdb::add_participant(String::from("Test"), String::from("A1"))
.await;
})
};
view! { view! {
<h1>"Welcome to Leptos!"</h1> <h1>"Welcome to Leptos!"</h1>
<button on:click=on_click>"Click Me: " {count}</button> <button on:click=on_click>"Click Me:"</button>
} }
} }

View File

@ -1,18 +1,15 @@
use leptos::{logging, server, ServerFnError}; 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! { cfg_if::cfg_if! {
if #[cfg(feature = "ssr")] { if #[cfg(feature = "ssr")] {
use once_cell::sync::Lazy;
use surrealdb::engine::remote::ws::{Client, Ws}; use surrealdb::engine::remote::ws::{Client, Ws};
use surrealdb::opt::auth::Root; use surrealdb::opt::auth::Root;
use surrealdb::Surreal; use surrealdb::Surreal;
pub static DB: Lazy<Surreal<Client>> = Lazy::new(Surreal::init); pub static DB: Lazy<Surreal<Client>> = Lazy::new(Surreal::init);
pub mod schemas;
} }
} }
@ -30,3 +27,25 @@ pub async fn connect() -> Result<(), ServerFnError> {
Ok(()) 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",
))),
}
}

View 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,
}

View File

@ -1 +0,0 @@

View File

@ -30,9 +30,9 @@
cargo-insta cargo-insta
llvmPackages_latest.llvm llvmPackages_latest.llvm
llvmPackages_latest.bintools llvmPackages_latest.bintools
llvmPackages_latest.clangNoLibc
zlib.out zlib.out
dart-sass dart-sass
clang
llvmPackages_latest.lld llvmPackages_latest.lld
(rust-bin.stable.latest.default.override { (rust-bin.stable.latest.default.override {
extensions= [ "rust-src" "rust-analyzer" ]; extensions= [ "rust-src" "rust-analyzer" ];