-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
82 lines (57 loc) · 1.85 KB
/
api.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
var express = require('express'),
Bourne = require('bourne'),
bodyParser = require('body-parser'),
db = new Bourne('data.json'),
router = express.Router();
router
.use(bodyParser.json())
.route('/contact')
.get(function (req, res) {
// TODO: fix workaround of not existing user.id
if (req.user) {
db.find({userId: parseInt(req.user.id, 10)}, function (err, data) {
res.json(data);
});
} else if (req.session.userId) {
db.find({ userId: req.session.userId }, function (err, data) {
res.json(data);
});
} else {
db.find({userId: parseInt(1, 10)}, function (err, data) {
res.json(data);
});
}
// TODO_END
})
.post(function (req, res) {
var contact = req.body;
contact.userId = req.user.id;
db.insert(contact, function (err, data) {
res.json(data);
})
});
router
.param('id', function (req, res, next) {
req.dbQuery = {id: parseInt(req.params.id, 10)};
next();
})
.route('/contact/:id')
.get(function (req, res) {
db.findOne(req.dbQuery, function (err, data) {
res.json(data);
});
})
.put(function (req, res) {
var contact = req.body;
delete contact.$promise;
delete contact.$resolved;
db.update(req.dbQuery, contact, function (err, data) {
res.json(data[0]);
});
})
.delete(function (req, res) {
db.delete(req.dbQuery, function () {
res.json(null);
});
});
module.exports = router;