diff options
author | Kjetil Orbekk <kj@orbekk.com> | 2022-10-15 09:34:44 -0400 |
---|---|---|
committer | Kjetil Orbekk <kj@orbekk.com> | 2022-10-15 09:34:44 -0400 |
commit | 2d9d37bd5e4770a05cae780f2113266f3fdd0915 (patch) | |
tree | 2348c12d2be7590c5fe4a64cf759f749c02413a7 /server | |
parent | 58f91c62065d9a7e37c953503100b87b506297e7 (diff) |
Add table creation
Diffstat (limited to 'server')
-rw-r--r-- | server/migrations/20221008120534_init.up.sql | 4 | ||||
-rw-r--r-- | server/src/main.rs | 35 |
2 files changed, 36 insertions, 3 deletions
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( |