-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.js
68 lines (65 loc) · 2.62 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
63
64
65
66
67
68
module.exports = function (stockRepository) {
return {
stockUp: function (req, res) {
stockRepository.stockUp(req.body.isbn, req.body.count)
.then(function (result) {
res.json({isbn: req.body.isbn, count: req.body.count});
}).catch(function (err) {
console.error(err.stack);
res.status(500).json({error: "Can't stock up right now. Try again later."});
});
},
getCount: function (req, res) {
var xRequestId = req.headers['x-request-id'];
var isbn = req.params.isbn;
console.log('tz-books (backend): getCount for isbn: ' + isbn + '; x-request-id: ' + xRequestId);
stockRepository.getCount(req.params.isbn).then(function (result) {
if (result !== null) {
// res.status(200).json({count: result});
console.log(result.count);
res.status(200).send('' + result.count);
} else {
res.status(404).json({error: 'No book with ISBN: ' + req.params.isbn});
}
//res.json({});
}).catch(function (err) {
console.error(err.stack);
res.status(500).json({error: "Can't stock up right now. Try again later."});
});;
},
getAvailability: function (req, res) {
stockRepository.getCount(req.params.isbn).then(function (result) {
if (result !== null) {
if (result > 0)
res.status(200).send("available");
else
res.status(200).send("unavailable");
} else {
res.status(200).send("unavailable");
}
res.send("unknown");
});
},
findAll: function (req, res) {
stockRepository.findAll().then(function (books) {
res.json(books);
}).catch(function (err) {
console.error(err.stack);
res.status(500).json({error: "Can't read the stock right now. Try again later."});
});
},
clientError: function (req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
},
serverError: function (err, req, res, next) {
res.status(err.status || 500);
console.error(err.stack);
res.json({
message: err.message,
error: (process.env.NODE_ENV === 'production') ? {} : err
});
}
};
};