Reflex/backend/routes/userRoutes.js
Professional 7c06cf85a5 фикс
2025-05-24 02:13:56 +07:00

57 lines
3.1 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const express = require('express');
const router = express.Router();
const { updateUserProfile, getUsersForSwiping, uploadUserProfilePhoto, setMainPhoto, deletePhoto, getUserById, getUserStats, recordProfileView } = require('../controllers/userController');
const { protect } = require('../middleware/authMiddleware'); // Нам нужен protect для защиты маршрута
const multer = require('multer'); // 1. Импортируем multer
const path = require('path'); // Может понадобиться для фильтрации файлов
// Настройка multer для загрузки фотографий
const storage = multer.memoryStorage(); // Хранить файл в памяти, а не на диске
// Конфигурация multer с проверкой типа файла
const upload = multer({
storage: storage,
fileFilter: (req, file, cb) => {
// Проверяем, является ли файл изображением
const filetypes = /jpeg|jpg|png|webp/;
const mimetype = filetypes.test(file.mimetype);
const extname = filetypes.test(path.extname(file.originalname).toLowerCase());
if (mimetype && extname) {
return cb(null, true);
} else {
// cb(new Error('Допустимы только изображения (jpg, jpeg, png, webp)!'), false);
const err = new Error('Разрешены только изображения (jpg, jpeg, png, webp)!'); // Изменено сообщение об ошибке
err.statusCode = 400; // Устанавливаем statusCode для multer ошибок
cb(err, false);
}
},
limits: {
fileSize: 5 * 1024 * 1024, // Ограничение размера файла (5 МБ)
},
});
// Маршрут для обновления профиля текущего аутентифицированного пользователя
// PUT /api/users/profile
router.put('/profile', protect, updateUserProfile);
router.get('/suggestions', protect, getUsersForSwiping); // <--- НОВЫЙ МАРШРУТ
// Маршрут для получения статистики пользователя
// GET /api/users/stats
router.get('/stats', protect, getUserStats); // <--- НОВЫЙ МАРШРУТ
// Маршрут для загрузки фотографии профиля
// POST /api/users/profile/photo
router.post('/profile/photo', protect, upload.single('profilePhoto'), uploadUserProfilePhoto);
router.put('/profile/photo/:photoId/set-main', protect, setMainPhoto); // <--- НОВЫЙ МАРШРУТ
router.delete('/profile/photo/:photoId', protect, deletePhoto); // <--- НОВЫЙ МАРШРУТ
// Маршрут для получения профиля по ID (должен быть ПЕРЕД POST /:userId/view)
// GET /api/users/:userId
router.get('/:userId', protect, getUserById); // <--- НОВЫЙ МАРШРУТ
// Маршрут для записи просмотра профиля
// POST /api/users/:userId/view
router.post('/:userId/view', protect, recordProfileView); // <--- НОВЫЙ МАРШРУТ
module.exports = router;