-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler_guest.js
88 lines (73 loc) · 2.1 KB
/
handler_guest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
'use strict';
const uuid = require('uuid');
const AWS = require('aws-sdk');
AWS.config.setPromisesDependency(require('bluebird'));
const dynamoDb = new AWS.DynamoDB.DocumentClient();
/* POST /guests */
module.exports.submitGuest = async (event) => {
const requestBody = JSON.parse(event.body);
const {
firstName,
lastName,
responded
} = requestBody
if (typeof firstName !== 'string' || typeof lastName !== 'string' || typeof responded !== 'boolean') {
return buildResponse(400, 'Validation failed. Invalid input type(s)');
}
const guest = buildGuestItem(firstName, lastName, responded);
await putGuest(guest);
return buildResponse(200, guest)
}
/* Puts guest in dynamo table */
const putGuest = async function putGuestInDynamo(guest) {
const guestItem = {
TableName: process.env.GUESTS_TABLE,
Item: guest,
};
await dynamoDb.put(guestItem).promise();
return guest;
}
/* Creates a guest item */
const buildGuestItem = function buildGuestItemDynamoModel(firstName, lastName, responded) {
return {
id: uuid.v1(),
'firstName': firstName,
'lastName': lastName,
'responded': responded,
};
};
/* GET /guests */
module.exports.listGuests = async (event) => {
let params = {
TableName: process.env.GUESTS_TABLE,
ProjectionExpression: "id, firstName, lastName, responded"
};
const data = await dynamoDb.scan(params).promise();
return buildResponse(200, data.Items)
}
/*module.exports.deleteGuest = async (event) => {
let uuid = `${event.pathParameters.uuid}`;
let params = {
TableName: process.env.GUESTS_TABLE,
Key: {
firstName: {"S": "Sally"},
lastName: {"S": "Seashells"},
responded: {"BOOL": false},
'id': {'S': [uuid]}
}
}
await dynamoDb.delete(params).promise()
return buildResponse(200, `This works ${uuid}`)
}*/
const buildResponse = function buildHttpResponse(statusCode, message) {
return {
statusCode: statusCode,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'OPTIONS, POST, GET',
},
body: JSON.stringify({
message: message
})
}
}