Skip to content

Commit 04fe74d

Browse files
committed
Refactor, use body parser, adjust update api content column
Signed-off-by: James Tsai <jamesscamel@gmail.com>
1 parent 53526c1 commit 04fe74d

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

lib/note/index.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const { newCheckViewPermission, errorForbidden, responseCodiMD, errorNotFound, e
88
const { updateHistory, historyDelete } = require('../history')
99
const { actionPublish, actionSlide, actionInfo, actionDownload, actionPDF, actionGist, actionRevision, actionPandoc } = require('./noteActions')
1010
const realtime = require('../realtime/realtime')
11-
const moment = require('moment')
1211

1312
async function getNoteById (noteId, { includeUser } = { includeUser: false }) {
1413
const id = await Note.parseNoteIdAsync(noteId)
@@ -291,7 +290,7 @@ const updateNote = async (req, res) => {
291290
const content = req.body.content
292291
const updated = await note.update({
293292
content: content,
294-
lastchangeAt: moment(now).format('YYYY-MM-DD HH:mm:ss'),
293+
lastchangeAt: now,
295294
authorship: [
296295
[
297296
req.user.id,
@@ -309,14 +308,17 @@ const updateNote = async (req, res) => {
309308
}
310309

311310
Revision.saveNoteRevision(note, (err, revision) => {
312-
if (err) return errorInternalError(req, res)
311+
if (err) {
312+
logger.error(err)
313+
return errorInternalError(req, res)
314+
}
313315
if (!revision) return errorNotFound(req, res)
314-
})
315-
316-
res.send({
317-
status: 'ok'
316+
res.send({
317+
status: 'ok'
318+
})
318319
})
319320
} catch (err) {
321+
logger.error(err)
320322
logger.error('Update note failed: Internal Error.')
321323
return errorInternalError(req, res)
322324
}

lib/routes.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const historyController = require('./history')
1212
const userController = require('./user')
1313
const noteController = require('./note')
1414
const response = require('./response')
15+
const bodyParser = require('body-parser')
1516
const appRouter = Router()
1617

1718
// register route
@@ -75,7 +76,7 @@ appRouter.get('/api/notes/myNotes', noteController.listMyNotes)
7576
// delete note by id
7677
appRouter.delete('/api/notes/:noteId', noteController.deleteNote)
7778
// update note content by id
78-
appRouter.put('/api/notes/:noteId', urlencodedParser, noteController.updateNote)
79+
appRouter.put('/api/notes/:noteId', bodyParser.json(), noteController.updateNote)
7980
// get note by id
8081
appRouter.get('/:noteId', wrap(noteController.showNote))
8182
// note actions

0 commit comments

Comments
 (0)