diff --git a/public/app.js b/public/app.js index c921bda..037c477 100644 --- a/public/app.js +++ b/public/app.js @@ -222,18 +222,79 @@ const initApp = () => { } knowledgeList.innerHTML = window.conhecimento.facts.map(f => `
-
+
${f.source} -

${f.content}

- ${new Date(f.addedAt).toLocaleDateString('pt-BR')} +

${f.content}

+ ${new Date(f.addedAt).toLocaleDateString('pt-BR')} +
+
+ +
-
`).join(''); }; + // Iniciar edição de item de conhecimento + window.startEditKnowledge = (id) => { + const fact = window.conhecimento.facts.find(f => f.id === id); + if (!fact) return; + + const contentDiv = document.getElementById(`knowledge-content-${id}`); + const actionsDiv = document.getElementById(`knowledge-actions-${id}`); + if (!contentDiv) return; + + if (actionsDiv) actionsDiv.style.display = 'none'; + + contentDiv.innerHTML = ` + ${fact.source} + +
+ + +
+ `; + + const textarea = document.getElementById(`edit-textarea-${id}`); + if (textarea) { + textarea.focus(); + textarea.setSelectionRange(textarea.value.length, textarea.value.length); + } + }; + + // Salvar edição de item de conhecimento + window.saveEditKnowledge = async (id) => { + const textarea = document.getElementById(`edit-textarea-${id}`); + if (!textarea) return; + const newContent = textarea.value.trim(); + if (!newContent) { + alert('O conteúdo do conhecimento não pode ser vazio.'); + return; + } + + try { + const res = await fetch(`/api/conhecimento/${id}`, { + method: 'PUT', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ content: newContent }) + }); + if (res.ok) { + const fact = window.conhecimento.facts.find(f => f.id === id); + if (fact) fact.content = newContent; + renderKnowledgeList(); + } else { + const err = await res.json(); + alert('Erro ao salvar conhecimento: ' + err.error); + } + } catch (e) { + console.error('Erro ao atualizar conhecimento:', e); + alert('Erro de conexão ao salvar conhecimento.'); + } + }; + // Deletar item de conhecimento (função global para onclick) window.deleteKnowledge = async (id) => { + if (!confirm('Deseja realmente remover este conhecimento aprendido?')) return; try { const res = await fetch(`/api/conhecimento/${id}`, { method: 'DELETE' }); if (res.ok) { diff --git a/server.js b/server.js index e1b3710..dde0b97 100644 --- a/server.js +++ b/server.js @@ -185,6 +185,19 @@ app.delete('/api/conhecimento/:id', requireAuth, async (req, res) => { res.json({ success: true }); }); +app.put('/api/conhecimento/:id', requireAuth, async (req, res) => { + const { content } = req.body; + if (!content) return res.status(400).json({ error: 'Conteúdo é obrigatório' }); + try { + const id = parseInt(req.params.id); + await dbPool.query("UPDATE escola.conhecimento SET content = $1 WHERE id = $2;", [content, id]); + res.json({ success: true }); + } catch (err) { + console.error('Erro ao atualizar conhecimento:', err); + res.status(500).json({ error: err.message }); + } +}); + // Analisar mensagem final e extrair conhecimento aprendido (chamado pelo frontend) app.post('/api/conhecimento/extrair', requireAuth, async (req, res) => { const { message } = req.body;