-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.js
executable file
·64 lines (59 loc) · 1.92 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
'use strict';
module.exports = function (stockRepo) {
return {
stock: {
get(req, res, next) {
const accept = req.headers.accept || '';
const isbn = req.params.isbn;
const requestId = req.header('x-request-id') || 'SuperInventoryBookId' + isbn;
stockRepo.getByIsbn(isbn)
.then(result => {
if (!result) {
return next();
}
res.status(200)
.setHeader('X-Request-ID', requestId);
if (accept.match(/text\/html/)) {
res.send(`<span>${result.count}</span>`);
} else {
res.json(result);
}
})
.catch(err => {
next(err);
});
},
post(req, res, next) {
let payload = req.body;
stockRepo.upsert(payload)
.then((result) => {
console.log('inserted', result);
res.status(201).json({ status: 'ok dude' });
})
.catch(err => {
next(err);
});
}
},
test: {
ping(req, res) {
res.status(200).send('ok');
}
},
errors: {
notFound(req, res) {
res.status(404).json({
status: 404,
msg: '404 error'
});
},
internal(err, req, res, next) {
console.log(err.stack);
res.status(500).json({
status: 500,
msg: err.message
});
}
}
}
};