/
Gabryelf
/
Pixel-Orb
Обзор
Документация
Войти
/
Gabryelf
/
Pixel-Orb
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/server/services/projectService.js
124 строки
3 KB
Valeev Serj
restruct_v0.0.1
07 авг 2026, 22:50
07 авг 2026, 22:50
41f78a4
Код
Авторство
О чём код?
class ProjectService { constructor() { this.projects = new Map(); } generateId() { return 'proj_' + Date.now().toString(36) + Math.random().toString(36).substr(2, 6); } createEmptyCanvas(size) { return new Array(size).fill(null).map(() => new Array(size).fill(null).map(() => ({ r: 255, g: 255, b: 255, a: 255 })) ); } createProject(req, res) { try { const { userId, title, canvasSize, isPublic, pixels, layers } = req.body; const size = canvasSize || 64; const project = { id: this.generateId(), userId: userId || 'guest', title: title || 'Untitled', description: req.body.description || '', canvasSize: size, pixels: pixels || this.createEmptyCanvas(size), layers: layers || [{ id: 'layer_1', name: 'Layer 1', visible: true, opacity: 1 }], currentLayer: 0, isPublic: isPublic || false, isAnimation: req.body.isAnimation || false, animationFrames: req.body.animationFrames || [], fps: req.body.fps || 12, ratings: [], createdAt: new Date(), updatedAt: new Date() }; this.projects.set(project.id, project); return res.json(project); } catch (error) { console.error('Create project error:', error); return res.status(500).json({ error: 'Failed to create project' }); } } getUserProjects(req, res) { try { const { userId } = req.params; const result = []; for (let project of this.projects.values()) { if (project.userId === userId) { result.push(project); } } return res.json(result); } catch (error) { console.error('Get projects error:', error); return res.status(500).json({ error: 'Failed to load projects' }); } } getProject(req, res) { try { const { id } = req.params; const project = this.projects.get(id); if (!project) { return res.status(404).json({ error: 'Project not found' }); } return res.json(project); } catch (error) { console.error('Get project error:', error); return res.status(500).json({ error: 'Failed to load project' }); } } updateProject(req, res) { try { const { id } = req.params; const project = this.projects.get(id); if (!project) { return res.status(404).json({ error: 'Project not found' }); } const allowedFields = ['title', 'description', 'pixels', 'layers', 'currentLayer', 'isPublic', 'isAnimation', 'animationFrames', 'fps']; for (let field of allowedFields) { if (req.body[field] !== undefined) { project[field] = req.body[field]; } } project.updatedAt = new Date(); this.projects.set(id, project); return res.json(project); } catch (error) { console.error('Update project error:', error); return res.status(500).json({ error: 'Failed to update project' }); } } deleteProject(req, res) { try { const { id } = req.params; const result = this.projects.delete(id); if (!result) { return res.status(404).json({ error: 'Project not found' }); } return res.json({ success: true }); } catch (error) { console.error('Delete project error:', error); return res.status(500).json({ error: 'Failed to delete project' }); } } } module.exports = new ProjectService();