1 Commits

Author SHA1 Message Date
Jacob Kassman
01eaf059dd Add the ability to add users
All checks were successful
Build Crate / build (push) Successful in 3m25s
2023-12-09 21:49:59 -05:00
6 changed files with 99 additions and 8 deletions

View File

@@ -33,6 +33,11 @@ pub struct PoolMatchList {
pub max_id: MatchId, pub max_id: MatchId,
} }
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct UserList{
pub users: Vec<String>,
}
impl PoolMatch { impl PoolMatch {
pub fn new(data: MatchData, time: DateTime<Utc>) -> PoolMatch { pub fn new(data: MatchData, time: DateTime<Utc>) -> PoolMatch {
PoolMatch { id: 0, data, time } PoolMatch { id: 0, data, time }
@@ -53,3 +58,17 @@ impl PoolMatchList {
self.pool_matches.push(pool_match); self.pool_matches.push(pool_match);
} }
} }
impl UserList {
pub fn new() -> UserList {
UserList {
users: vec![],
}
}
pub fn add_user(&mut self, user: String) -> usize {
let user_id = self.users.len();
self.users.push(user);
user_id
}
}

View File

@@ -1,6 +1,7 @@
// (Server only) In-memory data storage and persistent storage // (Server only) In-memory data storage and persistent storage
use crate::data::pool_match::PoolMatchList; use crate::data::pool_match::PoolMatchList;
use crate::data::pool_match::UserList;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{fs, path::Path, sync::Mutex}; use std::{fs, path::Path, sync::Mutex};
@@ -8,6 +9,7 @@ use std::{fs, path::Path, sync::Mutex};
#[derive(Serialize, Deserialize, Clone)] #[derive(Serialize, Deserialize, Clone)]
pub struct Store { pub struct Store {
pub matches: PoolMatchList, pub matches: PoolMatchList,
pub users: UserList,
} }
impl Store { impl Store {
@@ -16,6 +18,7 @@ impl Store {
match Path::new("data/store.json").exists() { match Path::new("data/store.json").exists() {
false => Store { false => Store {
matches: PoolMatchList::new(), matches: PoolMatchList::new(),
users: UserList::new(),
}, },
true => { true => {
let contents = fs::read_to_string("data/store.json").unwrap(); let contents = fs::read_to_string("data/store.json").unwrap();

View File

@@ -1 +1,2 @@
pub const MATCH: &str = "/api/post-match"; pub const MATCH: &str = "/api/post-match";
pub const USER: &str = "/api/post-user";

View File

@@ -2,10 +2,11 @@
use crate::{ use crate::{
data::{ data::{
pool_match::{PoolMatch, PoolMatchList}, pool_match::{PoolMatch, PoolMatchList, UserList},
store::DATA, store::DATA,
}, },
endpoints::MATCH, endpoints::MATCH,
endpoints::USER,
}; };
use axum::{ use axum::{
extract::Json, extract::Json,
@@ -15,6 +16,7 @@ use std::thread;
pub fn register_routes(app: Router) -> Router { pub fn register_routes(app: Router) -> Router {
let app = app.route(MATCH, post(post_match)); let app = app.route(MATCH, post(post_match));
let app = app.route(USER, post(post_user));
app app
} }
@@ -32,3 +34,19 @@ async fn post_match(Json(pool_match): Json<PoolMatch>) -> Json<PoolMatchList> {
Json(matches) Json(matches)
} }
async fn post_user(user: String) -> Json<UserList> {
// Update the store with the new match
let users = thread::spawn(move || {
// Get the store
let mut data = DATA.lock().unwrap();
let user_id = (*data).users.add_user(user);
println!("Added new user id: {}\nAll users: {:?}", user_id, (*data).users);
(*data).users.clone()
})
.join()
.unwrap();
Json(users)
}

View File

@@ -7,9 +7,9 @@ use web_sys::Event;
cfg_if::cfg_if! { cfg_if::cfg_if! {
if #[cfg(client)] { if #[cfg(client)] {
use crate::data::pool_match::{PoolMatch, PoolMatchList}; use crate::data::pool_match::{PoolMatch, PoolMatchList, UserList};
use crate::templates::global_state::AppStateRx; use crate::templates::global_state::AppStateRx;
use crate::endpoints::MATCH; use crate::endpoints::{MATCH, USER};
use crate::templates::get_api_path; use crate::templates::get_api_path;
use chrono::Utc; use chrono::Utc;
} }
@@ -20,14 +20,15 @@ cfg_if::cfg_if! {
#[derive(Serialize, Deserialize, Clone, ReactiveState)] #[derive(Serialize, Deserialize, Clone, ReactiveState)]
#[rx(alias = "PageStateRx")] #[rx(alias = "PageStateRx")]
struct PageState { struct PageState {
name: String, winner: String,
new_user: String,
} }
fn add_game_form_page<'a, G: Html>(cx: BoundedScope<'_, 'a>, state: &'a PageStateRx) -> View<G> { fn add_game_form_page<'a, G: Html>(cx: BoundedScope<'_, 'a>, state: &'a PageStateRx) -> View<G> {
let handle_add_match = move |_event: Event| { let handle_add_match = move |_event: Event| {
#[cfg(client)] #[cfg(client)]
{ {
// state.name.get().as_ref().clone() // state.winner.get().as_ref().clone()
spawn_local_scoped(cx, async move { spawn_local_scoped(cx, async move {
let new_match = PoolMatch::new( let new_match = PoolMatch::new(
MatchData { MatchData {
@@ -53,10 +54,37 @@ fn add_game_form_page<'a, G: Html>(cx: BoundedScope<'_, 'a>, state: &'a PageStat
} }
}; };
let handle_add_user = move |_event: Event| {
#[cfg(client)]
{
// state.winner.get().as_ref().clone()
spawn_local_scoped(cx, async move {
let client = reqwest::Client::new();
let new_users = client
.post(get_api_path(USER).as_str())
.body(state.new_user.get().as_ref().clone())
.send()
.await
.unwrap()
.json::<UserList>()
.await
.unwrap();
let global_state = Reactor::<G>::from_cx(cx).get_global_state::<AppStateRx>(cx);
global_state.users.set(new_users);
})
}
};
view! { cx, view! { cx,
Layout(title = "Add Game Results") { Layout(title = "Add Game Results") {
div (class = "flex flex-wrap") { div (class = "flex flex-wrap") {
input (bind:value = state.name, select {
option (value="red")
option (value="blue")
}
}
div (class = "flex flex-wrap") {
input (bind:value = state.winner,
class = "appearance-none block w-full bg-gray-200 text-gray-700 border \ class = "appearance-none block w-full bg-gray-200 text-gray-700 border \
border-red-500 rounded py-3 px-4 mb-3 leading-tight focus:outline-none \ border-red-500 rounded py-3 px-4 mb-3 leading-tight focus:outline-none \
focus:bg-white",) focus:bg-white",)
@@ -69,6 +97,20 @@ fn add_game_form_page<'a, G: Html>(cx: BoundedScope<'_, 'a>, state: &'a PageStat
"Add result" "Add result"
} }
} }
div (class = "flex flex-wrap") {
input (bind:value = state.new_user,
class = "appearance-none block w-full bg-gray-200 text-gray-700 border \
border-red-500 rounded py-3 px-4 mb-3 leading-tight focus:outline-none \
focus:bg-white",)
}
div (class = "flex flex-wrap") {
button(on:click = handle_add_user,
class = "flex-shrink-0 bg-teal-500 hover:bg-teal-700 border-teal-500 \
hover:border-teal-700 text-sm border-4 text-white py-1 px-2 rounded",
) {
"Add new user"
}
}
} }
} }
} }
@@ -79,7 +121,8 @@ async fn get_request_state(
_req: Request, _req: Request,
) -> Result<PageState, BlamedError<std::convert::Infallible>> { ) -> Result<PageState, BlamedError<std::convert::Infallible>> {
Ok(PageState { Ok(PageState {
name: "Ferris".to_string(), winner: "Ferris".to_string(),
new_user: "newguy".to_string(),
}) })
} }

View File

@@ -1,6 +1,8 @@
// Not a page, global state that is shared between all pages // Not a page, global state that is shared between all pages
use crate::data::pool_match::PoolMatchList; use crate::data::pool_match::PoolMatchList;
use crate::data::pool_match::UserList;
use perseus::{prelude::*, state::GlobalStateCreator}; use perseus::{prelude::*, state::GlobalStateCreator};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@@ -16,6 +18,7 @@ cfg_if::cfg_if! {
#[rx(alias = "AppStateRx")] #[rx(alias = "AppStateRx")]
pub struct AppState { pub struct AppState {
pub matches: PoolMatchList, pub matches: PoolMatchList,
pub users: UserList,
} }
pub fn get_global_state_creator() -> GlobalStateCreator { pub fn get_global_state_creator() -> GlobalStateCreator {
@@ -30,7 +33,11 @@ fn get_state() -> AppState {
.join() .join()
.unwrap(); .unwrap();
AppState { matches } let users = thread::spawn(move || DATA.lock().unwrap().deref().users.clone())
.join()
.unwrap();
AppState { matches, users }
} }
#[engine_only_fn] #[engine_only_fn]