109 lines
2.9 KiB
JavaScript
109 lines
2.9 KiB
JavaScript
const db = require("../../db/db");
|
|
|
|
class ProductController {
|
|
async findAll(req, res) {
|
|
|
|
const params = req.query;
|
|
const _products = await db.getProducts(params.dateExpired, params.postalCode);
|
|
let productsFilter = _products[0]
|
|
|
|
if (Number(params.recommend)) {
|
|
productsFilter = productsFilter.filter(item => item.recommend == params.recommend)
|
|
}
|
|
if (params.type) {
|
|
productsFilter = productsFilter.filter(item => item.type === params.type)
|
|
}
|
|
|
|
if (params.minPrice && !params.maxPrice) {
|
|
productsFilter = productsFilter.filter(item => {
|
|
const price = Number(item.price)
|
|
if (price >= Number(params.minPrice)) {
|
|
return item
|
|
}
|
|
})
|
|
}
|
|
if (params.maxPrice && !params.minPrice) {
|
|
productsFilter = productsFilter.filter(item => {
|
|
const price = Number(item.price)
|
|
if (price <= Number(params.maxPrice)) {
|
|
return item
|
|
}
|
|
})
|
|
}
|
|
if (params.maxPrice && params.minPrice) {
|
|
productsFilter = productsFilter.filter(item => {
|
|
const price = Number(item.price)
|
|
if (price >= Number(params.minPrice) && price <= Number(params.maxPrice)) {
|
|
return item
|
|
}
|
|
})
|
|
}
|
|
|
|
|
|
if (Number(params.bigPrice)) {
|
|
productsFilter.sort((a, b) => {
|
|
const itemA = Number(a.price)
|
|
const itemB = Number(b.price)
|
|
return itemB - itemA;
|
|
})
|
|
}
|
|
|
|
if (Number(params.lowPrice)) {
|
|
productsFilter.sort((a, b) => {
|
|
const itemA = Number(a.price)
|
|
const itemB = Number(b.price)
|
|
return itemA - itemB;
|
|
})
|
|
}
|
|
|
|
if (Number(params.order_descending)) {
|
|
productsFilter.sort((a, b) => {
|
|
const itemA = a.order_position
|
|
const itemB = b.order_position
|
|
return itemB - itemA;
|
|
})
|
|
}
|
|
|
|
if (Number(params.order_crescent)) {
|
|
productsFilter.sort((a, b) => {
|
|
const itemA = a.order_position
|
|
const itemB = b.order_position
|
|
return itemA - itemB;
|
|
})
|
|
}
|
|
|
|
if (Number(params.isNew)) {
|
|
productsFilter = productsFilter.filter(item => item.isNew == Number(params.isNew))
|
|
}
|
|
|
|
/* let productsFilterPages = []
|
|
const totalItens = params?.itens ? Number(params.itens) : 200
|
|
const page = params.page ? Number(params.page) : 1
|
|
const startIndex = (totalItens * page) - totalItens
|
|
const lastIndex = (totalItens * page)
|
|
const products = productsFilter.slice(startIndex, lastIndex)
|
|
productsFilterPages.push({
|
|
page: page,
|
|
productsPerPage: products.length,
|
|
products: products
|
|
}) */
|
|
|
|
|
|
return res.status(200).send({
|
|
data: productsFilter
|
|
})
|
|
}
|
|
|
|
async findById(req, res) {
|
|
const id = Number(req.params.id)
|
|
const _products = await db.getProducts();
|
|
const filterProduct = _products[0].filter(item => item.id === id)
|
|
|
|
return res.status(200).send({
|
|
data: filterProduct
|
|
})
|
|
}
|
|
}
|
|
|
|
module.exports = new ProductController();
|