64 lines
2.1 KiB
JavaScript
64 lines
2.1 KiB
JavaScript
const data = require('./data.json');
|
|
|
|
module.exports = (req, res, next) => {
|
|
if (req.url.startsWith('/search') && req.method === 'GET') {
|
|
try {
|
|
const searchText = req.query.name;
|
|
const searched = data.products.filter(
|
|
(product) =>
|
|
(product.name.toLowerCase()).includes(searchText.toLowerCase())
|
|
);
|
|
return res.json(searched);
|
|
} catch (error) {
|
|
console.error('Error loading data:', error);
|
|
res.status(500).json({ message: 'Internal Server Error' });
|
|
}
|
|
} else if (req.url.startsWith('/report') && req.method === 'GET') {
|
|
try {
|
|
// Добавляем фильтрацию по полю dateTime
|
|
const fromDate = req.query.fromDate;
|
|
const toDate = req.query.toDate;
|
|
const filteredData = filterByDateTime(data.items, fromDate, toDate);
|
|
|
|
// Возвращаем отфильтрованные и отсортированные элементы
|
|
const reportData = generateReport(filteredData, data.bikes).sort((a, b) => a.dateTime - b.dateTime);
|
|
res.json(reportData);
|
|
} catch (error) {
|
|
console.error('Error loading data:', error);
|
|
return res.status(500).json({ message: 'Internal Server Error' });
|
|
}
|
|
} else {
|
|
next();
|
|
}
|
|
};
|
|
|
|
// Функция generateReport для примера
|
|
function generateReport(items, bikes) {
|
|
// Ваш код для генерации отчета
|
|
return items.map((item) => ({
|
|
id: item.id,
|
|
dateTime: item.dateTime,
|
|
weight: item.weight,
|
|
radius: item.radius,
|
|
colorbike: item.colorbike,
|
|
maxCount: item.maxCount,
|
|
bikeName: bikes.find((bike) => bike.id === item.bikeId)?.name,
|
|
|
|
// Другие поля отчета, которые вам нужны
|
|
}));
|
|
}
|
|
|
|
// Функция для фильтрации по полю dateTime
|
|
function filterByDateTime(items, fromDate, toDate) {
|
|
if (!fromDate && !toDate) {
|
|
return items;
|
|
}
|
|
|
|
const filteredItems = items.filter((item) => {
|
|
const itemDate = item.dateTime;
|
|
return (!fromDate || itemDate >= fromDate) && (!toDate || itemDate <= toDate);
|
|
});
|
|
|
|
return filteredItems;
|
|
}
|