File tree 14 files changed +234
-22
lines changed
14 files changed +234
-22
lines changed Original file line number Diff line number Diff line change
1
+ import uuid from "uuid" ;
2
+ import * as dynamoDbLib from "./libs/dynamodb-lib" ;
3
+ import { success , failure } from "./libs/response-lib" ;
4
+
5
+ export async function main ( event , context ) {
6
+ const data = JSON . parse ( event . body ) ;
7
+ const params = {
8
+ TableName : process . env . tableName ,
9
+ Item : {
10
+ userId : event . requestContext . identity . cognitoIdentityId ,
11
+ noteId : uuid . v1 ( ) ,
12
+ content : data . content ,
13
+ attachment : data . attachment ,
14
+ createdAt : Date . now ( )
15
+ }
16
+ } ;
17
+
18
+ try {
19
+ await dynamoDbLib . call ( "put" , params ) ;
20
+ return success ( params . Item ) ;
21
+ } catch ( e ) {
22
+ return failure ( { status : false } ) ;
23
+ }
24
+ }
Original file line number Diff line number Diff line change
1
+ import * as dynamoDbLib from "./libs/dynamodb-lib" ;
2
+ import { success , failure } from "./libs/response-lib" ;
3
+
4
+ export async function main ( event , context ) {
5
+ const params = {
6
+ TableName : process . env . tableName ,
7
+ Key : {
8
+ userId : event . requestContext . identity . cognitoIdentityId ,
9
+ noteId : event . pathParameters . id
10
+ }
11
+ } ;
12
+
13
+ try {
14
+ await dynamoDbLib . call ( "delete" , params ) ;
15
+ return success ( { status : true } ) ;
16
+ } catch ( e ) {
17
+ return failure ( { status : false } ) ;
18
+ }
19
+ }
Load Diff This file was deleted.
Original file line number Diff line number Diff line change
1
+ import * as dynamoDbLib from "./libs/dynamodb-lib" ;
2
+ import { success , failure } from "./libs/response-lib" ;
3
+
4
+ export async function main ( event , context ) {
5
+ const params = {
6
+ TableName : process . env . tableName ,
7
+ Key : {
8
+ userId : event . requestContext . identity . cognitoIdentityId ,
9
+ noteId : event . pathParameters . id
10
+ }
11
+ } ;
12
+
13
+ try {
14
+ const result = await dynamoDbLib . call ( "get" , params ) ;
15
+ if ( result . Item ) {
16
+ return success ( result . Item ) ;
17
+ } else {
18
+ return failure ( { status : false , error : "Item not found." } ) ;
19
+ }
20
+ } catch ( e ) {
21
+ return failure ( { status : false } ) ;
22
+ }
23
+ }
Original file line number Diff line number Diff line change
1
+ import AWS from "aws-sdk" ;
2
+
3
+ export function call ( action , params ) {
4
+ const dynamoDb = new AWS . DynamoDB . DocumentClient ( ) ;
5
+
6
+ return dynamoDb [ action ] ( params ) . promise ( ) ;
7
+ }
Original file line number Diff line number Diff line change
1
+ export function success ( body ) {
2
+ return buildResponse ( 200 , body ) ;
3
+ }
4
+
5
+ export function failure ( body ) {
6
+ return buildResponse ( 500 , body ) ;
7
+ }
8
+
9
+ function buildResponse ( statusCode , body ) {
10
+ return {
11
+ statusCode : statusCode ,
12
+ headers : {
13
+ "Access-Control-Allow-Origin" : "*" ,
14
+ "Access-Control-Allow-Credentials" : true
15
+ } ,
16
+ body : JSON . stringify ( body )
17
+ } ;
18
+ }
Original file line number Diff line number Diff line change
1
+ import * as dynamoDbLib from "./libs/dynamodb-lib" ;
2
+ import { success , failure } from "./libs/response-lib" ;
3
+
4
+ export async function main ( event , context ) {
5
+ const params = {
6
+ TableName : process . env . tableName ,
7
+ KeyConditionExpression : "userId = :userId" ,
8
+ ExpressionAttributeValues : {
9
+ ":userId" : event . requestContext . identity . cognitoIdentityId
10
+ }
11
+ } ;
12
+
13
+ try {
14
+ const result = await dynamoDbLib . call ( "query" , params ) ;
15
+ return success ( result . Items ) ;
16
+ } catch ( e ) {
17
+ return failure ( { status : false } ) ;
18
+ }
19
+ }
Original file line number Diff line number Diff line change
1
+ {
2
+ "body" : " {\" content\" :\" hello world\" ,\" attachment\" :\" hello.jpg\" }" ,
3
+ "requestContext" : {
4
+ "identity" : {
5
+ "cognitoIdentityId" : " USER-SUB-1234"
6
+ }
7
+ }
8
+ }
Original file line number Diff line number Diff line change
1
+ {
2
+ "pathParameters" : {
3
+ "id" : " 20e832c0-72a5-11ea-a23d-91417b8ca86b"
4
+ },
5
+ "requestContext" : {
6
+ "identity" : {
7
+ "cognitoIdentityId" : " USER-SUB-1234"
8
+ }
9
+ }
10
+ }
Original file line number Diff line number Diff line change
1
+ {
2
+ "pathParameters" : {
3
+ "id" : " 20e832c0-72a5-11ea-a23d-91417b8ca86b"
4
+ },
5
+ "requestContext" : {
6
+ "identity" : {
7
+ "cognitoIdentityId" : " USER-SUB-1234"
8
+ }
9
+ }
10
+ }
Original file line number Diff line number Diff line change
1
+ {
2
+ "requestContext" : {
3
+ "identity" : {
4
+ "cognitoIdentityId" : " USER-SUB-1234"
5
+ }
6
+ }
7
+ }
Original file line number Diff line number Diff line change
1
+ {
2
+ "body" : " {\" content\" :\" new world\" ,\" attachment\" :\" new.jpg\" }" ,
3
+ "pathParameters" : {
4
+ "id" : " 20e832c0-72a5-11ea-a23d-91417b8ca86b"
5
+ },
6
+ "requestContext" : {
7
+ "identity" : {
8
+ "cognitoIdentityId" : " USER-SUB-1234"
9
+ }
10
+ }
11
+ }
Original file line number Diff line number Diff line change 1
- # NOTE: update this with your service name
2
1
service : serverless-react-notes-app-api
3
2
4
- # Create an optimized package for our functions
5
3
package :
6
4
individually : true
7
5
8
6
plugins :
9
- - serverless-bundle # Package our functions with Webpack
7
+ - serverless-bundle
10
8
- serverless-offline
11
- - serverless-dotenv-plugin # Load .env as environment variables
9
+ - serverless-dotenv-plugin
12
10
13
11
provider :
14
12
name : aws
15
13
runtime : nodejs10.x
16
14
stage : prod
17
15
region : eu-central-1
18
- # To load environment variables externally
19
- # rename env.example to .env and uncomment
20
- # the following line. Also, make sure to not
21
- # commit your .env.
22
- #
23
- # environment:
24
- # SAMPLE_ENV_VAR: ${env:SAMPLE_ENV_VAR}
16
+ environment :
17
+ tableName : notes
18
+ iamRoleStatements :
19
+ - Effect : Allow
20
+ Action :
21
+ - dynamodb:DescribeTable
22
+ - dynamodb:Query
23
+ - dynamodb:Scan
24
+ - dynamodb:GetItem
25
+ - dynamodb:PutItem
26
+ - dynamodb:UpdateItem
27
+ - dynamodb:DeleteItem
28
+ Resource : " arn:aws:dynamodb:eu-central-1:*:*"
25
29
26
30
functions :
27
- hello :
28
- handler : handler.hello
31
+ create :
32
+ handler : create.main
29
33
events :
30
34
- http :
31
- path : hello
35
+ path : notes
36
+ method : post
37
+ cors : true
38
+ authorizer : aws_iam
39
+ get :
40
+ handler : get.main
41
+ events :
42
+ - http :
43
+ path : notes/{id}
44
+ method : get
45
+ cors : true
46
+ authorizer : aws_iam
47
+ list :
48
+ handler : list.main
49
+ events :
50
+ - http :
51
+ path : notes
32
52
method : get
53
+ cors : true
54
+ authorizer : aws_iam
55
+ update :
56
+ handler : update.main
57
+ events :
58
+ - http :
59
+ path : notes/{id}
60
+ method : put
61
+ cors : true
62
+ authorizer : aws_iam
63
+ delete :
64
+ handler : delete.main
65
+ events :
66
+ - http :
67
+ path : notes/{id}
68
+ method : delete
69
+ cors : true
70
+ authorizer : aws_iam
Original file line number Diff line number Diff line change
1
+ import * as dynamoDbLib from "./libs/dynamodb-lib" ;
2
+ import { success , failure } from "./libs/response-lib" ;
3
+
4
+ export async function main ( event , context ) {
5
+ const data = JSON . parse ( event . body ) ;
6
+ const params = {
7
+ TableName : process . env . tableName ,
8
+ Key : {
9
+ userId : event . requestContext . identity . cognitoIdentityId ,
10
+ noteId : event . pathParameters . id
11
+ } ,
12
+ UpdateExpression : "SET content = :content, attachment = :attachment" ,
13
+ ExpressionAttributeValues : {
14
+ ":attachment" : data . attachment || null ,
15
+ ":content" : data . content || null
16
+ } ,
17
+ ReturnValues : "ALL_NEW"
18
+ } ;
19
+
20
+ try {
21
+ await dynamoDbLib . call ( "update" , params ) ;
22
+ return success ( { status : true } ) ;
23
+ } catch ( e ) {
24
+ return failure ( { status : false } ) ;
25
+ }
26
+ }
You can’t perform that action at this time.
0 commit comments