Skip to content

Commit 66d5395

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

File tree

3 files changed

+98
-68
lines changed

3 files changed

+98
-68
lines changed

lib/note/index.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const logger = require('../logger')
55
const { Note, User } = require('../models')
66

77
const { newCheckViewPermission, errorForbidden, responseCodiMD, errorNotFound, errorInternalError } = require('../response')
8-
const { updateHistory } = require('../history')
8+
const { updateHistory, historyDelete } = require('../history')
99
const { actionPublish, actionSlide, actionInfo, actionDownload, actionPDF, actionGist, actionRevision, actionPandoc } = require('./noteActions')
1010

1111
async function getNoteById (noteId, { includeUser } = { includeUser: false }) {
@@ -232,7 +232,35 @@ function listMyNotes (req, res) {
232232
}
233233
}
234234

235+
const deleteNote = async (req, res) => {
236+
if(req.isAuthenticated()) {
237+
const noteId = await Note.parseNoteIdAsync(req.params.noteId)
238+
try {
239+
const destroyed = await Note.destroy({
240+
where: {
241+
id: noteId,
242+
ownerId: req.user.id
243+
}
244+
})
245+
if (!destroyed) {
246+
logger.error('Delete note failed: Make sure the noteId and ownerId are correct.')
247+
return errorNotFound(req, res)
248+
}
249+
historyDelete(req, res)
250+
res.send({
251+
status: 'ok'
252+
})
253+
} catch (err) {
254+
logger.error('Delete note failed: Internal Error.')
255+
return errorInternalError(req, res)
256+
}
257+
} else {
258+
return errorForbidden(req, res)
259+
}
260+
}
261+
235262
exports.showNote = showNote
236263
exports.showPublishNote = showPublishNote
237264
exports.noteActions = noteActions
238265
exports.listMyNotes = listMyNotes
266+
exports.deleteNote = deleteNote

lib/routes.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ appRouter.get('/p/:shortid', response.showPublishSlide)
7272
appRouter.get('/p/:shortid/:action', response.publishSlideActions)
7373
// gey my note list
7474
appRouter.get('/api/notes/myNotes', noteController.listMyNotes)
75+
// delete note by id
76+
appRouter.delete('/api/notes/:noteId', noteController.deleteNote)
7577
// get note by id
7678
appRouter.get('/:noteId', wrap(noteController.showNote))
7779
// note actions

0 commit comments

Comments
 (0)