diff --git a/lib/note/index.js b/lib/note/index.js index eac2845a..f0e5de20 100644 --- a/lib/note/index.js +++ b/lib/note/index.js @@ -259,8 +259,42 @@ const deleteNote = async (req, res) => { } } +const updateNote = async (req, res) => { + if(req.isAuthenticated()) { + const noteId = await Note.parseNoteIdAsync(req.params.noteId) + try { + const note = await Note.findOne({ + where: { + id: noteId, + } + }) + if (!note) { + logger.error('Update note failed: Can\'t find the note.') + return errorNotFound(req, res) + } + + const updated = await note.update({ + content: req.body.content, + }) + if (!updated) { + logger.error('Update note failed: Write data error.') + return errorInternalError(req, res) + } + res.send({ + status: 'ok' + }) + } catch (err) { + logger.error('Update note failed: Internal Error.') + return errorInternalError(req, res) + } + } else { + return errorForbidden(req, res) + } +} + exports.showNote = showNote exports.showPublishNote = showPublishNote exports.noteActions = noteActions exports.listMyNotes = listMyNotes exports.deleteNote = deleteNote +exports.updateNote = updateNote diff --git a/lib/routes.js b/lib/routes.js index d36d3cdf..744339e5 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -74,6 +74,8 @@ appRouter.get('/p/:shortid/:action', response.publishSlideActions) appRouter.get('/api/notes/myNotes', noteController.listMyNotes) // delete note by id appRouter.delete('/api/notes/:noteId', noteController.deleteNote) +// update note content by id +appRouter.put('/api/notes/:noteId', urlencodedParser, noteController.updateNote) // get note by id appRouter.get('/:noteId', wrap(noteController.showNote)) // note actions