@@ -5,7 +5,7 @@ const logger = require('../logger')
5
5
const { Note, User } = require ( '../models' )
6
6
7
7
const { newCheckViewPermission, errorForbidden, responseCodiMD, errorNotFound, errorInternalError } = require ( '../response' )
8
- const { updateHistory } = require ( '../history' )
8
+ const { updateHistory, historyDelete } = require ( '../history' )
9
9
const { actionPublish, actionSlide, actionInfo, actionDownload, actionPDF, actionGist, actionRevision, actionPandoc } = require ( './noteActions' )
10
10
11
11
async function getNoteById ( noteId , { includeUser } = { includeUser : false } ) {
@@ -232,7 +232,35 @@ function listMyNotes (req, res) {
232
232
}
233
233
}
234
234
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
+
235
262
exports . showNote = showNote
236
263
exports . showPublishNote = showPublishNote
237
264
exports . noteActions = noteActions
238
265
exports . listMyNotes = listMyNotes
266
+ exports . deleteNote = deleteNote
0 commit comments