/
Gabryelf
/
Pixel-Orb
Обзор
Документация
Войти
/
Gabryelf
/
Pixel-Orb
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/server/services/galleryService.js
82 строки
2 KB
Valeev Serj
restruct_v0.0.1
07 авг 2026, 22:50
07 авг 2026, 22:50
41f78a4
Код
Авторство
О чём код?
const projectService = require('./projectService'); class GalleryService { constructor() { this.ratings = new Map(); } getPublicProjects(req, res) { try { const projects = []; for (let project of projectService.projects.values()) { if (project.isPublic) { projects.push(project); } } return res.json(projects); } catch (error) { console.error('Get gallery error:', error); return res.status(500).json({ error: 'Failed to load gallery' }); } } rateProject(req, res) { try { const { projectId, userId, ratingType } = req.body; if (!projectId || !userId || !ratingType) { return res.status(400).json({ error: 'Project ID, User ID and Rating type required' }); } const project = projectService.projects.get(projectId); if (!project) { return res.status(404).json({ error: 'Project not found' }); } if (!project.isPublic) { return res.status(400).json({ error: 'Project is not public' }); } const key = `${projectId}_${userId}`; if (this.ratings.has(key)) { return res.status(400).json({ error: 'You already rated this project' }); } const validRatings = ['like', 'love', 'creative', 'amazing', 'masterpiece']; if (!validRatings.includes(ratingType)) { return res.status(400).json({ error: 'Invalid rating type' }); } this.ratings.set(key, { projectId, userId, ratingType, timestamp: new Date() }); project.ratings = project.ratings || []; project.ratings.push({ userId, ratingType }); return res.json({ success: true, ratings: project.ratings, total: project.ratings.length }); } catch (error) { console.error('Rate project error:', error); return res.status(500).json({ error: 'Failed to rate project' }); } } getProjectRatings(projectId) { const result = []; for (let [key, value] of this.ratings) { if (value.projectId === projectId) { result.push(value); } } return result; } } module.exports = new GalleryService();