-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
172 lines (142 loc) · 4.99 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const express = require('express')
const app = express()
const port = process.env.PORT || 4000
const cors = require('cors')
const category = require('./data/category.json');
const products = require('./data/products.json');
const tags = require('./data/tag.json');
const brands = require('./data/brand.json');
// import function for Sorting ascending and descending order through price
const sorting_asc_des = require('./commonFunctions/sorting_asc_des')
// Global Middleware
app.use(cors())
app.get('/', (req, res) => {
res.send('Server is running')
})
// Search Product By Category Properties
app.get('/category', (req, res) => {
const filters = req.query;
const searched_category = category.filter(c => {
let isValid = true;
for (let key in filters) {
isValid = isValid && c[key] == filters[key];
}
return isValid;
});
res.send(searched_category);
})
// Search Product By Product Properties
app.get('/products', (req, res) => {
const filters = req.query;
const searched_product = products.filter(p => {
let isValid = true;
for (let key in filters) {
let productSearching = p[key].toString().toLowerCase();
let productFiltering = filters[key].toString().toLowerCase();
isValid = isValid && productSearching.includes(productFiltering);
}
return isValid;
});
res.send(searched_product);
})
// Search Product By Tags
app.get('/tags', (req, res) => {
const filters = req.query;
const searched_tag = tags.filter(p => {
let isValid = true;
for (let key in filters) {
let tagSearching = p[key].toString().toLowerCase();
let tagFiltering = filters[key].toString().toLowerCase();
isValid = isValid && tagSearching.includes(tagFiltering);
}
return isValid;
});
res.send(searched_tag);
})
// Search Product By Brands
app.get('/brands', (req, res) => {
const filters = req.query;
const searched_brand = brands.filter(p => {
let isValid = true;
for (let key in filters) {
let brandSearching = p[key].toString().toLowerCase();
let brandFiltering = filters[key].toString().toLowerCase();
isValid = isValid && brandSearching.includes(brandFiltering);
}
return isValid;
});
res.send(searched_brand);
})
// Sorting Price Low to High
app.get('/products/aes', (req, res) => {
// Call the function and sending path and product to sorting_asc_des function for sorting
const asc_products = sorting_asc_des(req.path, products) //call and pass arguments to sorting_asc_des
res.send(asc_products);
})
// Sorting Price High to Low
app.get('/products/des', (req, res) => {
const des_products = sorting_asc_des(req.path, products) //call and pass arguments to sorting_asc_des
res.send(des_products);
})
// Search Category by ID
app.get('/category/:id', (req, res) => {
const id = req.params.id;
const selected_category = category.find(c => c.id == id);
res.send(selected_category);
});
// Search Product by ProductID
app.get('/products/:id', (req, res) => {
const id = req.params.id;
const selected_product = products.find(p => p.id == id);
res.send(selected_product);
});
// Search Product by CategoryID
app.get('/products/category/:id', (req, res) => {
const id = req.params.id;
const category_product = products.filter(product => product.categoryID === id);
const filters = req.query;
const category_searched_product = category_product.filter(p => {
let isValid = true;
for (let key in filters) {
let productSearching = p[key].toString().toLowerCase();
let productFiltering = filters[key].toString().toLowerCase();
isValid = isValid && productSearching.includes(productFiltering);
}
return isValid;
});
res.send(category_searched_product);
})
// Search Product by TAG
app.get('/products/tag/:id', (req, res) => {
const id = req.params.id;
const tag_product = products.filter(product => product.tagID === id);
res.send(tag_product);
})
// Sorting category Products in ascending order
app.get('/products/category/:id/aes', (req, res) => {
const id = req.params.id;
const category_product = products.filter(product => product.categoryID === id);
const asc_category = sorting_asc_des(req.path, category_product) //call and pass arguments to sorting_asc_des
res.send(asc_category);
})
// Sorting category Products in descending order
app.get('/products/category/:id/des', (req, res) => {
const id = req.params.id;
const category_product = products.filter(product => product.categoryID === id);
const des_category = sorting_asc_des(req.path, category_product) //call and pass arguments to sorting_asc_des
res.send(des_category);
})
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
}).on('error', (err) => {
if (err.code === 'EACCES') {
console.error(`Port ${port} requires elevated privileges`);
process.exit(1);
} else if (err.code === 'EADDRINUSE') {
console.error(`Port ${port} is already in use`);
process.exit(1);
} else {
console.error(`Error: ${err}`);
process.exit(1);
}
});