From 2a89db5172f682683d7055803622705994ab09bb Mon Sep 17 00:00:00 2001 From: progre Date: Sun, 26 Nov 2023 18:30:37 +0900 Subject: [PATCH] wip --- junowen-server/src/database.rs | 7 +---- junowen-server/src/database/dynamodb.rs | 30 ++++++++++++++----- .../src/database/dynamodb/shared_room.rs | 19 +++--------- junowen-server/src/database/file.rs | 9 ++---- junowen-server/src/routes/custom.rs | 3 +- 5 files changed, 31 insertions(+), 37 deletions(-) diff --git a/junowen-server/src/database.rs b/junowen-server/src/database.rs index 4e6ef01..5f21fd8 100644 --- a/junowen-server/src/database.rs +++ b/junowen-server/src/database.rs @@ -60,12 +60,7 @@ pub enum PutError { pub trait SharedRoomTables: Send + Sync + 'static { async fn put_room(&self, offer: SharedRoom) -> Result<(), PutError>; async fn find_room(&self, name: String) -> Result>; - async fn keep_room( - &self, - name: String, - key: String, - ttl_sec: u64, - ) -> Result>; + async fn keep_room(&self, name: String, key: String, ttl_sec: u64) -> Result; async fn remove_room(&self, name: String, key: Option) -> Result; async fn put_room_opponent_answer( diff --git a/junowen-server/src/database/dynamodb.rs b/junowen-server/src/database/dynamodb.rs index becc6bc..e186cf4 100644 --- a/junowen-server/src/database/dynamodb.rs +++ b/junowen-server/src/database/dynamodb.rs @@ -3,7 +3,10 @@ mod shared_room; use std::env; use anyhow::{anyhow, Result}; -use aws_sdk_dynamodb::{error::SdkError, types::AttributeValue}; +use aws_sdk_dynamodb::{ + error::SdkError, + types::{AttributeValue, ReturnValue}, +}; use serde::{Deserialize, Serialize}; use serde_dynamo::{from_item, to_item}; @@ -74,17 +77,30 @@ impl DynamoDB { where T: Deserialize<'a>, { - let output = self + let result = self .client .delete_item() .table_name(table_name) .key("name", AttributeValue::S(name)) + .return_values(ReturnValue::AllOld) .send() - .await?; - let item = output - .attributes() - .ok_or_else(|| anyhow!("attributes not found"))?; - Ok(Some(from_item(item.to_owned())?)) + .await; + match result { + Err(err) => { + if let SdkError::ServiceError(service_error) = &err { + if service_error.err().is_resource_not_found_exception() { + return Ok(None); + } + } + Err(err.into()) + } + Ok(output) => { + let item = output + .attributes() + .ok_or_else(|| anyhow!("attributes not found"))?; + Ok(Some(from_item(item.to_owned())?)) + } + } } async fn remove_item_by_name_and_key( diff --git a/junowen-server/src/database/dynamodb/shared_room.rs b/junowen-server/src/database/dynamodb/shared_room.rs index 4530962..f057fb4 100644 --- a/junowen-server/src/database/dynamodb/shared_room.rs +++ b/junowen-server/src/database/dynamodb/shared_room.rs @@ -1,6 +1,5 @@ -use anyhow::{anyhow, Result}; +use anyhow::Result; use aws_sdk_dynamodb::{error::SdkError, types::AttributeValue}; -use serde_dynamo::from_item; use crate::database::{PutError, SharedRoom, SharedRoomOpponentAnswer, SharedRoomTables}; @@ -16,12 +15,7 @@ impl SharedRoomTables for DynamoDB { .await } - async fn keep_room( - &self, - name: String, - key: String, - ttl_sec: u64, - ) -> Result> { + async fn keep_room(&self, name: String, key: String, ttl_sec: u64) -> Result { let result = self .client .update_item() @@ -36,12 +30,7 @@ impl SharedRoomTables for DynamoDB { .send() .await; match result { - Ok(output) => { - let item = output - .attributes() - .ok_or_else(|| anyhow!("attributes not found"))?; - Ok(Some(from_item(item.to_owned())?)) - } + Ok(_) => Ok(true), Err(error) => { let SdkError::ServiceError(err) = &error else { return Err(error.into()); @@ -49,7 +38,7 @@ impl SharedRoomTables for DynamoDB { if !err.err().is_conditional_check_failed_exception() { return Err(error.into()); } - Ok(None) + Ok(false) } } } diff --git a/junowen-server/src/database/file.rs b/junowen-server/src/database/file.rs index 7ce6bb5..f131fd6 100644 --- a/junowen-server/src/database/file.rs +++ b/junowen-server/src/database/file.rs @@ -53,12 +53,7 @@ impl SharedRoomTables for File { .find(|x: &SharedRoom| x.name == name)) } - async fn keep_room( - &self, - name: String, - key: String, - ttl_sec: u64, - ) -> Result> { + async fn keep_room(&self, name: String, key: String, ttl_sec: u64) -> Result { let mut store = self.read().await?; if store.get("offers").is_none() { store["offers"] = Value::Array(vec![]); @@ -68,7 +63,7 @@ impl SharedRoomTables for File { let offer = serde_json::from_value::((*x).to_owned()).unwrap(); offer.name() == &name && offer.key() == &key }) else { - return Ok(None); + return Ok(false); }; let mut new_offer = serde_json::from_value::(offer.to_owned()).unwrap(); new_offer.ttl_sec = ttl_sec; diff --git a/junowen-server/src/routes/custom.rs b/junowen-server/src/routes/custom.rs index eaa0c16..f8bb965 100644 --- a/junowen-server/src/routes/custom.rs +++ b/junowen-server/src/routes/custom.rs @@ -106,10 +106,9 @@ async fn post_room_keep( if Uuid::parse_str(&key).is_err() { return Ok(PostRoomKeepResponse::BadRequest); } - if db + if !db .keep_room(name.to_owned(), key, ttl_sec(now_sec())) .await? - .is_none() { return Ok(PostRoomKeepResponse::BadRequest); }