Move note actions to their own file.
Because of circular import problems, this commit also moves the error messages from response.js to errors.js Signed-off-by: David Mehren <dmehren1@gmail.com>
This commit is contained in:
@@ -5,8 +5,8 @@ var LZString = require('lz-string')
|
||||
|
||||
// core
|
||||
var logger = require('./logger')
|
||||
var response = require('./response')
|
||||
var models = require('./models')
|
||||
const errors = require('./errors')
|
||||
|
||||
// public
|
||||
var History = {
|
||||
@@ -121,14 +121,14 @@ function parseHistoryToObject (history) {
|
||||
function historyGet (req, res) {
|
||||
if (req.isAuthenticated()) {
|
||||
getHistory(req.user.id, function (err, history) {
|
||||
if (err) return response.errorInternalError(res)
|
||||
if (!history) return response.errorNotFound(res)
|
||||
if (err) return errors.errorInternalError(res)
|
||||
if (!history) return errors.errorNotFound(res)
|
||||
res.send({
|
||||
history: parseHistoryToArray(history)
|
||||
})
|
||||
})
|
||||
} else {
|
||||
return response.errorForbidden(res)
|
||||
return errors.errorForbidden(res)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -136,40 +136,40 @@ function historyPost (req, res) {
|
||||
if (req.isAuthenticated()) {
|
||||
var noteId = req.params.noteId
|
||||
if (!noteId) {
|
||||
if (typeof req.body['history'] === 'undefined') return response.errorBadRequest(res)
|
||||
if (typeof req.body['history'] === 'undefined') return errors.errorBadRequest(res)
|
||||
logger.debug(`SERVER received history from [${req.user.id}]: ${req.body.history}`)
|
||||
try {
|
||||
var history = JSON.parse(req.body.history)
|
||||
} catch (err) {
|
||||
return response.errorBadRequest(res)
|
||||
return errors.errorBadRequest(res)
|
||||
}
|
||||
if (Array.isArray(history)) {
|
||||
setHistory(req.user.id, history, function (err, count) {
|
||||
if (err) return response.errorInternalError(res)
|
||||
if (err) return errors.errorInternalError(res)
|
||||
res.end()
|
||||
})
|
||||
} else {
|
||||
return response.errorBadRequest(res)
|
||||
return errors.errorBadRequest(res)
|
||||
}
|
||||
} else {
|
||||
if (typeof req.body['pinned'] === 'undefined') return response.errorBadRequest(res)
|
||||
if (typeof req.body['pinned'] === 'undefined') return errors.errorBadRequest(res)
|
||||
getHistory(req.user.id, function (err, history) {
|
||||
if (err) return response.errorInternalError(res)
|
||||
if (!history) return response.errorNotFound(res)
|
||||
if (!history[noteId]) return response.errorNotFound(res)
|
||||
if (err) return errors.errorInternalError(res)
|
||||
if (!history) return errors.errorNotFound(res)
|
||||
if (!history[noteId]) return errors.errorNotFound(res)
|
||||
if (req.body.pinned === 'true' || req.body.pinned === 'false') {
|
||||
history[noteId].pinned = (req.body.pinned === 'true')
|
||||
setHistory(req.user.id, history, function (err, count) {
|
||||
if (err) return response.errorInternalError(res)
|
||||
if (err) return errors.errorInternalError(res)
|
||||
res.end()
|
||||
})
|
||||
} else {
|
||||
return response.errorBadRequest(res)
|
||||
return errors.errorBadRequest(res)
|
||||
}
|
||||
})
|
||||
}
|
||||
} else {
|
||||
return response.errorForbidden(res)
|
||||
return errors.errorForbidden(res)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -178,22 +178,22 @@ function historyDelete (req, res) {
|
||||
var noteId = req.params.noteId
|
||||
if (!noteId) {
|
||||
setHistory(req.user.id, [], function (err, count) {
|
||||
if (err) return response.errorInternalError(res)
|
||||
if (err) return errors.errorInternalError(res)
|
||||
res.end()
|
||||
})
|
||||
} else {
|
||||
getHistory(req.user.id, function (err, history) {
|
||||
if (err) return response.errorInternalError(res)
|
||||
if (!history) return response.errorNotFound(res)
|
||||
if (err) return errors.errorInternalError(res)
|
||||
if (!history) return errors.errorNotFound(res)
|
||||
delete history[noteId]
|
||||
setHistory(req.user.id, history, function (err, count) {
|
||||
if (err) return response.errorInternalError(res)
|
||||
if (err) return errors.errorInternalError(res)
|
||||
res.end()
|
||||
})
|
||||
})
|
||||
}
|
||||
} else {
|
||||
return response.errorForbidden(res)
|
||||
return errors.errorForbidden(res)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user