-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
76 lines (65 loc) · 1.94 KB
/
Copy pathserver.js
File metadata and controls
76 lines (65 loc) · 1.94 KB
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
69
70
71
72
73
74
75
76
const express = require('express')
const bodyParser = require('body-parser')
const store = require('./store')
const app = express()
app.use(express.static('public'))
app.use(bodyParser.json())
app.post('/products', (req, res) => {
store.createProduct({
name: req.body.name,
description: req.body.description,
price: req.body.price
}).then((data) => {
if (data.length > 0) {
res.send(data[0]);
}
})
})
app.put('/products', (req, res) => {
store.updateProduct({
id: req.body.id,
name: req.body.name,
description: req.body.description,
price: req.body.price
}).then(data => {
if (data.length > 0) {
res.send(data[0]);
}
})
})
app.delete('/products/:id', (req, res) => {
const id = req.params.id;
store.deleteProduct(id).then(data => res.sendStatus(200));
})
app.get('/products', async (req, res) => {
const currentPage = req.query.page ? req.query.page : 1;
const pageSize = req.query.limit ? req.query.limit : 10;
const sortBy = req.query.sort ? req.query.sort : '-id';
const totalRowsCountResult = await store.countProducts();
const totalRowsCount = totalRowsCountResult.total_count;
const totalPages = Math.ceil(totalRowsCount/pageSize);
const getOrderBy = rawSortBy => {
if (rawSortBy.indexOf('-') < 0) {
return [rawSortBy, 'ASC'];
}
return [rawSortBy.replace('-', ''), 'DESC'];
}
store.getProducts({
limit: pageSize,
offset: pageSize * (currentPage - 1),
orderBy: getOrderBy(sortBy)
}).then(data => res.send({
data,
totalPages,
currentPage,
pageSize,
sortBy
}));
})
app.get('/products/:id', async (req, res) => {
const id = req.params.id;
store.getProduct(id).then(data => res.send(data));
})
app.listen(7555, () => {
console.log('Server running on http://localhost:7555')
})