diff --git a/src/entity/gateway/postgres/migrations/V0005__trade.sql b/src/entity/gateway/postgres/migrations/V0005__trade.sql new file mode 100644 index 0000000..16d3b84 --- /dev/null +++ b/src/entity/gateway/postgres/migrations/V0005__trade.sql @@ -0,0 +1,5 @@ +create table trades ( + id serial primary key not null, + character1 integer references character (id) not null, + character2 integer references character (id) not null, +); diff --git a/src/entity/gateway/postgres/models.rs b/src/entity/gateway/postgres/models.rs index 81296ae..c1257ee 100644 --- a/src/entity/gateway/postgres/models.rs +++ b/src/entity/gateway/postgres/models.rs @@ -858,3 +858,19 @@ impl From<(CharacterEntityId, EquippedEntity)> for PgEquipped { } } +#[derive(Debug, sqlx::FromRow)] +pub struct PgTradeEntity { + id: i32, + character1: i32, + character2: i32, +} + +impl From for TradeEntity { + fn from(other: PgTradeEntity) -> TradeEntity { + TradeEntity { + id: TradeId(other.id as u32), + character1: CharacterEntityId(other.character1 as u32), + character2: CharacterEntityId(other.character2 as u32), + } + } +} diff --git a/src/entity/gateway/postgres/postgres.rs b/src/entity/gateway/postgres/postgres.rs index 938c946..81ae972 100644 --- a/src/entity/gateway/postgres/postgres.rs +++ b/src/entity/gateway/postgres/postgres.rs @@ -558,6 +558,16 @@ async fn get_bank_meseta(conn: &mut sqlx::PgConnection, char_id: &CharacterEntit Ok(Meseta(meseta.0 as u32)) } +async fn create_trade(conn: &mut sqlx::PgConnection, char_id1: &CharacterEntityId, char_id2: &CharacterEntityId) -> Result +{ + let trade = sqlx::query_as::<_, PgTradeEntity>(r#"insert into trades (character1, character2) values ($1, $2) returning *;"#) + .bind(char_id1.0) + .bind(char_id2.0) + .fetch_one(conn) + .await?; + Ok(trade.into()) +} + #[async_trait::async_trait] impl EntityGateway for PostgresGateway { async fn transaction<'a>(&'a mut self) -> Result, GatewayError> @@ -693,6 +703,10 @@ impl EntityGateway for PostgresGateway { async fn get_bank_meseta(&mut self, char_id: &CharacterEntityId, bank: &BankName) -> Result { get_bank_meseta(&mut *self.pool.acquire().await?, char_id, bank).await } + + async fn create_trade(&mut self, char_id1: &CharacterEntityId, char_id2: &CharacterEntityId) -> Result { + create_trade(&mut *self.pool.acquire().await?, char_id1, char_id2).await + } } @@ -809,5 +823,9 @@ impl<'c> EntityGateway for PostgresTransaction<'c> { async fn get_bank_meseta(&mut self, char_id: &CharacterEntityId, bank: &BankName) -> Result { get_bank_meseta(&mut *self.pgtransaction, char_id, bank).await } + + async fn create_trade(&mut self, char_id1: &CharacterEntityId, char_id2: &CharacterEntityId) -> Result { + create_trade(&mut *self.pgtransaction, char_id1, char_id2).await + } }