-
Notifications
You must be signed in to change notification settings - Fork 52
/
index.js
148 lines (140 loc) · 4.17 KB
/
index.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
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
const AWS = require('aws-sdk');
AWS.config.update( {
region: 'us-east-1'
});
const dynamodb = new AWS.DynamoDB.DocumentClient();
const dynamodbTableName = 'product-inventory';
const healthPath = '/health';
const productPath = '/product';
const productsPath = '/products';
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 === productPath:
response = await getProduct(event.queryStringParameters.productId);
break;
case event.httpMethod === 'GET' && event.path === productsPath:
response = await getProducts();
break;
case event.httpMethod === 'POST' && event.path === productPath:
response = await saveProduct(JSON.parse(event.body));
break;
case event.httpMethod === 'PATCH' && event.path === productPath:
const requestBody = JSON.parse(event.body);
response = await modifyProduct(requestBody.productId, requestBody.updateKey, requestBody.updateValue);
break;
case event.httpMethod === 'DELETE' && event.path === productPath:
response = await deleteProduct(JSON.parse(event.body).productId);
break;
default:
response = buildResponse(404, '404 Not Found');
}
return response;
}
async function getProduct(productId) {
const params = {
TableName: dynamodbTableName,
Key: {
'productId': productId
}
}
return await dynamodb.get(params).promise().then((response) => {
return buildResponse(200, response.Item);
}, (error) => {
console.error('Do your custom error handling here. I am just gonna log it: ', error);
});
}
async function getProducts() {
const params = {
TableName: dynamodbTableName
}
const allProducts = await scanDynamoRecords(params, []);
const body = {
products: allProducts
}
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('Do your custom error handling here. I am just gonna log it: ', error);
}
}
async function saveProduct(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('Do your custom error handling here. I am just gonna log it: ', error);
})
}
async function modifyProduct(productId, updateKey, updateValue) {
const params = {
TableName: dynamodbTableName,
Key: {
'productId': productId
},
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('Do your custom error handling here. I am just gonna log it: ', error);
})
}
async function deleteProduct(productId) {
const params = {
TableName: dynamodbTableName,
Key: {
'productId': productId
},
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('Do your custom error handling here. I am just gonna log it: ', error);
})
}
function buildResponse(statusCode, body) {
return {
statusCode: statusCode,
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
}
}