/* ============================================================ AURA OS — Uygulama A: Siber Not Defteri POST/GET /api/notes ile backend'e bağlanır ============================================================ */ const NotesApp = { _textarea: null, _listEl: null, _msgEl: null, _saveBtn: null, }; /* API Yardımcıları */ async function apiGetNotes() { const res = await fetch('/api/notes', { method: 'GET' }); if (!res.ok) throw new Error('GET /api/notes -> ' + res.status); const data = await res.json(); return data.notes || []; } async function apiPostNote(text) { const res = await fetch('/api/notes', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ text }), }); if (!res.ok) { const err = await res.json().catch(() => ({})); throw new Error((err && err.error) || 'POST /api/notes -> ' + res.status); } return await res.json(); } /* Not listesini render eder */ function notesRenderList(notes) { const listEl = NotesApp._listEl; if (!listEl) return; if (!notes || notes.length === 0) { listEl.innerHTML = '
Henüz kayıtlı not yok.
'; return; } listEl.innerHTML = ''; notes.forEach(n => { const time = n.createdAt ? new Date(n.createdAt) : null; let dateText = ''; if (time) { const days = ['Pazar','Pazartesi','Salı','Çarşamba','Perşembe','Cuma','Cumartesi']; dateText = days[time.getDay()] + ' ' + String(time.getDate()).padStart(2,'0') + '.' + String(time.getMonth()+1).padStart(2,'0') + '.' + time.getFullYear() + ' ' + String(time.getHours()).padStart(2,'0') + ':' + String(time.getMinutes()).padStart(2,'0'); } const item = document.createElement('div'); item.className = 'note-item'; item.innerHTML = '
' + (dateText ? '
▸ ' + dateText + '
' : ''); item.querySelector('.ni-text').textContent = n.text; listEl.appendChild(item); }); } /* Notları yükle */ async function refreshNotesList() { try { const notes = await apiGetNotes(); notesRenderList(notes); return notes; } catch (err) { notesRenderList([]); return []; } } /* Kaydet butonu */ async function saveCurrentNote() { const textarea = NotesApp._textarea; const msgEl = NotesApp._msgEl; const btn = NotesApp._saveBtn; const text = textarea ? textarea.value : ''; if (!text.trim()) { flashMsg(msgEl, '⚠ Boş not kaydedilemez!', true); if (textarea) textarea.focus(); return; } if (btn) { btn.disabled = true; btn.textContent = '⏳ Kaydediliyor...'; } try { const result = await apiPostNote(text); flashMsg(msgEl, '✓ Not kaydedildi! (' + result.notes.length + ' not)', false); if (textarea) textarea.value = ''; notesRenderList(result.notes); } catch (err) { flashMsg(msgEl, '✗ Hata: ' + err.message, true); } finally { if (btn) { btn.disabled = false; btn.textContent = '💾 Kaydet'; } } } function flashMsg(el, text, isErr) { if (!el) return; el.textContent = text; el.style.color = isErr ? '#ff6b6b' : '#39ff14'; setTimeout(() => { if (el) el.textContent = ''; }, 4000); } /* Uygulama başlat şablonu: DOM'daki