111 lines
3.9 KiB
JavaScript
111 lines
3.9 KiB
JavaScript
const express = require('express');
|
|
const router = express.Router();
|
|
const axios = require('axios');
|
|
const logger = require('../../logger');
|
|
const multer = require('multer');
|
|
const FormData = require('form-data');
|
|
const fs = require('fs');
|
|
|
|
const BACKEND_URL = process.env.BACKEND_URL || 'http://localhost:3001';
|
|
const upload = multer({ dest: 'uploads/tmp/' });
|
|
|
|
// ================= ЗАКАЗЫ =================
|
|
router.get('/orders', async (req, res) => {
|
|
try {
|
|
const { status, date } = req.query;
|
|
const { data } = await axios.get(`${BACKEND_URL}/api/food/orders`, {
|
|
params: { status, date }, timeout: 5000,
|
|
});
|
|
res.json(data);
|
|
} catch (e) {
|
|
logger.error('[admin/food/orders GET]', e.message);
|
|
res.status(500).json({ error: 'Не удалось получить заказы' });
|
|
}
|
|
});
|
|
|
|
router.patch('/orders/:id/status', async (req, res) => {
|
|
try {
|
|
const { data } = await axios.patch(`${BACKEND_URL}/api/food/orders/${req.params.id}/status`, req.body, { timeout: 5000 });
|
|
res.json(data);
|
|
} catch (e) {
|
|
logger.error('[admin/food/orders PATCH]', e.message);
|
|
res.status(500).json({ error: 'Ошибка обновления статуса' });
|
|
}
|
|
});
|
|
|
|
// ================= МЕНЮ =================
|
|
router.get('/items', async (req, res) => {
|
|
try {
|
|
const { data } = await axios.get(`${BACKEND_URL}/api/food/menu`);
|
|
res.json(data);
|
|
} catch (e) {
|
|
res.status(500).json({ error: 'Не удалось получить меню' });
|
|
}
|
|
});
|
|
|
|
router.post('/items', upload.single('image'), async (req, res) => {
|
|
try {
|
|
const formData = new FormData();
|
|
|
|
// Передаем текстовые поля
|
|
Object.keys(req.body).forEach(key => {
|
|
formData.append(key, req.body[key]);
|
|
});
|
|
|
|
// Передаем файл, если он есть
|
|
if (req.file) {
|
|
formData.append('image', fs.createReadStream(req.file.path));
|
|
}
|
|
|
|
const { data } = await axios.post(`${BACKEND_URL}/api/food/items`, formData, {
|
|
headers: formData.getHeaders(),
|
|
maxContentLength: Infinity,
|
|
maxBodyLength: Infinity
|
|
});
|
|
|
|
if (req.file) fs.unlinkSync(req.file.path);
|
|
res.json(data);
|
|
} catch (e) {
|
|
console.error('Ошибка проксирования POST:', e.response?.data || e.message);
|
|
res.status(500).json({ error: e.response?.data?.error || 'Ошибка добавления блюда' });
|
|
}
|
|
});
|
|
|
|
router.put('/items/:id', upload.single('image'), async (req, res) => {
|
|
try {
|
|
const formData = new FormData();
|
|
|
|
// Передаем текстовые поля
|
|
Object.keys(req.body).forEach(key => {
|
|
formData.append(key, req.body[key]);
|
|
});
|
|
|
|
// Передаем файл, если он есть
|
|
if (req.file) {
|
|
formData.append('image', fs.createReadStream(req.file.path));
|
|
}
|
|
|
|
const { data } = await axios.put(`${BACKEND_URL}/api/food/items/${req.params.id}`, formData, {
|
|
headers: formData.getHeaders(),
|
|
maxContentLength: Infinity,
|
|
maxBodyLength: Infinity
|
|
});
|
|
|
|
if (req.file) fs.unlinkSync(req.file.path);
|
|
res.json(data);
|
|
} catch (e) {
|
|
console.error('Ошибка проксирования PUT:', e.response?.data || e.message);
|
|
res.status(500).json({ error: e.response?.data?.error || 'Ошибка обновления блюда' });
|
|
}
|
|
});
|
|
|
|
router.delete('/items/:id', async (req, res) => {
|
|
try {
|
|
const { data } = await axios.delete(`${BACKEND_URL}/api/food/items/${req.params.id}`);
|
|
res.json(data);
|
|
} catch (e) {
|
|
res.status(500).json({ error: 'Ошибка удаления блюда' });
|
|
}
|
|
});
|
|
|
|
module.exports = router; |