From e8fd37e32d3f41580d016f0ca61d68908c058bbf Mon Sep 17 00:00:00 2001 From: jake Date: Sat, 31 Oct 2020 10:50:14 -0600 Subject: [PATCH] save_item -> change_item, impl for postgres --- src/entity/gateway/entitygateway.rs | 2 +- src/entity/gateway/inmemory.rs | 7 +++++-- src/entity/gateway/postgres/postgres.rs | 8 ++++++++ src/ship/items/manager.rs | 22 +++------------------- 4 files changed, 17 insertions(+), 22 deletions(-) diff --git a/src/entity/gateway/entitygateway.rs b/src/entity/gateway/entitygateway.rs index ee6c1fa..1327eaf 100644 --- a/src/entity/gateway/entitygateway.rs +++ b/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!(); } diff --git a/src/entity/gateway/inmemory.rs b/src/entity/gateway/inmemory.rs index 602d869..e943376 100644 --- a/src/entity/gateway/inmemory.rs +++ b/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(()) } diff --git a/src/entity/gateway/postgres/postgres.rs b/src/entity/gateway/postgres/postgres.rs index 266a98b..bbc8d39 100644 --- a/src/entity/gateway/postgres/postgres.rs +++ b/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 { diff --git a/src/ship/items/manager.rs b/src/ship/items/manager.rs index 642ec51..a78e8b7 100644 --- a/src/ship/items/manager.rs +++ b/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(()) }