diff options
author | Kjetil Orbekk <kj@orbekk.com> | 2023-01-02 11:58:49 -0500 |
---|---|---|
committer | Kjetil Orbekk <kj@orbekk.com> | 2023-01-02 11:58:49 -0500 |
commit | 8696dcc0afdc0896081405df13c887384500961b (patch) | |
tree | eea3f0d0534795611ba4c05dcc943ff9af364465 /server/src | |
parent | 2d50ae0b134366b875fa54912422b2484dd70d9b (diff) |
Insert table in db on creation
Diffstat (limited to 'server/src')
-rw-r--r-- | server/src/table.rs | 62 |
1 files changed, 52 insertions, 10 deletions
diff --git a/server/src/table.rs b/server/src/table.rs index 7e49c7f..d2eff51 100644 --- a/server/src/table.rs +++ b/server/src/table.rs @@ -9,7 +9,8 @@ use protocol::{ simple_bots::{AlwaysPassBiddingBot, RandomPlayingBot}, }; use rand::random; -use sqlx::PgPool; +use sqlx::{PgPool, Postgres, Transaction}; +use uuid::Uuid; use crate::error::BridgeError; @@ -24,7 +25,9 @@ pub trait Table { self: Box<Self>, card: Card, ) -> Result<Box<dyn Table + Send>, BridgeError>; - async fn new_deal(self: Box<Self>) -> Result<Box<dyn Table + Send>, BridgeError>; + async fn new_deal( + self: Box<Self>, + ) -> Result<Box<dyn Table + Send>, BridgeError>; } pub struct InMemoryTable { @@ -73,7 +76,9 @@ impl Table for InMemoryTable { Ok(Box::new(Self { state: game.into() })) } - async fn new_deal(self: Box<Self>) -> Result<Box<dyn Table + Send>, BridgeError> { + async fn new_deal( + self: Box<Self>, + ) -> Result<Box<dyn Table + Send>, BridgeError> { Ok(Box::new(Self { state: GameState::new(random(), random()).into(), })) @@ -116,11 +121,37 @@ pub struct DbTable { } impl DbTable { - pub fn new(db: PgPool) -> Self { - Self { + pub async fn new(db: PgPool, id: Uuid) -> Result<Self, BridgeError> { + let mut txn = db.begin().await?; + if let Some(_) = + sqlx::query!("select id from bridge_table where id = $1", id) + .fetch_optional(&mut txn) + .await? + { + return Self::restore(db, txn, id).await; + } + + sqlx::query!("insert into bridge_table (id) values($1)", id) + .execute(&mut txn) + .await?; + + txn.commit().await?; + Ok(Self { db, inner: Box::new(InMemoryTable::new()), - } + }) + } + + pub async fn restore( + db: PgPool, + txn: Transaction<'_, Postgres>, + id: Uuid, + ) -> Result<Self, BridgeError> { + txn.rollback().await?; + Ok(Self { + db, + inner: Box::new(InMemoryTable::new()), + }) } } @@ -134,17 +165,28 @@ impl Table for DbTable { self: Box<Self>, bid: Bid, ) -> Result<Box<dyn Table + Send>, BridgeError> { - Ok(Box::new(Self { inner: self.inner.bid(bid).await?, ..*self })) + Ok(Box::new(Self { + inner: self.inner.bid(bid).await?, + ..*self + })) } async fn play( self: Box<Self>, card: Card, ) -> Result<Box<dyn Table + Send>, BridgeError> { - Ok(Box::new(Self { inner: self.inner.play(card).await?, ..*self })) + Ok(Box::new(Self { + inner: self.inner.play(card).await?, + ..*self + })) } - async fn new_deal(self: Box<Self>) -> Result<Box<dyn Table + Send>, BridgeError> { - Ok(Box::new(Self { inner: self.inner.new_deal().await?, ..*self })) + async fn new_deal( + self: Box<Self>, + ) -> Result<Box<dyn Table + Send>, BridgeError> { + Ok(Box::new(Self { + inner: self.inner.new_deal().await?, + ..*self + })) } } |