Add update note api

Signed-off-by: James Tsai <jamesscamel@gmail.com>
This commit is contained in:
James Tsai 2020-07-21 09:14:02 +08:00
parent 66d53956c4
commit b597dc9811
2 changed files with 36 additions and 0 deletions

View File

@ -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

View File

@ -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