-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
153 lines (145 loc) · 4.21 KB
/
index.js
File metadata and controls
153 lines (145 loc) · 4.21 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
const AWS = require('aws-sdk');
AWS.config.update( {
region: 'us-east-2'
});
const dynamodb = new AWS.DynamoDB.DocumentClient();
const dynamodbTableName = 'contact-list';
const healthPath = '/health';
const contactPath = '/contact';
const contactsPath = '/contacts';
exports.handler = async function(event) {
console.log('Request event: ', event);
let response;
switch(true) {
case event.httpMethod === 'GET' && event.path === healthPath:
response = buildResponse(200);
break;
case event.httpMethod === 'GET' && event.path === contactPath:
response = await getContact(event.queryStringParameters.contactId);
break;
case event.httpMethod === 'GET' && event.path === contactsPath:
response = await getContacts();
break;
case event.httpMethod === 'POST' && event.path === contactPath:
response = await saveContact(JSON.parse(event.body));
break;
case event.httpMethod === 'PATCH' && event.path === contactPath:
const requestBody = JSON.parse(event.body);
response = await modifyContact(requestBody.contactId, requestBody.updateKey, requestBody.updateValue);
break;
case event.httpMethod === 'DELETE' && event.path === contactPath:
response = await deleteContact(JSON.parse(event.body).contactId);
break;
default:
response = buildResponse(404, '404 Not Found');
}
return response;
}
// Get a single contact from DynamoDB table
async function getContact(contactId) {
const params = {
TableName: dynamodbTableName,
Key: {
'contactId': contactId
}
}
return await dynamodb.get(params).promise().then((response) => {
return buildResponse(200, response.Item);
}, (error) => {
console.error('Error getting contact: ', error);
});
}
// Get all contacts from DynamoDB table
async function getContacts() {
const params = {
TableName: dynamodbTableName
}
const allContacts = await scanDynamoRecords(params, []);
const body = {
contacts: allContacts
}
return buildResponse(200, body);
}
async function scanDynamoRecords(scanParams, itemArray) {
try {
const dynamoData = await dynamodb.scan(scanParams).promise();
itemArray = itemArray.concat(dynamoData.Items);
if (dynamoData.LastEvaluatedKey) {
scanParams.ExclusiveStartkey = dynamoData.LastEvaluatedKey;
return await scanDynamoRecords(scanParams, itemArray);
}
return itemArray;
} catch(error) {
console.error('Error getting contacts: ', error);
}
}
// Add a new contact to DynamoDB table
async function saveContact(requestBody) {
const params = {
TableName: dynamodbTableName,
Item: requestBody
}
return await dynamodb.put(params).promise().then(() => {
const body = {
Operation: 'SAVE',
Message: 'SUCCESS',
Item: requestBody
}
return buildResponse(200, body);
}, (error) => {
console.error('Error saving contact: ', error);
})
}
// Modify/update a single contact in DynamoDB table
async function modifyContact(contactId, updateKey, updateValue) {
const params = {
TableName: dynamodbTableName,
Key: {
'contactId': contactId
},
UpdateExpression: `set ${updateKey} = :value`,
ExpressionAttributeValues: {
':value': updateValue
},
ReturnValues: 'UPDATED_NEW'
}
return await dynamodb.update(params).promise().then((response) => {
const body = {
Operation: 'UPDATE',
Message: 'SUCCESS',
UpdatedAttributes: response
}
return buildResponse(200, body);
}, (error) => {
console.error('Error modifying contact: ', error);
})
}
// Delete a single contact from DynamoDB table
async function deleteContact(contactId) {
const params = {
TableName: dynamodbTableName,
Key: {
'contactId': contactId
},
ReturnValues: 'ALL_OLD'
}
return await dynamodb.delete(params).promise().then((response) => {
const body = {
Operation: 'DELETE',
Message: 'SUCCESS',
Item: response
}
return buildResponse(200, body);
}, (error) => {
console.error('Error deleting contact: ', error);
})
}
function buildResponse(statusCode, body) {
return {
statusCode: statusCode,
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
}
}