forked from serverless/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.js
More file actions
106 lines (87 loc) · 2.99 KB
/
Copy pathhandler.js
File metadata and controls
106 lines (87 loc) · 2.99 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
const mongoose = require('mongoose');
const Promise = require('bluebird');
const validator = require('validator');
const UserModel = require('./model/User.js');
mongoose.Promise = Promise;
const mongoString = ''; // MongoDB Url
const createErrorResponse = (statusCode, message) => ({
statusCode: statusCode || 501,
headers: { 'Content-Type': 'text/plain' },
body: message || 'Incorrect id',
});
const dbExecute = (db, fn) => db.then(fn).finally(() => db.close());
function dbConnectAndExecute(dbUrl, fn) {
return dbExecute(mongoose.connect(dbUrl, { useMongoClient: true }), fn);
}
module.exports.user = (event, context, callback) => {
if (!validator.isAlphanumeric(event.pathParameters.id)) {
callback(null, createErrorResponse(400, 'Incorrect id'));
return;
}
dbConnectAndExecute(mongoString, () => (
UserModel
.find({ _id: event.pathParameters.id })
.then(user => callback(null, { statusCode: 200, body: JSON.stringify(user) }))
.catch(err => callback(null, createErrorResponse(err.statusCode, err.message)))
));
};
module.exports.createUser = (event, context, callback) => {
const data = JSON.parse(event.body);
const user = new UserModel({
name: data.name,
firstname: data.firstname,
birth: data.birth,
city: data.city,
ip: event.requestContext.identity.sourceIp,
});
if (user.validateSync()) {
callback(null, createErrorResponse(400, 'Incorrect user data'));
return;
}
dbConnectAndExecute(mongoString, () => (
user
.save()
.then(() => callback(null, {
statusCode: 200,
body: JSON.stringify({ id: user.id }),
}))
.catch(err => callback(null, createErrorResponse(err.statusCode, err.message)))
));
};
module.exports.deleteUser = (event, context, callback) => {
if (!validator.isAlphanumeric(event.pathParameters.id)) {
callback(null, createErrorResponse(400, 'Incorrect id'));
return;
}
dbConnectAndExecute(mongoString, () => (
UserModel
.remove({ _id: event.pathParameters.id })
.then(() => callback(null, { statusCode: 200, body: JSON.stringify('Ok') }))
.catch(err => callback(null, createErrorResponse(err.statusCode, err.message)))
));
};
module.exports.updateUser = (event, context, callback) => {
const data = JSON.parse(event.body);
const id = event.pathParameters.id;
if (!validator.isAlphanumeric(id)) {
callback(null, createErrorResponse(400, 'Incorrect id'));
return;
}
const user = new UserModel({
_id: id,
name: data.name,
firstname: data.firstname,
birth: data.birth,
city: data.city,
ip: event.requestContext.identity.sourceIp,
});
if (user.validateSync()) {
callback(null, createErrorResponse(400, 'Incorrect parameter'));
return;
}
dbConnectAndExecute(mongoString, () => (
UserModel.findByIdAndUpdate(id, user)
.then(() => callback(null, { statusCode: 200, body: JSON.stringify('Ok') }))
.catch(err => callback(err, createErrorResponse(err.statusCode, err.message)))
));
};