From 2d9d37bd5e4770a05cae780f2113266f3fdd0915 Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Sat, 15 Oct 2022 09:34:44 -0400 Subject: Add table creation --- server/migrations/20221008120534_init.up.sql | 4 +++- server/src/main.rs | 35 ++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 3 deletions(-) (limited to 'server') 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, 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( -- cgit v1.2.3