@@ -5,7 +5,7 @@ const logger = require('../logger')
55const { Note, User } = require ( '../models' )
66
77const { newCheckViewPermission, errorForbidden, responseCodiMD, errorNotFound, errorInternalError } = require ( '../response' )
8- const { updateHistory } = require ( '../history' )
8+ const { updateHistory, historyDelete } = require ( '../history' )
99const { actionPublish, actionSlide, actionInfo, actionDownload, actionPDF, actionGist, actionRevision, actionPandoc } = require ( './noteActions' )
1010
1111async 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+
235262exports . showNote = showNote
236263exports . showPublishNote = showPublishNote
237264exports . noteActions = noteActions
238265exports . listMyNotes = listMyNotes
266+ exports . deleteNote = deleteNote
0 commit comments