-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.js
52 lines (48 loc) · 1.36 KB
/
helpers.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
const filterData = (data,query) => {
if (result) {
result.forEach(item => {
if (search && item.lokasi.toLowerCase().includes(search.toLowerCase())) {
lokasi.push(item.lokasi)
isLok = true
}
});
}
const per_page = 3;
const page = req.params.page || 1;
const queryObject = url.parse(req.url, true).search;
let params = [];
if (search && Number(search)) {
params.push(`harga = '${search}'`)
}
if (search && Number(search)) {
params.push(`harga = '${search}'`)
}
if (search && Number(search)) {
params.push(`id = '${search}'`)
}
if (search && isLok) {
if (lokasi.length > 0) {
let locs = ''
for (let i = 0; i < lokasi.length; i++) {
locs += ` lokasi = '${lokasi[i]}' ${i == lokasi.length - 1 ? "" : "OR"}`
}
params.push(`${locs}`)
} else {
params.push(lokasi)
}
}
if (search) {
params.push(`foto = '${search}'`)
}
let sql = `SELECT * FROM iklan`;
if (params.length > 0) {
sql += ` WHERE `;
for (let i = 0; i < params.length; i++) {
sql += `${params[i]}`;
if (params.length != i + 1) {
sql += ` OR `;
}
}
}
}
exports.filterData = filterData