Browse Source

add postgres trade stuff

pull/113/head
jake 2 years ago
parent
commit
4a6bd47c9e
  1. 5
      src/entity/gateway/postgres/migrations/V0005__trade.sql
  2. 16
      src/entity/gateway/postgres/models.rs
  3. 18
      src/entity/gateway/postgres/postgres.rs

5
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,
);

16
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<PgTradeEntity> 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),
}
}
}

18
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)) Ok(Meseta(meseta.0 as u32))
} }
async fn create_trade(conn: &mut sqlx::PgConnection, char_id1: &CharacterEntityId, char_id2: &CharacterEntityId) -> Result<TradeEntity, GatewayError>
{
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] #[async_trait::async_trait]
impl EntityGateway for PostgresGateway { impl EntityGateway for PostgresGateway {
async fn transaction<'a>(&'a mut self) -> Result<Box<dyn EntityGatewayTransaction + 'a>, GatewayError> async fn transaction<'a>(&'a mut self) -> Result<Box<dyn EntityGatewayTransaction + 'a>, GatewayError>
@ -693,6 +703,10 @@ impl EntityGateway for PostgresGateway {
async fn get_bank_meseta(&mut self, char_id: &CharacterEntityId, bank: &BankName) -> Result<Meseta, GatewayError> { async fn get_bank_meseta(&mut self, char_id: &CharacterEntityId, bank: &BankName) -> Result<Meseta, GatewayError> {
get_bank_meseta(&mut *self.pool.acquire().await?, char_id, bank).await 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<TradeEntity, GatewayError> {
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<Meseta, GatewayError> { async fn get_bank_meseta(&mut self, char_id: &CharacterEntityId, bank: &BankName) -> Result<Meseta, GatewayError> {
get_bank_meseta(&mut *self.pgtransaction, char_id, bank).await get_bank_meseta(&mut *self.pgtransaction, char_id, bank).await
} }
async fn create_trade(&mut self, char_id1: &CharacterEntityId, char_id2: &CharacterEntityId) -> Result<TradeEntity, GatewayError> {
create_trade(&mut *self.pgtransaction, char_id1, char_id2).await
}
} }
Loading…
Cancel
Save