Skip to content
This repository has been archived by the owner on Apr 3, 2019. It is now read-only.

Commit

Permalink
use derivation to get QueryInput
Browse files Browse the repository at this point in the history
  • Loading branch information
mockersf committed Dec 21, 2017
1 parent 93bd775 commit 79b4e75
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 31 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ serde_derive = "*"
serde_json = "*"
serde_urlencoded = "*"
serde_dynamodb = { git = "https://github.com/mockersf/serde_dynamodb" }
serde_dynamodb_derive = { git = "https://github.com/mockersf/serde_dynamodb" }

failure = "*"

Expand Down
45 changes: 15 additions & 30 deletions src/api/todo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ use uuid;
use serde::ser::{Serialize, Serializer, SerializeStruct};
use failure::{Error, Fail};
use serde_dynamodb;
use serde_dynamodb::ToQueryInput;

use rusoto_core::{DefaultCredentialsProvider, Region};
use rusoto_dynamodb::{DynamoDb, DynamoDbClient, QueryInput, PutItemInput};
use rusoto_dynamodb::{DynamoDb, DynamoDbClient, PutItemInput};
use rusoto_core::default_tls_client;

use model;
Expand Down Expand Up @@ -59,38 +60,26 @@ struct NotFound {
id: String,
}

#[derive(Serialize, Debug)]
struct TodoFilter {
#[serde(rename = ":uid")]
user_id: Option<String>,
#[serde(rename = ":id")]
id: Option<String>,
}

pub fn list(
event: &crowbar::Value,
_context: &crowbar::LambdaContext,
) -> crowbar::LambdaResult<crowbar::ApiGatewayResponse<model::api::ItemList>> {
let table = env::var("table").unwrap();
let provider = DefaultCredentialsProvider::new().unwrap();
let client = DynamoDbClient::new(default_tls_client().unwrap(), provider, Region::UsEast1);
let uid_filter = TodoFilter {
user_id: Some(
let uid_filter = model::basic_item::BasicItemQueryInput {
uid: Some(
event["requestContext"]["authorizer"]["user_id"]
.as_str()
.unwrap()
.to_string(),
.to_string()
.into(),
),
id: None,
};
let query_input = QueryInput {
table_name: table,
expression_attribute_values: Some(serde_dynamodb::to_hashmap(&uid_filter).unwrap()),
key_condition_expression: Some("uid = :uid".to_string()),
..Default::default()
};

let query_output: Vec<model::basic_item::BasicItem> = client
.query(&query_input)
.query(&uid_filter.to_query_input(table))
.unwrap()
.items
.unwrap_or_else(|| vec![])
Expand Down Expand Up @@ -212,25 +201,21 @@ pub fn get(
let table = env::var("table").unwrap();
let provider = DefaultCredentialsProvider::new().unwrap();
let client = DynamoDbClient::new(default_tls_client().unwrap(), provider, Region::UsEast1);
let user_id_filter = TodoFilter {
user_id: Some(
let todo_filter = model::basic_item::BasicItemQueryInput {
uid: Some(
event["requestContext"]["authorizer"]["user_id"]
.as_str()
.unwrap()
.to_string(),
.to_string()
.into(),
),
id: Some(todo_id.clone()),
};

let query_input = QueryInput {
table_name: table,
expression_attribute_values: Some(serde_dynamodb::to_hashmap(&user_id_filter).unwrap()),
key_condition_expression: Some("uid = :uid and id = :id".to_string()),
id: Some(todo_id.clone().into()),
..Default::default()
};

let query_output: Option<model::basic_item::BasicItem> =
client
.query(&query_input)
.query(&todo_filter.to_query_input(table))
.unwrap()
.items
.unwrap_or_else(|| vec![])
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ extern crate serde_derive;
extern crate serde_json;
extern crate serde_urlencoded;
extern crate serde_dynamodb;
#[macro_use]
extern crate serde_dynamodb_derive;

extern crate uuid;
extern crate frank_jwt;
Expand Down
6 changes: 5 additions & 1 deletion src/model/domain/basic_item.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use rusoto_dynamodb::QueryInput;
use serde_dynamodb;
use serde_dynamodb::ToQueryInput;

use super::*;

#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, ToQueryInput)]
pub struct BasicItem {
pub uid: super::super::UserId,
pub id: ItemId,
Expand Down

0 comments on commit 79b4e75

Please sign in to comment.