save_item -> change_item, impl for postgres

This commit is contained in:
jake 2020-10-31 10:50:14 -06:00
parent ce3e58d459
commit e8fd37e32d
4 changed files with 17 additions and 22 deletions

View File

@ -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!();
}

View File

@ -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(())
}

View File

@ -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 {

View File

@ -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(())
}