2024-01-17 17:15:19 +00:00
|
|
|
const db = require("../db/db");
|
2024-01-11 10:16:45 +00:00
|
|
|
|
|
|
|
const productsJson = require("./products.json")
|
2024-01-17 17:15:19 +00:00
|
|
|
|
2024-01-11 10:16:45 +00:00
|
|
|
class ProductController {
|
2024-01-17 17:15:19 +00:00
|
|
|
async findAll(req, res) {
|
|
|
|
|
|
|
|
|
|
|
|
const _products = await db.getProducts();
|
|
|
|
const mapedProducts = await _products[0].map(function (item) {
|
|
|
|
return {
|
|
|
|
id: item.id,
|
|
|
|
name: item.name,
|
|
|
|
description: item.description,
|
|
|
|
type: item.type,
|
2024-01-18 10:13:15 +00:00
|
|
|
price: item.price,
|
2024-01-17 17:15:19 +00:00
|
|
|
specialPrice: item.specialPrice,
|
|
|
|
isNew: item.isNew,
|
|
|
|
slug: item.slug,
|
|
|
|
category: item.category,
|
|
|
|
postalCode: item.postalCode,
|
|
|
|
dateExpired: item.dateExpired,
|
|
|
|
images: [item.image],
|
|
|
|
featured: item.featured,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2024-01-11 10:16:45 +00:00
|
|
|
//Gerar produtos fake
|
|
|
|
/* const fakeProducts = generateFlowers(50)
|
|
|
|
console.log(fakeProducts); */
|
|
|
|
const params = req.query;
|
2024-01-17 17:15:19 +00:00
|
|
|
let productsFilter = mapedProducts
|
|
|
|
console.log(params);
|
2024-01-11 10:16:45 +00:00
|
|
|
|
|
|
|
if (Number(params.featured)) {
|
|
|
|
productsFilter = productsFilter.filter(item => item.featured === Number(params.featured))
|
|
|
|
}
|
2024-01-17 17:15:19 +00:00
|
|
|
if (params.category) {
|
|
|
|
productsFilter = productsFilter.filter(item => item.category === Number(params.category))
|
|
|
|
}
|
|
|
|
if (params.postalCode) {
|
|
|
|
productsFilter = productsFilter.filter(item => item.postalCode === params.postalCode)
|
|
|
|
}
|
|
|
|
if (params.minPrice && !params.maxPrice) {
|
|
|
|
productsFilter = productsFilter.filter(item => {
|
|
|
|
const price = Number(item.price.replace(/€/g, ''))
|
|
|
|
if (price >= Number(params.minPrice)) {
|
|
|
|
return item
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (params.maxPrice && !params.minPrice) {
|
|
|
|
productsFilter = productsFilter.filter(item => {
|
|
|
|
const price = Number(item.price.replace(/€/g, ''))
|
|
|
|
if (price <= Number(params.maxPrice)) {
|
|
|
|
return item
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (params.maxPrice && params.minPrice) {
|
|
|
|
productsFilter = productsFilter.filter(item => {
|
|
|
|
const price = Number(item.price.replace(/€/g, ''))
|
|
|
|
if (price >= Number(params.minPrice) && price <= Number(params.maxPrice)) {
|
|
|
|
console.log(price);
|
|
|
|
return item
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (params.dateExpired) {
|
|
|
|
const [day, month, year] = params.dateExpired.split("/");
|
|
|
|
const dateSearch = new Date(year, month - 1, day);
|
|
|
|
productsFilter = productsFilter.filter(item => {
|
|
|
|
const [day, month, year] = item.dateExpired.split("/");
|
|
|
|
const dateProduct = new Date(year, month - 1, day);
|
|
|
|
if (dateProduct >= dateSearch) {
|
|
|
|
return item
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2024-01-11 10:16:45 +00:00
|
|
|
|
2024-01-17 17:15:19 +00:00
|
|
|
if (Number(params.bigPrice)) {
|
|
|
|
productsFilter.sort((a, b) => {
|
|
|
|
const itemA = Number(a.price.replace(/€/g, ''))
|
|
|
|
const itemB = Number(b.price.replace(/€/g, ''))
|
|
|
|
return itemB - itemA;
|
|
|
|
})
|
2024-01-11 10:16:45 +00:00
|
|
|
}
|
2024-01-17 17:15:19 +00:00
|
|
|
|
|
|
|
if (Number(params.lowPrice)) {
|
|
|
|
productsFilter.sort((a, b) => {
|
|
|
|
const itemA = Number(a.price.replace(/€/g, ''))
|
|
|
|
const itemB = Number(b.price.replace(/€/g, ''))
|
|
|
|
return itemA - itemB;
|
2024-01-11 10:16:45 +00:00
|
|
|
})
|
2024-01-17 17:15:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (params.isNew) {
|
|
|
|
productsFilter = productsFilter.filter(item => item.isNew === true)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
})
|
2024-01-11 10:16:45 +00:00
|
|
|
|
|
|
|
return res.status(200).send({
|
2024-01-17 17:15:19 +00:00
|
|
|
data: productsFilterPages
|
2024-01-11 10:16:45 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
findBySlug(req, res) {
|
|
|
|
const slug = req.params.slug
|
|
|
|
const products = productsJson
|
|
|
|
const filterSlug = products.filter(item => item.slug === slug)
|
|
|
|
|
|
|
|
return res.status(200).send({
|
|
|
|
data: filterSlug
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = new ProductController();
|