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/src/main.rs | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) (limited to 'server/src/main.rs') 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