/** * API Functions * Backend communication layer */ /** * Fetch entries from backend */ async function fetchEntries(fromDate = null, toDate = null) { try { let url = '/api/entries'; const params = new URLSearchParams(); if (fromDate) params.append('from', fromDate); if (toDate) params.append('to', toDate); if (params.toString()) { url += '?' + params.toString(); } const response = await fetch(url); if (!response.ok) { throw new Error('Failed to fetch entries'); } const entries = await response.json(); return entries; } catch (error) { console.error('Error fetching entries:', error); showNotification('Fehler beim Laden der Einträge', 'error'); return []; } } /** * Create a new entry */ async function createEntry(date, startTime, endTime, pauseMinutes, location) { try { const body = { date: formatDateISO(date), startTime, endTime, location: location || 'office' }; // Only include pauseMinutes if explicitly provided (not empty) if (pauseMinutes !== null && pauseMinutes !== undefined && pauseMinutes !== '') { body.pauseMinutes = parseInt(pauseMinutes); } const response = await fetch('/api/entries', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) }); if (!response.ok) { const error = await response.json(); throw new Error(error.error || 'Failed to create entry'); } const entry = await response.json(); return entry; } catch (error) { console.error('Error creating entry:', error); showNotification(error.message || 'Fehler beim Erstellen des Eintrags', 'error'); return null; } } /** * Update an existing entry */ async function updateEntry(id, date, startTime, endTime, pauseMinutes, location) { try { const body = { date: formatDateISO(date), startTime, endTime, location: location || 'office' }; // Only include pauseMinutes if explicitly provided (not empty) if (pauseMinutes !== null && pauseMinutes !== undefined && pauseMinutes !== '') { body.pauseMinutes = parseInt(pauseMinutes); } const response = await fetch(`/api/entries/${id}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) }); if (!response.ok) { const error = await response.json(); throw new Error(error.error || 'Failed to update entry'); } const entry = await response.json(); return entry; } catch (error) { console.error('Error updating entry:', error); showNotification(error.message || 'Fehler beim Aktualisieren des Eintrags', 'error'); return null; } } /** * Delete an entry */ async function deleteEntry(id) { try { const response = await fetch(`/api/entries/${id}`, { method: 'DELETE' }); if (!response.ok) { throw new Error('Failed to delete entry'); } return true; } catch (error) { console.error('Error deleting entry:', error); showNotification('Fehler beim Löschen des Eintrags', 'error'); return false; } } /** * Get a setting by key */ async function getSetting(key) { try { const response = await fetch(`/api/settings/${key}`); if (!response.ok) { if (response.status === 404) { return null; // Setting doesn't exist yet } throw new Error('Failed to get setting'); } const data = await response.json(); return data.value; } catch (error) { console.error('Error getting setting:', error); return null; } } /** * Set a setting */ async function setSetting(key, value) { try { const response = await fetch('/api/settings', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ key, value }) }); if (!response.ok) { throw new Error('Failed to set setting'); } return true; } catch (error) { console.error('Error setting setting:', error); showNotification('Fehler beim Speichern der Einstellung', 'error'); return false; } }