Browse Source

save_item -> change_item, impl for postgres

pbs
jake 4 years ago
parent
commit
e8fd37e32d
  1. 2
      src/entity/gateway/entitygateway.rs
  2. 7
      src/entity/gateway/inmemory.rs
  3. 8
      src/entity/gateway/postgres/postgres.rs
  4. 22
      src/ship/items/manager.rs

2
src/entity/gateway/entitygateway.rs

@ -65,7 +65,7 @@ pub trait EntityGateway: Send + Sync + Clone {
unimplemented!();
}
async fn save_item(&mut self, _item: &ItemEntity) -> Result<(), GatewayError> {
async fn change_item(&mut self, _id: &ItemEntityId, _item: &ItemDetail) -> Result<(), GatewayError> {
unimplemented!();
}

7
src/entity/gateway/inmemory.rs

@ -165,9 +165,12 @@ impl EntityGateway for InMemoryGateway {
Ok(new_item)
}
async fn save_item(&mut self, item: &ItemEntity) -> Result<(), GatewayError> {
async fn change_item(&mut self, id: &ItemEntityId, item: &ItemDetail) -> Result<(), GatewayError> {
let mut items = self.items.lock().unwrap();
items.insert(item.id, item.clone());
if let Some((_, ref mut old_item)) = items.iter_mut().find(|(existing_id, _)| **existing_id == *id) {
old_item.item = item.clone();
}
Ok(())
}

8
src/entity/gateway/postgres/postgres.rs

@ -312,6 +312,14 @@ impl EntityGateway for PostgresGateway {
})
}
async fn change_item(&mut self, id: &ItemEntityId, item: &ItemDetail) -> Result<(), GatewayError> {
sqlx::query("update item set item = $1 where id = $2")
.bind(sqlx::types::Json(PgItemDetail::from(item.clone())))
.bind(id.0)
.execute(&self.pool).await?;
Ok(())
}
async fn change_item_location(&mut self, item_id: &ItemEntityId, item_location: ItemLocation) -> Result<(), GatewayError> {
let mut tx = self.pool.begin().await?;
if let ItemLocation::Inventory{slot, ..} = &item_location {

22
src/ship/items/manager.rs

@ -933,16 +933,8 @@ impl ItemManager {
character_id: character.id,
slot: slot,
equipped: true,
}).await;
entity_gateway.save_item(&ItemEntity{
id: inventory_item.entity_id,
location: ItemLocation::Inventory{
character_id: character.id,
slot: slot,
equipped: true,
},
item: inventory_item.item.clone(),
}).await;
}).await?;
entity_gateway.change_item(&inventory_item.entity_id, &inventory_item.item).await?;
Ok(())
}
@ -968,15 +960,7 @@ impl ItemManager {
slot: slot,
equipped: false,
}).await;
entity_gateway.save_item(&ItemEntity{
id: inventory_item.entity_id,
location: ItemLocation::Inventory{
character_id: character.id,
slot: slot,
equipped: false,
},
item: inventory_item.item.clone(),
}).await;
entity_gateway.change_item(&inventory_item.entity_id, &inventory_item.item).await?;
Ok(())
}

Loading…
Cancel
Save