Skip to content

Commit

Permalink
Small tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
jayair committed Nov 11, 2020
1 parent f64f667 commit f4d8694
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 55 deletions.
3 changes: 2 additions & 1 deletion services/notes/billing.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export const main = handler(async (event, context) => {
source,
amount,
description,
currency: "usd"
currency: "usd",
});

return { status: true };
});
21 changes: 7 additions & 14 deletions services/notes/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,14 @@ export const main = handler(async (event, context) => {
const data = JSON.parse(event.body);
const params = {
TableName: process.env.tableName,
// 'Item' contains the attributes of the item to be created
// - 'userId': user identities are federated through the
// Cognito Identity Pool, we will use the identity id
// as the user id of the authenticated user
// - 'noteId': a unique uuid
// - 'content': parsed from request body
// - 'attachment': parsed from request body
// - 'createdAt': current Unix timestamp
Item: {
userId: event.requestContext.identity.cognitoIdentityId,
noteId: uuid.v1(),
content: data.content,
attachment: data.attachment,
createdAt: Date.now()
}
// The attributes of the item to be created
userId: event.requestContext.identity.cognitoIdentityId, // The id of the author
noteId: uuid.v1(), // A unique uuid
content: data.content, // Parsed from request body
attachment: data.attachment, // Parsed from request body
createdAt: Date.now(), // Current Unix timestamp
},
};

await dynamoDb.put(params);
Expand Down
8 changes: 3 additions & 5 deletions services/notes/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ export const main = handler(async (event, context) => {
const params = {
TableName: process.env.tableName,
// 'Key' defines the partition key and sort key of the item to be removed
// - 'userId': Identity Pool identity id of the authenticated user
// - 'noteId': path parameter
Key: {
userId: event.requestContext.identity.cognitoIdentityId,
noteId: event.pathParameters.id
}
userId: event.requestContext.identity.cognitoIdentityId, // The id of the author
noteId: event.pathParameters.id, // The id of the note from the path
},
};

await dynamoDb.delete(params);
Expand Down
11 changes: 4 additions & 7 deletions services/notes/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,17 @@ export const main = handler(async (event, context) => {
const params = {
TableName: process.env.tableName,
// 'Key' defines the partition key and sort key of the item to be retrieved
// - 'userId': Identity Pool identity id of the authenticated user
// - 'noteId': path parameter
Key: {
userId: event.requestContext.identity.cognitoIdentityId,
noteId: event.pathParameters.id
}
userId: event.requestContext.identity.cognitoIdentityId, // The id of the author
noteId: event.pathParameters.id, // The id of the note from the path
},
};

const result = await dynamoDb.get(params);
if ( ! result.Item) {
if (!result.Item) {
throw new Error("Item not found.");
}

// Return the retrieved item
return result.Item;
});

6 changes: 1 addition & 5 deletions services/notes/libs/billing-lib.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
export function calculateCost(storage) {
const rate = storage <= 10
? 4
: storage <= 100
? 2
: 1;
const rate = storage <= 10 ? 4 : storage <= 100 ? 2 : 1;

return rate * storage * 100;
}
6 changes: 3 additions & 3 deletions services/notes/libs/dynamodb-lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import AWS from "aws-sdk";
const client = new AWS.DynamoDB.DocumentClient();

export default {
get : (params) => client.get(params).promise(),
put : (params) => client.put(params).promise(),
query : (params) => client.query(params).promise(),
get: (params) => client.get(params).promise(),
put: (params) => client.put(params).promise(),
query: (params) => client.query(params).promise(),
update: (params) => client.update(params).promise(),
delete: (params) => client.delete(params).promise(),
};
9 changes: 4 additions & 5 deletions services/notes/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ export const main = handler(async (event, context) => {
// 'KeyConditionExpression' defines the condition for the query
// - 'userId = :userId': only return items with matching 'userId'
// partition key
// 'ExpressionAttributeValues' defines the value in the condition
// - ':userId': defines 'userId' to be Identity Pool identity id
// of the authenticated user
KeyConditionExpression: "userId = :userId",
// 'ExpressionAttributeValues' defines the value in the condition
// - ':userId': defines 'userId' to be the id of the author
ExpressionAttributeValues: {
":userId": event.requestContext.identity.cognitoIdentityId
}
":userId": event.requestContext.identity.cognitoIdentityId,
},
};

const result = await dynamoDb.query(params);
Expand Down
11 changes: 5 additions & 6 deletions services/notes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@
"url": "https://github.com/AnomalyInnovations/serverless-nodejs-starter.git"
},
"devDependencies": {
"aws-sdk": "^2.768.0",
"jest": "^26.5.2",
"serverless-bundle": "2.0.0",
"serverless-dotenv-plugin": "^2.1.1",
"serverless-offline": "^5.3.3"
"aws-sdk": "^2.773.0",
"serverless-bundle": "3.2.0",
"serverless-dotenv-plugin": "^2.3.2",
"serverless-offline": "^6.1.4"
},
"dependencies": {
"stripe": "^8.107.0",
"stripe": "^8.119.0",
"uuid": "^7.0.3"
}
}
6 changes: 3 additions & 3 deletions services/notes/serverless.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
service: notes-api
service: notes-app-api

# Create an optimized package for our functions
package:
Expand Down Expand Up @@ -28,6 +28,8 @@ provider:
stripeSecretKey: ${env:STRIPE_SECRET_KEY}
tableName: !ImportValue '${self:custom.sstApp}-TableName'

# 'iamRoleStatements' defines the permission policy for the Lambda function.
# In this case Lambda functions are granted with permissions to access DynamoDB.
iamRoleStatements:
- Effect: Allow
Action:
Expand All @@ -47,8 +49,6 @@ functions:
# Defines an HTTP API endpoint that calls the main function in create.js
# - path: url path is /notes
# - method: POST request
# - cors: enabled CORS (Cross-Origin Resource Sharing) for browser cross
# domain api call
# - authorizer: authenticate using the AWS IAM role
create:
handler: create.main
Expand Down
10 changes: 4 additions & 6 deletions services/notes/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,21 @@ export const main = handler(async (event, context) => {
const params = {
TableName: process.env.tableName,
// 'Key' defines the partition key and sort key of the item to be updated
// - 'userId': Identity Pool identity id of the authenticated user
// - 'noteId': path parameter
Key: {
userId: event.requestContext.identity.cognitoIdentityId,
noteId: event.pathParameters.id
userId: event.requestContext.identity.cognitoIdentityId, // The id of the author
noteId: event.pathParameters.id, // The id of the note from the path
},
// 'UpdateExpression' defines the attributes to be updated
// 'ExpressionAttributeValues' defines the value in the update expression
UpdateExpression: "SET content = :content, attachment = :attachment",
ExpressionAttributeValues: {
":attachment": data.attachment || null,
":content": data.content || null
":content": data.content || null,
},
// 'ReturnValues' specifies if and how to return the item's attributes,
// where ALL_NEW returns all attributes of the item after the update; you
// can inspect 'result' below to see how it works with different settings
ReturnValues: "ALL_NEW"
ReturnValues: "ALL_NEW",
};

await dynamoDb.update(params);
Expand Down

0 comments on commit f4d8694

Please sign in to comment.