|
@@ -1,23 +1,29 @@
|
|
|
+/**
|
|
|
+ * @module mongoRouter
|
|
|
+ *
|
|
|
+ * Backend module to store React-Redux components in MongoDB.
|
|
|
+ */
|
|
|
import mongoose from 'mongoose'
|
|
|
-import { History } from './basicSchema'
|
|
|
+import { History } from './core/basicSchema'
|
|
|
|
|
|
-function routeGen (model) {
|
|
|
+/**
|
|
|
+ * Generates GET, POST, UPDATE and DELETE handlers which interact with
|
|
|
+ * MongoDB.
|
|
|
+ *
|
|
|
+ * @param {object} Model - mongoose model to use
|
|
|
+ * @param {boolean} versioned - use a revision control mechanism
|
|
|
+ * @return {[type]}
|
|
|
+ */
|
|
|
+function routeGen (Model, versioned = true) {
|
|
|
/** GET handler (request one item or a list of items) */
|
|
|
- function get (req, res, next) {
|
|
|
- const path = req.params['0']
|
|
|
- /** If the path doesn't match, call the next router */
|
|
|
- if (path !== model.modelName.toLowerCase()) {
|
|
|
- next()
|
|
|
- }
|
|
|
+ const get = (req, res) => {
|
|
|
+ let { id } = req.params
|
|
|
|
|
|
- let id = req.params['1']
|
|
|
/** check whether an id was specified. */
|
|
|
if (typeof id === 'string' && id.length > 1) {
|
|
|
- // remove leading slash
|
|
|
- id = id.substring(1)
|
|
|
try {
|
|
|
// try to convert the id string to a valid ObjectId
|
|
|
- id = mongoose.Types.ObjectId(req.params['1'].substring(1))
|
|
|
+ id = mongoose.Types.ObjectId(id)
|
|
|
} catch (err) {
|
|
|
// If id couldn't be converted, return an error message.
|
|
|
res.status(422)
|
|
@@ -25,7 +31,7 @@ function routeGen (model) {
|
|
|
return
|
|
|
}
|
|
|
// if yes, return the one item
|
|
|
- model.findOne({ _id: id }, function (err, item) {
|
|
|
+ Model.findOne({ _id: id }, function (err, item) {
|
|
|
if (err) {
|
|
|
res.status(404)
|
|
|
res.send(err)
|
|
@@ -37,8 +43,8 @@ function routeGen (model) {
|
|
|
// if not, return a list of items
|
|
|
// limit the number of returned items and use an offset
|
|
|
const limit = req.query.limit ? Math.min(Math.max(parseInt(req.query.limit), 1), 100) : 20
|
|
|
- const offset = req.query.offset ? Math.min(Math.max(parseInt(req.query.offset), 1), 100) : 20
|
|
|
- model.find({}, {limit: limit, skip: offset}, function (err, items) {
|
|
|
+ const offset = req.query.offset ? Math.max(parseInt(req.query.offset), 0) : 0
|
|
|
+ Model.find({}, {limit: limit, skip: offset}, function (err, items) {
|
|
|
if (err) {
|
|
|
res.status(404)
|
|
|
res.send(err)
|
|
@@ -50,18 +56,12 @@ function routeGen (model) {
|
|
|
}
|
|
|
|
|
|
/** POST handler (insert a new item into the database) */
|
|
|
- function post (req, res, next) {
|
|
|
- const path = req.params['0']
|
|
|
- // If the path doesn't match, call the next router
|
|
|
- if (path !== model.modelName.toLowerCase()) {
|
|
|
- next()
|
|
|
- }
|
|
|
-
|
|
|
+ const post = (req, res) => {
|
|
|
// create the new item
|
|
|
- const item = new model(req.body)
|
|
|
+ const item = new Model(req.body)
|
|
|
|
|
|
// Check, if the model supports revision control
|
|
|
- if (typeof model.schema.obj.__history !== 'undefined') {
|
|
|
+ if (typeof Model.schema.obj.__history !== 'undefined') {
|
|
|
item.__history = new History({
|
|
|
author: '',
|
|
|
created: Date(),
|
|
@@ -78,19 +78,13 @@ function routeGen (model) {
|
|
|
res.send(err)
|
|
|
return
|
|
|
}
|
|
|
- res.send({ success: `${model.modelName} added.` })
|
|
|
+ res.send({ success: `${Model.modelName} added.` })
|
|
|
})
|
|
|
}
|
|
|
|
|
|
/** PUT handler (update existing project) */
|
|
|
- function put (req, res, next) {
|
|
|
- const path = req.params['0']
|
|
|
- /** If the path doesn't match, call the next router */
|
|
|
- if (path !== model.modelName.toLowerCase()) {
|
|
|
- next()
|
|
|
- }
|
|
|
-
|
|
|
- let id = req.params['1']
|
|
|
+ const put = (req, res) => {
|
|
|
+ let id = req.params['0']
|
|
|
/** check whether an id was specified. */
|
|
|
if (typeof id === 'string' && id.length > 1) {
|
|
|
// remove leading slash
|
|
@@ -107,16 +101,16 @@ function routeGen (model) {
|
|
|
}
|
|
|
|
|
|
// Find the object in the database
|
|
|
- model.findOne({ _id: id }, function (err, item) {
|
|
|
+ Model.findOne({ _id: id }, function (err, item) {
|
|
|
if (err) {
|
|
|
res.status(404)
|
|
|
res.send(err)
|
|
|
}
|
|
|
|
|
|
// Check, if the model supports revision control
|
|
|
- if (typeof model.schema.obj.__history !== 'undefined') {
|
|
|
+ if (typeof Model.schema.obj.__history !== 'undefined') {
|
|
|
// If yes, don't update the item, but create a new one based on the original
|
|
|
- const newItem = model(item)
|
|
|
+ const newItem = Model(item)
|
|
|
// replace the requested elements
|
|
|
for (let prop in req.body) {
|
|
|
newItem[prop] = req.body[prop]
|
|
@@ -137,7 +131,7 @@ function routeGen (model) {
|
|
|
res.status(422)
|
|
|
res.send(err)
|
|
|
}
|
|
|
- res.json({ message: `${model.modelName} updated.` })
|
|
|
+ res.json({ message: `${Model.modelName} updated.` })
|
|
|
})
|
|
|
} else {
|
|
|
for (let prop in req.body) {
|
|
@@ -150,27 +144,29 @@ function routeGen (model) {
|
|
|
res.status(422)
|
|
|
res.send(err)
|
|
|
}
|
|
|
- res.json({ message: `${model.modelName} updated.` })
|
|
|
+ res.json({ message: `${Model.modelName} updated.` })
|
|
|
})
|
|
|
})
|
|
|
}
|
|
|
|
|
|
/** DELETE handler (delete project) */
|
|
|
- function del (req, res, next) {
|
|
|
- const path = req.params['0']
|
|
|
- const id = mongoose.Types.ObjectId(req.params['1'].substring(1))
|
|
|
- /** If the path doesn't match, call the next router */
|
|
|
- if (path !== '/project') {
|
|
|
- next()
|
|
|
- }
|
|
|
+ const del = (req, res) => {
|
|
|
+ const id = mongoose.Types.ObjectId(req.params['0'])
|
|
|
|
|
|
- Project.remove({ _id: id }, function (err, project) {
|
|
|
+ Model.remove({ _id: id }, function (err, project) {
|
|
|
if (err) {
|
|
|
res.send(err)
|
|
|
}
|
|
|
res.json({ message: 'Movie deleted.' })
|
|
|
})
|
|
|
}
|
|
|
+
|
|
|
+ return {
|
|
|
+ get,
|
|
|
+ post,
|
|
|
+ put,
|
|
|
+ del
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
export default routeGen
|