Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
progre committed Nov 26, 2023
1 parent 5cffadd commit 2a89db5
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 37 deletions.
7 changes: 1 addition & 6 deletions junowen-server/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<SharedRoom>>;
async fn keep_room(
&self,
name: String,
key: String,
ttl_sec: u64,
) -> Result<Option<SharedRoom>>;
async fn keep_room(&self, name: String, key: String, ttl_sec: u64) -> Result<bool>;
async fn remove_room(&self, name: String, key: Option<String>) -> Result<bool>;

async fn put_room_opponent_answer(
Expand Down
30 changes: 23 additions & 7 deletions junowen-server/src/database/dynamodb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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(
Expand Down
19 changes: 4 additions & 15 deletions junowen-server/src/database/dynamodb/shared_room.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand All @@ -16,12 +15,7 @@ impl SharedRoomTables for DynamoDB {
.await
}

async fn keep_room(
&self,
name: String,
key: String,
ttl_sec: u64,
) -> Result<Option<SharedRoom>> {
async fn keep_room(&self, name: String, key: String, ttl_sec: u64) -> Result<bool> {
let result = self
.client
.update_item()
Expand All @@ -36,20 +30,15 @@ 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());
};
if !err.err().is_conditional_check_failed_exception() {
return Err(error.into());
}
Ok(None)
Ok(false)
}
}
}
Expand Down
9 changes: 2 additions & 7 deletions junowen-server/src/database/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<SharedRoom>> {
async fn keep_room(&self, name: String, key: String, ttl_sec: u64) -> Result<bool> {
let mut store = self.read().await?;
if store.get("offers").is_none() {
store["offers"] = Value::Array(vec![]);
Expand All @@ -68,7 +63,7 @@ impl SharedRoomTables for File {
let offer = serde_json::from_value::<SharedRoom>((*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::<SharedRoom>(offer.to_owned()).unwrap();
new_offer.ttl_sec = ttl_sec;
Expand Down
3 changes: 1 addition & 2 deletions junowen-server/src/routes/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 2a89db5

Please sign in to comment.