Skip to content

Commit 8950bb1

Browse files
backup
1 parent ff0e65b commit 8950bb1

File tree

6 files changed

+98
-25
lines changed

6 files changed

+98
-25
lines changed

api/create.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ module.exports.create = (event, context, callback) => {
1212
const requestBody = JSON.parse(event.body); // User submitted data
1313

1414
// Grab the individual elements of the post.
15+
const userId = requestBody.userId;
16+
const parentId = requestBody.parentId;
17+
const correctAnswer = requestBody.correctAnswer;
1518
const title = requestBody.title;
1619
const body = requestBody.body;
17-
const user_id = requestBody.user_id;
18-
const parent_id = requestBody.parent_id;
19-
2020

2121
/**
2222
* Save the post to permanent storage
@@ -29,7 +29,7 @@ module.exports.create = (event, context, callback) => {
2929
console.log('Submitting post');
3030

3131
const postDetail = {
32-
TableName: process.env.POST_TABLE,
32+
TableName: process.env.DYNAMODB_TABLE,
3333
Item: post,
3434
};
3535

@@ -48,20 +48,22 @@ module.exports.create = (event, context, callback) => {
4848
const timestamp = new Date().getTime();
4949
return {
5050
id: uuid.v1(),
51-
user_id: data.user_id,
52-
parent_id: data.parent_id,
51+
userId: data.userId,
52+
parentId: data.parentId,
53+
correctAnswer: data.correctAnswer,
5354
title: data.title,
5455
body: data.body,
5556
createdAt: timestamp,
56-
updatedAt: timestamp
57+
updatedAt: timestamp,
58+
dummyHashKey: 'OK'
5759
};
5860
};
5961

6062
// Validate the submitted data
6163
if (
6264
typeof title !== 'string' ||
6365
typeof body !== 'string' ||
64-
typeof user_id !== 'number'
66+
typeof userId !== 'number'
6567
) {
6668
console.error('Validation Failed');
6769
callback(new Error('Couldn\'t submit post because of validation errors.'));

api/delete.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const dynamoDb = new AWS.DynamoDB.DocumentClient();
77
module.exports.delete = (event, context, callback) => {
88

99
const params = {
10-
TableName: process.env.POST_TABLE,
10+
TableName: process.env.DYNAMODB_TABLE,
1111
Key: {
1212
id: event.pathParameters.id,
1313
},

api/get.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const dynamoDb = new AWS.DynamoDB.DocumentClient();
77
module.exports.get = (event, context, callback) => {
88

99
const params = {
10-
TableName: process.env.POST_TABLE,
10+
TableName: process.env.DYNAMODB_TABLE,
1111
Key: {
1212
id: event.pathParameters.id,
1313
},

api/list.js

+59-9
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,76 @@
11
'use strict';
22

33
const AWS = require('aws-sdk'); // eslint-disable-line import/no-extraneous-dependencies
4-
54
const dynamoDb = new AWS.DynamoDB.DocumentClient();
5+
6+
/*
7+
var params = {
8+
TableName: process.env.DYNAMODB_TABLE,
9+
Limit: 10
10+
};
11+
612
var params = {
7-
TableName: process.env.POST_TABLE,
8-
Limit: 10
13+
14+
TableName: process.env.DYNAMODB_TABLE,
15+
IndexName: "UpdatedAtIndex",
16+
ScanIndexForward: true
917
};
18+
*/
19+
20+
const params = {
21+
TableName: process.env.DYNAMODB_TABLE,
22+
Limit: 5,
23+
IndexName: 'UpdatedAtIndex',
24+
KeyConditionExpression: 'dummyHashKey = :x',
25+
ExpressionAttributeValues: {
26+
':x': 'OK'
27+
},
28+
ProjectionExpression: "id, userId, parentId, correctAnswer, title, body, createdAt, updatedAt"
29+
}
30+
1031

1132
module.exports.list = (event, context, callback) => {
1233

34+
// Do we have any parameters?
35+
/*
1336
if( event.queryStringParameters !== null && typeof event.queryStringParameters === 'object' ) {
1437
15-
if( event.queryStringParameters.hasOwnProperty('limit') ) {
38+
// Do we have a limit clause?
39+
if( event.queryStringParameters.hasOwnProperty('limit') ) params.Limit = event.queryStringParameters.limit;
1640
17-
params.Limit = event.queryStringParameters.limit;
18-
}
41+
// Do we have a page number?
1942
}
43+
*/
44+
dynamoDb.query(params, function(error, data) {
45+
46+
// Handle potential errors
47+
if (error) {
48+
49+
console.error(error);
50+
callback(null, {
51+
statusCode: error.statusCode || 501,
52+
headers: { 'Content-Type': 'text/plain' },
53+
body: 'Couldn\'t fetch the posts.',
54+
});
55+
56+
return;
57+
}
58+
59+
// Create a response
60+
const response = {
61+
statusCode: 200,
62+
body: JSON.stringify(data),
63+
};
64+
65+
callback(null, response);
66+
67+
});
2068

21-
// fetch all posts from the database
69+
// Fetch all posts from the database
70+
/*
2271
dynamoDb.scan(params, (error, result) => {
2372
24-
// handle potential errors
73+
// Handle potential errors
2574
if (error) {
2675
2776
console.error(error);
@@ -34,12 +83,13 @@ module.exports.list = (event, context, callback) => {
3483
return;
3584
}
3685
37-
// create a response
86+
// Create a response
3887
const response = {
3988
statusCode: 200,
4089
body: JSON.stringify(result.Items),
4190
};
4291
4392
callback(null, response);
4493
});
94+
*/
4595
};

api/update.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@ module.exports.update = (event, context, callback) => {
2626
}
2727

2828
const params = {
29-
TableName: process.env.POST_TABLE,
29+
TableName: process.env.DYNAMODB_TABLE,
3030
Key: {
3131
id: event.pathParameters.id,
3232
},
3333
ExpressionAttributeValues: {
3434
':title': data.title,
3535
':body': data.body,
36-
':updatedAt': timestamp,
36+
':updated_at': timestamp,
3737
},
38-
UpdateExpression: 'SET title = :title, body = :body, updatedAt = :updatedAt',
38+
UpdateExpression: 'SET title = :title, body = :body, updated_at = :updated_at',
3939
ReturnValues: 'ALL_NEW',
4040
};
4141

serverless.yml

+24-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ provider:
4040
# - "Ref" : "ServerlessDeploymentBucket"
4141
# - "/*"
4242
environment:
43-
POST_TABLE: ${self:service}-${opt:stage, self:provider.stage}
43+
DYNAMODB_TABLE: posts
4444
iamRoleStatements:
4545
- Effect: Allow
4646
Action:
@@ -169,14 +169,35 @@ resources:
169169
AttributeDefinitions:
170170
-
171171
AttributeName: "id"
172-
AttributeType: "S"
172+
AttributeType: "S"
173+
-
174+
AttributeName: "updatedAt"
175+
AttributeType: "N"
176+
-
177+
AttributeName: "dummyHashKey"
178+
AttributeType: "S"
173179
KeySchema:
174180
-
175181
AttributeName: "id"
176182
KeyType: "HASH"
183+
GlobalSecondaryIndexes:
184+
-
185+
IndexName: UpdatedAtIndex
186+
KeySchema:
187+
-
188+
AttributeName: "dummyHashKey"
189+
KeyType: HASH
190+
-
191+
AttributeName: "updatedAt"
192+
KeyType: RANGE
193+
Projection:
194+
ProjectionType: KEYS_ONLY
195+
ProvisionedThroughput:
196+
ReadCapacityUnits: 1
197+
WriteCapacityUnits: 1
177198
ProvisionedThroughput:
178199
ReadCapacityUnits: 1
179200
WriteCapacityUnits: 1
180201
StreamSpecification:
181202
StreamViewType: "NEW_AND_OLD_IMAGES"
182-
TableName: ${self:provider.environment.POST_TABLE}
203+
TableName: ${self:provider.environment.DYNAMODB_TABLE}

0 commit comments

Comments
 (0)