Skip to content

Commit b597dc9

Browse files
committed
Add update note api
Signed-off-by: James Tsai <jamesscamel@gmail.com>
1 parent 66d5395 commit b597dc9

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

lib/note/index.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,42 @@ const deleteNote = async (req, res) => {
259259
}
260260
}
261261

262+
const updateNote = async (req, res) => {
263+
if(req.isAuthenticated()) {
264+
const noteId = await Note.parseNoteIdAsync(req.params.noteId)
265+
try {
266+
const note = await Note.findOne({
267+
where: {
268+
id: noteId,
269+
}
270+
})
271+
if (!note) {
272+
logger.error('Update note failed: Can\'t find the note.')
273+
return errorNotFound(req, res)
274+
}
275+
276+
const updated = await note.update({
277+
content: req.body.content,
278+
})
279+
if (!updated) {
280+
logger.error('Update note failed: Write data error.')
281+
return errorInternalError(req, res)
282+
}
283+
res.send({
284+
status: 'ok'
285+
})
286+
} catch (err) {
287+
logger.error('Update note failed: Internal Error.')
288+
return errorInternalError(req, res)
289+
}
290+
} else {
291+
return errorForbidden(req, res)
292+
}
293+
}
294+
262295
exports.showNote = showNote
263296
exports.showPublishNote = showPublishNote
264297
exports.noteActions = noteActions
265298
exports.listMyNotes = listMyNotes
266299
exports.deleteNote = deleteNote
300+
exports.updateNote = updateNote

lib/routes.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ appRouter.get('/p/:shortid/:action', response.publishSlideActions)
7474
appRouter.get('/api/notes/myNotes', noteController.listMyNotes)
7575
// delete note by id
7676
appRouter.delete('/api/notes/:noteId', noteController.deleteNote)
77+
// update note content by id
78+
appRouter.put('/api/notes/:noteId', urlencodedParser, noteController.updateNote)
7779
// get note by id
7880
appRouter.get('/:noteId', wrap(noteController.showNote))
7981
// note actions

0 commit comments

Comments
 (0)