-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-nodejs-01.txt
More file actions
102 lines (98 loc) · 2.9 KB
/
api-nodejs-01.txt
File metadata and controls
102 lines (98 loc) · 2.9 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
var models = require('./models')
var auth_roles = require('../auth_module/models').roles
var User = require('../auth_module/models').User
var auth_middleware = require('../auth_module/middlewares');
var $handler = require('../toolkit/db').$handler
var $404 = require('../toolkit/db').$404;
module.exports = {
routes: {
'/': {
method: ['post','get'],
middleware: [auth_middleware.has_role(auth_roles.SUPERUSER)],
callback: function (req,res) {
if(req.method == 'POST') {
var body = req.body;
body.owner = req.user;
body.created = new Date();
models.Dashboard(body).save(
$handler(res, function(dashboard) {
req.user.dashboards.push(dashboard.id);
req.user.save($handler(res, function(body){
res.sendStatus(200);
}))
})
);
}
if(req.method == 'GET') {
models.Dashboard.find({owner: req.user}, $handler(res, function(body) {
res.status(200).send({dashboars: body, user_id: req.user.id});
}))
}
}
},
'/:dash_id': {
method: ['get', 'put', 'delete'],
middleware: [auth_middleware.is_authenticated],
callback: function (req,res) {
if(req.method == 'GET') {
models.Dashboard
.findById(req.params.dash_id)
.exec(res, $handler(res,
$404(function(body){
models.Widget
.find({dashboard: body})
.populate('template')
.exec($handler(res, function(widgets) {
body = body.toObject()
body.widgets = widgets;
res.send(body);
}))
})))
}
if(req.method == 'PUT') {
var _params = {_id: req.params.dash_id},
_body = req.body;
_body.updated = new Date();
models.Dashboard.update(_params, _body, $handler(res, function(body) {
res.status(200).send(body);
}))
}
if(req.method == 'DELETE') {
models.Dashboard.findByIdAndRemove(req.params.dash_id, $handler(res, function(body) {
res.status(200).send(body)
}))
}
}
},
'/:dash_id/widgets': {
method: ['delete', 'post', 'put', 'get'],
middleware: [auth_middleware.has_role([auth_roles.SUPERUSER])],
callback: function(req, res) {
var widget_id = req.query.widget_id;
var dash_id = req.params.dash_id;
if(req.method == 'GET') {
models.Widget.find({dashboard:dash_id}, $handler(res, $404(function(widgets){
res.send({data: widgets})
})))
}
if(req.method == 'DELETE') {
models.Widget.findByIdAndRemove(widget_id, $handler(res, function(body) {
res.status(201).send(body)
}))
}
if(req.method == 'POST') {
var body = req.body;
debugger;
body.create = new Date();
models.Dashboard.findById(dash_id, $handler(res, $404(function(dashboard) {
var widget = models.Widget(body);
widget.dashboard = dashboard.id;
widget.save($handler(res,function(widget) {
res.send(widget);
}))
})))
}
}
},
}
}