-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
93 lines (82 loc) · 2.17 KB
/
index.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const url = require("url");
const jsonServer = require("json-server");
const server = jsonServer.create();
const router = jsonServer.router("db.json");
const middlewares = jsonServer.defaults();
const paginate = require("./paginate");
const port = process.env.PORT || 3000;
router.render = (req, res) => {
const data = res.locals.data;
//if its not items route
if (!req.originalUrl.includes("items")) {
res.json({
meta: {
totalCount: data.length,
},
results: data,
});
return;
}
var page = 1;
const perPage = 16;
//if p parameter came
if (req.originalUrl.indexOf("p=") > -1) {
const urlParts = url.parse(req.url, true);
page = urlParts.query.p;
}
const brandsPool = {};
const tagsPool = {};
const itemTypesPool = {};
for (var i = 0; i < data.length; i++) {
if (brandsPool[data[i].manufacturer]) {
brandsPool[data[i].manufacturer] += 1;
} else {
brandsPool[data[i].manufacturer] = 1;
}
if (itemTypesPool[data[i].itemType]) {
itemTypesPool[data[i].itemType] += 1;
} else {
itemTypesPool[data[i].itemType] = 1;
}
for (var j = 0; j < data[i].tags.length; j++) {
if (tagsPool[data[i].tags[j]]) {
tagsPool[data[i].tags[j]] += 1;
} else {
tagsPool[data[i].tags[j]] = 1;
}
}
}
const brands = Object.entries(brandsPool).map((item) => ({
name: item[0],
count: item[1],
}));
const tags = Object.entries(tagsPool).map((item) => ({
name: item[0],
count: item[1],
}));
const itemTypes = Object.entries(itemTypesPool).map((item) => ({
name: item[0],
count: item[1],
}));
const pageIndex = (page - 1) * perPage;
const items = data.slice(pageIndex, pageIndex + perPage);
res.json({
meta: {
total: res.locals.data.length,
pagination: paginate({
totalCount: data.length,
currentPage: page,
perPage: perPage,
}),
},
results: {
brands: [{ name: "All", count: data.length }, ...brands],
tags: [{ name: "All", count: data.length }, ...tags],
itemTypes,
items: items,
},
});
};
server.use(middlewares);
server.use(router);
server.listen(port);