-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
62 lines (56 loc) · 1.81 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
module.exports = function(stockRepository) {
return {
sendHello: function (req, res, next) {
res.send('Hello World!');
},
getStock: function(req, res, next) {
stockRepository
.findAll()
.then(function (result) {
res.json(result);
})
.catch(next);
},
getStockItem: function(req, res, next) {
var isbn = req.params.isbn;
stockRepository
.getBook(isbn)
.then(function(result) {
if (!result) {
res.status(404);
next();
}
res.json(result);
}).catch(next);
},
getBookAvailability: function(req, res, next) {
var isbn = req.params.isbn;
stockRepository
.getBook(isbn)
.then(function(result) {
if (!result) {
res.status(404);
next();
}
res.format({
html: function() {
res.send('<div class="copiesLeft">' + result.count + '</div>');
},
json: function() {
res.status(200).json(result.count);
}
});
}).catch(next);
},
stockUp: function(req, res, next) {
var isbn = req.body.isbn;
var count = req.body.count;
stockRepository
.stockUp(isbn, count)
.then(function(result) {
res.json(result);
})
.catch(next);
}
};
};