Refactor, use body parser, adjust update api content column

Signed-off-by: James Tsai <jamesscamel@gmail.com>
This commit is contained in:
James Tsai 2020-07-30 18:13:35 +08:00
parent 53526c154a
commit 04fe74d520
2 changed files with 11 additions and 8 deletions

View File

@ -8,7 +8,6 @@ const { newCheckViewPermission, errorForbidden, responseCodiMD, errorNotFound, e
const { updateHistory, historyDelete } = require('../history')
const { actionPublish, actionSlide, actionInfo, actionDownload, actionPDF, actionGist, actionRevision, actionPandoc } = require('./noteActions')
const realtime = require('../realtime/realtime')
const moment = require('moment')
async function getNoteById (noteId, { includeUser } = { includeUser: false }) {
const id = await Note.parseNoteIdAsync(noteId)
@ -291,7 +290,7 @@ const updateNote = async (req, res) => {
const content = req.body.content
const updated = await note.update({
content: content,
lastchangeAt: moment(now).format('YYYY-MM-DD HH:mm:ss'),
lastchangeAt: now,
authorship: [
[
req.user.id,
@ -309,14 +308,17 @@ const updateNote = async (req, res) => {
}
Revision.saveNoteRevision(note, (err, revision) => {
if (err) return errorInternalError(req, res)
if (err) {
logger.error(err)
return errorInternalError(req, res)
}
if (!revision) return errorNotFound(req, res)
})
res.send({
status: 'ok'
res.send({
status: 'ok'
})
})
} catch (err) {
logger.error(err)
logger.error('Update note failed: Internal Error.')
return errorInternalError(req, res)
}

View File

@ -12,6 +12,7 @@ const historyController = require('./history')
const userController = require('./user')
const noteController = require('./note')
const response = require('./response')
const bodyParser = require('body-parser')
const appRouter = Router()
// register route
@ -75,7 +76,7 @@ appRouter.get('/api/notes/myNotes', noteController.listMyNotes)
// delete note by id
appRouter.delete('/api/notes/:noteId', noteController.deleteNote)
// update note content by id
appRouter.put('/api/notes/:noteId', urlencodedParser, noteController.updateNote)
appRouter.put('/api/notes/:noteId', bodyParser.json(), noteController.updateNote)
// get note by id
appRouter.get('/:noteId', wrap(noteController.showNote))
// note actions