-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.js
41 lines (39 loc) · 1.43 KB
/
routes.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
var util = require('util');
module.exports = function(stockRepository) {
return {
helloWorld: function(req, res, next) {
res.send('Hello World!');
},
putBook: function(req, res, next) {
var isbn = req.body.isbn;
var count = req.body.count;
stockRepository.stockUp(isbn, count).then(function(result) {
res.json({isbn: req.body.isbn, count: req.body.count});
}).catch(next);
},
getBookByISBN: function(req, res, next) {
stockRepository.getCount(req.params.isbn).then(function(result) {
if (result !== null) {
res.format({
html: function() {
res.status(200).send(
util.format('<p class="copiesLeft">%s</p>', result)
);
},
json: function() {
res.json({count: result});
}
});
} else {
next();
//res.status(404).json({error: 'No book with ISBN: ' + req.params.isbn});
}
}).catch(next);
},
getBooks: function(req, res, next) {
stockRepository.findAll().then(function(docs) {
res.json(docs);
}).catch(next);
}
};
};