index.get.ts
3.43 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
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
import is from '@sindresorhus/is'
import { destr } from 'destr'
import { db } from '@/server/fake-db/apps/ecommerce'
import { paginateArray } from '@/server/utils/paginateArray'
export default defineEventHandler(event => {
const { q = '', stock = null, category = null, status = null, sortBy, orderBy, itemsPerPage = 10, page = 1 } = getQuery(event)
const searchQuery = is.string(q) ? q : undefined
const queryLower = (searchQuery ?? '').toString().toLowerCase()
const parsedStock = destr(stock)
const stockLocal = is.boolean(parsedStock) ? parsedStock : undefined
const parsedSortBy = destr(sortBy)
const sortByLocal = is.string(parsedSortBy) ? parsedSortBy : ''
const parsedOrderBy = destr(orderBy)
const orderByLocal = is.string(parsedOrderBy) ? parsedOrderBy : ''
const parsedItemsPerPage = destr(itemsPerPage)
const parsedPage = destr(page)
const itemsPerPageLocal = is.number(parsedItemsPerPage) ? parsedItemsPerPage : 10
const pageLocal = is.number(parsedPage) ? parsedPage : 1
// Filtering Products
let filteredProducts = db.products.filter(product => (
(product.productName.toLowerCase().includes(queryLower) || product.productBrand.toLowerCase().includes(queryLower))
&& product.category === (category || product.category)
&& (product.status === (status || product.status))
&& (typeof stockLocal === 'undefined' ? true : (product.stock === stockLocal))
)).reverse()
// Sort
if (sortByLocal) {
if (sortByLocal === 'product') {
filteredProducts = filteredProducts.sort((a, b) => {
if (orderByLocal === 'asc')
return a.productName.toLowerCase() > b.productName.toLowerCase() ? 1 : -1
else if (orderByLocal === 'desc')
return a.productName.toLowerCase() < b.productName.toLowerCase() ? 1 : -1
return 0
})
}
if (sortByLocal === 'category') {
filteredProducts = filteredProducts.sort((a, b) => {
if (orderByLocal === 'asc')
return a.category > b.category ? 1 : -1
else if (orderByLocal === 'desc')
return a.category < b.category ? 1 : -1
return 0
})
}
if (sortByLocal === 'status') {
filteredProducts = filteredProducts.sort((a, b) => {
if (orderByLocal === 'asc')
return a.status > b.status ? 1 : -1
else if (orderByLocal === 'desc')
return a.status < b.status ? 1 : -1
return 0
})
}
if (sortByLocal === 'price') {
filteredProducts = filteredProducts.sort((a, b) => {
if (orderByLocal === 'asc')
return Number(a.price.slice(1)) > Number(b.price.slice(1)) ? 1 : -1
else if (orderByLocal === 'desc')
return Number(a.price.slice(1)) < Number(b.price.slice(1)) ? 1 : -1
return 0
})
}
if (sortByLocal === 'qty') {
filteredProducts = filteredProducts.sort((a, b) => {
if (orderByLocal === 'asc')
return a.qty > b.qty ? 1 : -1
else if (orderByLocal === 'desc')
return a.qty < b.qty ? 1 : -1
return 0
})
}
if (sortByLocal === 'sku') {
filteredProducts = filteredProducts.sort((a, b) => {
if (orderByLocal === 'asc')
return a.sku > b.sku ? 1 : -1
else if (orderByLocal === 'desc')
return a.sku < b.sku ? 1 : -1
return 0
})
}
}
return { products: paginateArray(filteredProducts, itemsPerPageLocal, pageLocal), total: filteredProducts.length }
})