forked from waggertron/noderpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmethods.js
80 lines (69 loc) · 2.5 KB
/
methods.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
'use strict';
let db = require('./db');
let methods = {
createUser: {
description: `creates a new user, and returns the details of the new user`,
params: ['user:the user object'],
returns: ['user'],
exec(userObj) {
return new Promise((resolve) => {
if (typeof (userObj) !== 'object') {
throw new Error('was expecting an object!');
}
// you would usually do some validations here
// and check for required fields
// attach an id the save to db
let _userObj = JSON.parse(JSON.stringify(userObj));
_userObj.id = (Math.random() * 10000000) | 0;
resolve(db.users.save(userObj));
});
}
},
fetchUser: {
description: `fetches the user of the given id`,
params: ['id:the id of the user were looking for'],
returns: ['user'],
exec(userObj) {
return new Promise((resolve) => {
if (typeof (userObj) !== 'object') {
throw new Error('was expecting an object!');
}
// you would usually do some validations here
// and check for required fields
// fetch
resolve(db.users.fetch(userObj.id) || {});
});
}
},
fetchAllUsers: {
description: `fetches the entire list of users`,
params: [],
returns: ['userscollection'],
exec() {
return new Promise((resolve) => {
// fetch
resolve(db.users.fetchAll() || {});
});
}
},
createTask: {
description: `creates a new user, and returns the details of the new user`,
params: ['task:the task object'],
returns: ['task'],
exec(taskObj) {
return new Promise((resolve) => {
// you would usually do some validations here
// and check for required fields
// create a task object, then save it to db
// attach an id the save to db
let task = {
userId: taskObj.userId,
taskContent: taskObj.taskContent,
expiry: taskObj.expiry
};
resolve(db.tasks.save(task));
});
}
}
};
module.exports = methods;