/
Gabryelf
/
Pixel-Orb
Обзор
Документация
Войти
/
Gabryelf
/
Pixel-Orb
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/client/services/projectService.js
112 строк
3 KB
Valeev Serj
restruct_v0.0.1
07 авг 2026, 22:50
07 авг 2026, 22:50
41f78a4
Код
Авторство
О чём код?
class ProjectService { constructor() { this.projects = new Map(); } async createProject(data) { const response = await fetch('/api/projects', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) }); if (!response.ok) { const error = await response.json(); throw new Error(error.error || 'Failed to create project'); } const project = await response.json(); this.projects.set(project.id, project); return project; } async getUserProjects(userId) { const response = await fetch(`/api/projects/${userId}`); if (!response.ok) { const error = await response.json(); throw new Error(error.error || 'Failed to load projects'); } const projects = await response.json(); projects.forEach(p => this.projects.set(p.id, p)); return projects; } async getProject(id) { // Проверяем кеш if (this.projects.has(id)) { return this.projects.get(id); } const response = await fetch(`/api/project/${id}`); if (!response.ok) { const error = await response.json(); throw new Error(error.error || 'Failed to load project'); } const project = await response.json(); this.projects.set(project.id, project); return project; } async updateProject(id, data) { const response = await fetch(`/api/projects/${id}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) }); if (!response.ok) { const error = await response.json(); throw new Error(error.error || 'Failed to update project'); } const project = await response.json(); this.projects.set(project.id, project); return project; } async deleteProject(id) { const response = await fetch(`/api/projects/${id}`, { method: 'DELETE' }); if (!response.ok) { const error = await response.json(); throw new Error(error.error || 'Failed to delete project'); } this.projects.delete(id); return true; } async savePixel(projectId, x, y, color) { const project = this.projects.get(projectId); if (!project) { throw new Error('Project not found'); } if (project.pixels[y] && project.pixels[y][x]) { project.pixels[y][x] = { ...color }; project.updatedAt = new Date(); // Сохраняем на сервере await this.updateProject(projectId, { pixels: project.pixels, updatedAt: project.updatedAt }); return true; } return false; } getLocalProject(id) { return this.projects.get(id); } } const projectService = new ProjectService(); export default projectService;