-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
117 lines (105 loc) · 2.93 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
'use strict';
const serverless = require('serverless-http');
const bodyParser = require('body-parser');
const express = require('express');
const app = express();
const AWS = require('aws-sdk');
const USERS_TABLE = process.env.USERS_TABLE;
const dynamoDb = new AWS.DynamoDB.DocumentClient();
app.use(bodyParser.json({ strict: false }));
app.get('/', function(req, res) {
res.send('Hello World!');
});
app.get('/users/:email', async function(req, res) {
const params = {
TableName: USERS_TABLE,
Key: {
email: req.params.email
}
};
console.log('Params ', params);
try {
const result = await dynamoDb.get(params).promise();
console.log('Result ', result);
if (result && result.Item) {
const { email, username } = result.Item;
res.json({ email, username });
} else {
res.status(404).json({ error: 'User not found' });
}
} catch (error) {
console.log('Error ', error);
if (error) {
console.log(error);
res.status(400).json({ error: 'Could not get user' });
}
}
});
app.post('/users', async function(req, res) {
const { email, username } = req.body;
if (typeof email !== 'string') {
res.status(400).json({ error: '"email" must be a string' });
} else if (typeof username !== 'string') {
res.status(400).json({ error: '"username" must be a string' });
}
const params = {
TableName: USERS_TABLE,
Item: {
email,
username
}
};
const { error } = await dynamoDb.put(params).promise();
if (error) {
console.log(error);
res.status(400).json({ error: 'Could not create user' });
}
res.json({ email, username });
});
app.put('/users', async (req, res) => {
const { email, username } = req.body;
console.log('Email ', email);
console.log('username ', username);
const params = {
TableName: USERS_TABLE,
Key: { email },
UpdateExpression: 'set username = :username',
ExpressionAttributeValues: { ':username': username },
ReturnValues: 'UPDATED_NEW'
};
console.log(params);
try {
const result = await dynamoDb.update(params).promise();
console.log(result);
res.json({ message: 'Successfully modified row', result });
} catch (err) {
console.error(err);
res.json({ message: err });
}
});
app.delete('/users/:emailId', async (req, res) => {
const { emailId } = req.params;
console.log('Email Id ', emailId);
var params = {
TableName: USERS_TABLE,
Key: {
email: emailId
},
ConditionExpression: 'email <= :email',
ExpressionAttributeValues: {
':email': emailId
}
};
try {
const data = await dynamoDb.delete(params).promise();
console.log('DeleteItem succeeded:', JSON.stringify(data, null, 2));
res.json({ message: 'deleted successfully' });
} catch (err) {
console.error(
'Unable to delete item. Error JSON:',
JSON.stringify(err, null, 2)
);
res.json({ message: err });
}
});
module.exports.handler = serverless(app);