-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
180 lines (160 loc) · 4.75 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
*
* project name : ax-lambda
* project author : mindula dilthushan
* author email : minduladilthushan1@gmail.com
*
*/
// import AWS from 'aws-sdk';
const AWS = require('aws-sdk');
const dotEnv = require('dotenv');
const {USER, USERS} = require("./api/ax.api");
//dot env config
dotEnv.config();
const env = process.env;
//configuration
AWS.config.update({
region: 'us-west-1'
});
//dynamodb client
const doClient = new AWS.DynamoDB.DocumentClient();
const dbTableAx = `${env.TABLE_NAME}`
//routes
const userPath = `${USER}`;
const usersPath = `${USERS}`;
//handler
exports.handler = async function (event) {
console.log('Request event => ', event);
let response;
switch (true) {
case event.httpMethod === 'GET' && event.path === userPath:
response = await getUser(event.queryStringParameters.MemberId);
break;
case event.httpMethod === 'GET' && event.path === usersPath:
response = await getUsers();
break;
case event.httpMethod === 'POST' && event.path === userPath:
response = await saveUser(JSON.parse(event.body));
break;
case event.httpMethod === 'PATCH' && event.path === userPath:
const requestBody = JSON.parse(event.body);
response = await updateUser(requestBody.MemberId, requestBody.updateKey, requestBody.updateValue);
break;
case event.httpMethod === 'DELETE' && event.path === userPath:
response = await deleteUser(JSON.parse(event.body).MemberId);
break;
default:
response = axResponse(404, '404 Not Found');
}
return response;
}
//search user
async function getUser(id) {
const params = {
TableName: dbTableAx,
Key: {
'id': id
}
}
return await doClient.get(params).promise().then((response) => {
return axResponse(200, response.Item);
}, (error) => {
console.error('Do your custom error handling here. I am just gonna log it: ', error);
});
}
//search all users
async function getUsers() {
const params = {
TableName: dbTableAx
}
const allUsers = await scanDbRecords(params, []);
const body = {
users: allUsers
}
return axResponse(200, body);
}
//check users in db records
async function scanDbRecords(scanParams, itemArray) {
try {
const scanData = await doClient.scan(scanParams).promise();
itemArray = itemArray.concat(scanData.Items);
if (scanData.LastEvaluatedKey) {
scanParams.ExclusiveStartkey = scanData.LastEvaluatedKey;
return await scanDbRecords(scanParams, itemArray);
}
return itemArray;
} catch (error) {
console.error('Do your custom error handling here. I am just gonna log it: ', error);
}
}
//save user
async function saveUser(requestBody) {
const params = {
TableName: dbTableAx,
Item: requestBody
}
return await doClient.put(params).promise().then(() => {
const body = {
Operation: 'SAVE',
Message: 'USER SAVE SUCCESS!',
Item: requestBody
}
return axResponse(200, body);
}, (error) => {
console.error('Do your custom error handling here. I am just gonna log it: ', error);
})
}
//update user
async function updateUser(id, updateKey, updateValue) {
const params = {
TableName: dbTableAx,
Key: {
'id': id
},
UpdateExpression: `set ${updateKey} = :value`,
ExpressionAttributeValues: {
':value': updateValue
},
ReturnValues: 'UPDATED_NEW'
}
return await doClient.update(params).promise().then((response) => {
const body = {
Operation: 'UPDATE',
Message: 'SUCCESS',
UpdatedAttributes: response
}
return axResponse(200, body);
}, (error) => {
console.error('Do your custom error handling here. I am just gonna log it: ', error);
})
}
//delete user
async function deleteUser(id) {
const params = {
TableName: dbTableAx,
Key: {
'id': id
},
ReturnValues: 'ALL_OLD'
}
return await doClient.delete(params).promise().then((response) => {
const body = {
Operation: 'DELETE',
Message: 'USER DELETE SUCCESS!',
Item: response
}
return axResponse(200, body);
}, (error) => {
console.error('Do your custom error handling here. I am just gonna log it: ', error);
})
}
//response handling
function axResponse(statusCode, body) {
return {
statusCode: statusCode,
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
}
}