-
Notifications
You must be signed in to change notification settings - Fork 0
/
userRouter.js
77 lines (65 loc) · 1.84 KB
/
userRouter.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
const { UserSchema } = require("./models/userModel.js");
const getUser = async (event, context, callback) => {
context.callbackWaitsForEmptyEventLoop = false;
try {
const users = await UserSchema.findAll();
const response = { status: 200, body: { users } };
if (response !== null) {
callback(null, response);
}
} catch (error) {
console.log(error);
}
};
const addUser = async (event, context, callback) => {
context.callbackWaitsForEmptyEventLoop = false;
try {
const { correo, contrasena } = JSON.parse(event.body);
const newUser = { correo, contrasena };
const user = await UserSchema.create(newUser);
if (user !== null) {
callback(null, user);
}
} catch (error) {
callback(error);
}
};
const updateUser = async (event, context, callback) => {
context.callbackWaitsForEmptyEventLoop = false;
try {
const { id } = event.pathParameters;
const { correo, contrasena } = JSON.parse(event.body);
const newUser = { correo, contrasena };
const user = await UserSchema.findByPk(id);
const response = await user.update(newUser);
if (response !== null) {
callback(null, response);
}
} catch (error) {
callback(error);
}
};
const deleteUser = async (event, context, callback) => {
context.callbackWaitsForEmptyEventLoop = false;
try {
const { id } = event.pathParameters;
const user = await UserSchema.findByPk(id);
const response = await user.destroy();
callback(null, response);
} catch (error) {
callback(error);
}
};
const updateRecordUser = async (event) => {
const { id } = event.pathParameters;
const newUserData = JSON.parse(event.body);
const user = service.updateRecord(id, newUserData);
return { status: 200, body: { user } };
};
module.exports = {
getUser,
addUser,
updateUser,
updateRecordUser,
deleteUser,
};