/** * TASKMATE - Applications Route * ============================== * API-Endpoints für Anwendungs-/Repository-Verwaltung */ const express = require('express'); const router = express.Router(); const { getDb } = require('../database'); const logger = require('../utils/logger'); const gitService = require('../services/gitService'); /** * GET /api/applications/:projectId * Anwendungs-Konfiguration für ein Projekt abrufen */ router.get('/:projectId', (req, res) => { try { const { projectId } = req.params; const db = getDb(); const application = db.prepare(` SELECT a.*, p.name as project_name FROM applications a JOIN projects p ON a.project_id = p.id WHERE a.project_id = ? `).get(projectId); if (!application) { return res.json({ configured: false, projectId: parseInt(projectId) }); } // Prüfe ob das Repository existiert und erreichbar ist const isRepo = gitService.isGitRepository(application.local_path); const isAccessible = gitService.isPathAccessible(application.local_path); res.json({ configured: true, ...application, isRepository: isRepo, isAccessible }); } catch (error) { logger.error('Fehler beim Abrufen der Anwendung:', error); res.status(500).json({ error: 'Serverfehler' }); } }); /** * POST /api/applications * Anwendung für ein Projekt erstellen oder aktualisieren */ router.post('/', (req, res) => { try { const { projectId, localPath, giteaRepoUrl, giteaRepoOwner, giteaRepoName, defaultBranch } = req.body; const userId = req.user.id; const db = getDb(); if (!projectId || !localPath) { return res.status(400).json({ error: 'projectId und localPath sind erforderlich' }); } // Prüfe ob der Pfad erreichbar ist if (!gitService.isPathAccessible(localPath)) { return res.status(400).json({ error: 'Pfad nicht erreichbar. Stelle sicher, dass das Laufwerk in Docker gemountet ist.', hint: 'Gemountete Laufwerke: C:, D:, E:' }); } // Prüfe ob bereits eine Anwendung für dieses Projekt existiert const existing = db.prepare('SELECT id FROM applications WHERE project_id = ?').get(projectId); if (existing) { // Update db.prepare(` UPDATE applications SET local_path = ?, gitea_repo_url = ?, gitea_repo_owner = ?, gitea_repo_name = ?, default_branch = ? WHERE project_id = ? `).run(localPath, giteaRepoUrl || null, giteaRepoOwner || null, giteaRepoName || null, defaultBranch || 'main', projectId); logger.info(`Anwendung aktualisiert für Projekt ${projectId}`); } else { // Insert db.prepare(` INSERT INTO applications (project_id, local_path, gitea_repo_url, gitea_repo_owner, gitea_repo_name, default_branch, created_by) VALUES (?, ?, ?, ?, ?, ?, ?) `).run(projectId, localPath, giteaRepoUrl || null, giteaRepoOwner || null, giteaRepoName || null, defaultBranch || 'main', userId); logger.info(`Anwendung erstellt für Projekt ${projectId}`); } // Anwendung zurückgeben const application = db.prepare(` SELECT a.*, p.name as project_name FROM applications a JOIN projects p ON a.project_id = p.id WHERE a.project_id = ? `).get(projectId); res.json({ success: true, application }); } catch (error) { logger.error('Fehler beim Speichern der Anwendung:', error); res.status(500).json({ error: 'Serverfehler' }); } }); /** * DELETE /api/applications/:projectId * Anwendungs-Konfiguration entfernen */ router.delete('/:projectId', (req, res) => { try { const { projectId } = req.params; const db = getDb(); const result = db.prepare('DELETE FROM applications WHERE project_id = ?').run(projectId); if (result.changes === 0) { return res.status(404).json({ error: 'Keine Anwendung für dieses Projekt gefunden' }); } logger.info(`Anwendung gelöscht für Projekt ${projectId}`); res.json({ success: true }); } catch (error) { logger.error('Fehler beim Löschen der Anwendung:', error); res.status(500).json({ error: 'Serverfehler' }); } }); /** * GET /api/applications/user/base-path * Basis-Pfad des aktuellen Benutzers abrufen */ router.get('/user/base-path', (req, res) => { try { const userId = req.user.id; const db = getDb(); const user = db.prepare('SELECT repositories_base_path FROM users WHERE id = ?').get(userId); res.json({ basePath: user?.repositories_base_path || null, configured: !!user?.repositories_base_path }); } catch (error) { logger.error('Fehler beim Abrufen des Basis-Pfads:', error); res.status(500).json({ error: 'Serverfehler' }); } }); /** * PUT /api/applications/user/base-path * Basis-Pfad des aktuellen Benutzers setzen */ router.put('/user/base-path', (req, res) => { try { const { basePath } = req.body; const userId = req.user.id; const db = getDb(); if (!basePath) { return res.status(400).json({ error: 'basePath ist erforderlich' }); } // Prüfe ob der Pfad erreichbar ist if (!gitService.isPathAccessible(basePath)) { return res.status(400).json({ error: 'Pfad nicht erreichbar. Stelle sicher, dass das Laufwerk in Docker gemountet ist.', hint: 'Gemountete Laufwerke: C:, D:, E:' }); } db.prepare('UPDATE users SET repositories_base_path = ? WHERE id = ?').run(basePath, userId); logger.info(`Basis-Pfad gesetzt für Benutzer ${userId}: ${basePath}`); res.json({ success: true, basePath }); } catch (error) { logger.error('Fehler beim Setzen des Basis-Pfads:', error); res.status(500).json({ error: 'Serverfehler' }); } }); /** * POST /api/applications/:projectId/sync * Synchronisierungszeitpunkt aktualisieren */ router.post('/:projectId/sync', (req, res) => { try { const { projectId } = req.params; const db = getDb(); db.prepare('UPDATE applications SET last_sync = CURRENT_TIMESTAMP WHERE project_id = ?').run(projectId); res.json({ success: true }); } catch (error) { logger.error('Fehler beim Aktualisieren der Synchronisierung:', error); res.status(500).json({ error: 'Serverfehler' }); } }); module.exports = router;