summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKjetil Orbekk <kj@orbekk.com>2022-10-15 09:34:44 -0400
committerKjetil Orbekk <kj@orbekk.com>2022-10-15 09:34:44 -0400
commit2d9d37bd5e4770a05cae780f2113266f3fdd0915 (patch)
tree2348c12d2be7590c5fe4a64cf759f749c02413a7
parent58f91c62065d9a7e37c953503100b87b506297e7 (diff)
Add table creation
-rw-r--r--Cargo.lock5
-rw-r--r--server/migrations/20221008120534_init.up.sql4
-rw-r--r--server/src/main.rs35
-rw-r--r--webapp/Cargo.toml1
-rw-r--r--webapp/src/main.rs18
5 files changed, 54 insertions, 9 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 5e6c007..df63e24 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2411,9 +2411,9 @@ checksum = "e8db7427f936968176eaa7cdf81b7f98b980b18495ec28f1b5791ac3bfe3eea9"
[[package]]
name = "uuid"
-version = "1.2.0"
+version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "683f0a095f6dcf74520a5f17a12452ae6f970abbd2443299a1e226fd38195f2b"
+checksum = "feb41e78f93363bb2df8b0e86a2ca30eed7806ea16ea0c790d757cf93f79be83"
dependencies = [
"getrandom",
"rand",
@@ -2567,6 +2567,7 @@ dependencies = [
"regex",
"strum",
"strum_macros",
+ "uuid",
"wasm-bindgen-futures",
"wasm-logger",
"web-sys",
diff --git a/server/migrations/20221008120534_init.up.sql b/server/migrations/20221008120534_init.up.sql
index 729446c..8c53bb8 100644
--- a/server/migrations/20221008120534_init.up.sql
+++ b/server/migrations/20221008120534_init.up.sql
@@ -16,10 +16,12 @@ create table active_tables (
id uuid primary key not null
);
+create type player_position as enum ('west', 'north', 'east', 'south');
+
create table table_players (
active_tables_id uuid not null references active_tables (id),
player_id varchar(64) not null references players (id),
- position jsonb,
+ position player_position,
primary key(active_tables_id, player_id, position)
);
create unique index player_table on table_players (player_id);
diff --git a/server/src/main.rs b/server/src/main.rs
index 6896818..eaf1379 100644
--- a/server/src/main.rs
+++ b/server/src/main.rs
@@ -1,4 +1,5 @@
use std::{collections::HashMap, env, str::FromStr, sync::Arc};
+use uuid::Uuid;
use auth::AuthenticatedSession;
use axum::{
@@ -72,8 +73,38 @@ async fn main() {
.unwrap();
}
-async fn create_table(session: AuthenticatedSession) -> Result<(), BridgeError> {
- todo!()
+async fn create_table(
+ session: AuthenticatedSession,
+ extension: ContextExtension,
+) -> Result<Json<Uuid>, BridgeError> {
+ let txn = extension.db.begin().await?;
+ let table_id = sqlx::query!(
+ r#"
+ insert into active_tables (id)
+ values ($1)
+ returning id
+ "#,
+ Uuid::new_v4()
+ )
+ .fetch_one(&extension.db)
+ .await?
+ .id;
+
+ sqlx::query!(
+ r#"
+ insert into table_players (active_tables_id,
+ player_id,
+ position)
+ values ($1, $2, 'south')
+ "#,
+ table_id,
+ session.player_id
+ )
+ .execute(&extension.db)
+ .await?;
+
+ txn.commit().await?;
+ Ok(Json(table_id))
}
async fn user_info(
diff --git a/webapp/Cargo.toml b/webapp/Cargo.toml
index b7c0d28..fd048ab 100644
--- a/webapp/Cargo.toml
+++ b/webapp/Cargo.toml
@@ -21,6 +21,7 @@ wasm-bindgen-futures = "0.4.33"
protocol = { path = "../protocol" }
yew-router = "0.16.0"
web-sys = { version = "0.3.60", features = ["Location", "Document"] }
+uuid = { version = "1.2.1", features = ["serde"] }
[dev-dependencies]
env_logger = "0.8.4"
diff --git a/webapp/src/main.rs b/webapp/src/main.rs
index 1b60a9f..4ba8e5f 100644
--- a/webapp/src/main.rs
+++ b/webapp/src/main.rs
@@ -2,6 +2,7 @@ use std::rc::Rc;
#[allow(unused_imports)]
use log::{debug, error, info, warn};
+use uuid::Uuid;
use yew::prelude::*;
use yew_router::prelude::*;
pub mod bridge_engine;
@@ -53,15 +54,24 @@ fn home() -> Html {
None => html! { <p><a href="/api/login">{ "Log in" }</a></p> },
};
+ let create_table = Callback::from(|_| {
+ wasm_bindgen_futures::spawn_local(async move {
+ let response = Request::post("/api/table").send().await.unwrap();
+ let table_id: Uuid = response.json().await.unwrap();
+ info!("Created table {table_id}");
+ });
+ });
+
html! {
<>
if let Some(error) = &ctx.error {
<ErrorInfo ..error.clone()/>
}
- { user }
- <p>
- <Link<Route> to={Route::Playground}>{ "Playground" }</Link<Route>>
- </p>
+ <ul>
+ <li>{ user }</li>
+ <li><Link<Route> to={Route::Playground}>{ "Playground" }</Link<Route>></li>
+ <li><button onclick={create_table}>{ "Create table" }</button></li>
+ </ul>
</>
}
}