1 Commits

Author SHA1 Message Date
ed0c107a80 Worked with @jkassman on match adding code
All checks were successful
Build Crate / build (push) Successful in 3m19s
2024-09-03 00:39:43 -04:00
10 changed files with 101 additions and 48 deletions

View File

@@ -42,7 +42,7 @@ https://daisyui.com/components/button/
`cargo install sea-orm-cli@1.0.0-rc.5` `cargo install sea-orm-cli@1.0.0-rc.5`
## 5. Install tailwindcss, for styling ## 6. Install tailwindcss, for styling
`npm install -D tailwindcss` `npm install -D tailwindcss`

View File

@@ -1,5 +1,6 @@
pub const REGISTER: &str = "/api/register"; pub const REGISTER: &str = "/api/register";
pub const LOGIN: &str = "/api/login"; pub const LOGIN: &str = "/api/login";
pub const ADD_MATCH: &str = "/api/add-match";
// TODO -> remove once it's used // TODO -> remove once it's used
#[cfg(engine)] #[cfg(engine)]
pub const LOGIN_TEST: &str = "/api/login-test"; pub const LOGIN_TEST: &str = "/api/login-test";

10
src/models/game_result.rs Normal file
View File

@@ -0,0 +1,10 @@
use serde::{Deserialize, Serialize};
use crate::entity::sea_orm_active_enums::GameType;
#[derive(Serialize, Deserialize, Clone)]
pub struct GameResult {
pub winner: String,
pub loser: String,
pub game_type: GameType,
}

View File

@@ -1,2 +1,3 @@
pub mod auth; pub mod auth;
pub mod game_result;
pub mod generic; pub mod generic;

View File

@@ -1,3 +1,4 @@
pub mod auth; pub mod auth;
pub mod pool;
pub mod routes; pub mod routes;
pub mod server_state; pub mod server_state;

View File

@@ -0,0 +1,25 @@
use crate::{
// entity::{prelude::*, user},
models::{game_result::GameResult, generic::GenericResponse},
server::server_state::ServerState,
};
use axum::{
extract::{Json, State},
http::StatusCode,
};
pub async fn post_game_result(
State(state): State<ServerState>,
Json(game_result): Json<GameResult>,
) -> (StatusCode, Result<(), Json<GenericResponse>>) {
(
StatusCode::BAD_REQUEST,
Err(Json(GenericResponse {
status: "Oopsie we had a fucky wucky".to_owned(),
})),
)
// if (game_result.loser == "ferris"){
// throw 406 "Not Acceptable; Ferris cannot lose"
// }
}

1
src/server/pool/mod.rs Normal file
View File

@@ -0,0 +1 @@
pub mod add_match;

View File

@@ -1,5 +1,5 @@
// (Server only) Routes // (Server only) Routes
use crate::endpoints::{FORGOT_PASSWORD, LOGIN, LOGIN_TEST, REGISTER}; use crate::endpoints::{ADD_MATCH, FORGOT_PASSWORD, LOGIN, LOGIN_TEST, REGISTER};
use axum::routing::{post, Router}; use axum::routing::{post, Router};
use super::{ use super::{
@@ -8,6 +8,7 @@ use super::{
login::{post_login_user, post_test_login}, login::{post_login_user, post_test_login},
register::post_register_user, register::post_register_user,
}, },
pool::add_match::post_game_result,
server_state::ServerState, server_state::ServerState,
}; };
@@ -15,6 +16,7 @@ pub fn get_api_router(state: ServerState) -> Router {
Router::new() Router::new()
.route(REGISTER, post(post_register_user)) .route(REGISTER, post(post_register_user))
.route(LOGIN, post(post_login_user)) .route(LOGIN, post(post_login_user))
.route(ADD_MATCH, post(post_game_result))
.route(LOGIN_TEST, post(post_test_login)) .route(LOGIN_TEST, post(post_test_login))
.route(FORGOT_PASSWORD, post(post_forgot_password)) .route(FORGOT_PASSWORD, post(post_forgot_password))
.with_state(state) .with_state(state)

View File

@@ -1,70 +1,80 @@
use crate::{components::layout::Layout, state_enums::ContentState}; use crate::{
components::{layout::Layout, sub_components::error_block::ErrorBlock},
state_enums::ContentState,
};
use perseus::prelude::*; use perseus::prelude::*;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use sycamore::prelude::*; use sycamore::prelude::*;
use web_sys::Event; use web_sys::Event;
cfg_if::cfg_if! {
if #[cfg(client)] {
use crate::{
endpoints::ADD_MATCH,
models::{game_result::GameResult, generic::GenericResponse},
templates::get_api_path,
};
use crate::entity::sea_orm_active_enums::GameType;
use reqwest::StatusCode;
}
}
// Reactive page // Reactive page
#[derive(Serialize, Deserialize, Clone, ReactiveState)] #[derive(Serialize, Deserialize, Clone, ReactiveState)]
#[rx(alias = "PageStateRx")] #[rx(alias = "PageStateRx")]
struct PageState { struct PageState {
winner: String, winner: String,
new_user: String, loser: String,
error: 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.winner.get().as_ref().clone() let var_name = async move {
spawn_local_scoped(cx, async move {}) let game_result = GameResult {
} winner: (*state.winner.get()).clone(),
loser: (*state.loser.get()).clone(),
game_type: GameType::Pool,
}; };
let handle_add_user = move |_event: Event| { let client = reqwest::Client::new();
#[cfg(client)] let response = client
{ .post(get_api_path(ADD_MATCH).as_str())
// state.winner.get().as_ref().clone() .json(&game_result)
spawn_local_scoped(cx, async move {}) .send()
.await
.unwrap();
if response.status() != StatusCode::OK {
let response = response.json::<GenericResponse>().await.unwrap();
state.error.set(response.status.to_string());
return;
}
// let response = response.json::<())>().await.unwrap();
};
let var_name = var_name;
spawn_local_scoped(cx, var_name)
} }
}; };
view! { cx, view! { cx,
Layout(content_state = ContentState::Pool) { Layout(content_state = ContentState::Pool) {
div (class = "flex flex-wrap") {
select { ErrorBlock(error = state.error.clone())
option (value="red")
option (value="blue") div (class = "label") { span (class = "label-text") { "Winner" } }
} input (bind:value = state.winner, class = "input input-bordered w-full m-2")
}
div (class = "flex flex-wrap") { div (class = "label") { span (class = "label-text") { "Loser" } }
input (bind:value = state.winner, input (bind:value = state.loser, class = "input input-bordered w-full m-2")
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 \ div (class = "flex justify-center") {
focus:bg-white",) button (on:click = handle_add_match, class="btn"){"Record Match"}
}
div (class = "flex flex-wrap") {
button(on:click = handle_add_match,
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 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"
}
} }
} }
} }
@@ -76,8 +86,9 @@ 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 {
winner: "Ferris".to_owned(), winner: String::new(),
new_user: "newguy".to_owned(), loser: String::new(),
error: String::new(),
}) })
} }

View File

@@ -0,0 +1 @@