|
@@ -4,7 +4,115 @@
|
|
* Backend module to store React-Redux components in MongoDB.
|
|
* Backend module to store React-Redux components in MongoDB.
|
|
*/
|
|
*/
|
|
import mongoose from 'mongoose'
|
|
import mongoose from 'mongoose'
|
|
-mongoose.Promise = global.Promise
|
|
|
|
|
|
+import { Router } from 'express'
|
|
|
|
+mongoose.Promise = Promise
|
|
|
|
+
|
|
|
|
+const MAX_RESULTS = 100
|
|
|
|
+
|
|
|
|
+class RESTController {
|
|
|
|
+ constructor (model, key) {
|
|
|
|
+ this.model = model
|
|
|
|
+ this.modelName = model.modelName.toLowerCase()
|
|
|
|
+ this.key = key
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ create (data) {
|
|
|
|
+ return this.model
|
|
|
|
+ .create(data)
|
|
|
|
+ .then(instance => {
|
|
|
|
+ const response = {}
|
|
|
|
+ response[this.modelName] = instance
|
|
|
|
+ return response
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ read (id) {
|
|
|
|
+ const filter = {}
|
|
|
|
+ filter[this.key] = id
|
|
|
|
+ return this.model
|
|
|
|
+ .findOne(filter)
|
|
|
|
+ .then(instance => {
|
|
|
|
+ const response = {}
|
|
|
|
+ response[this.modelName] = instance
|
|
|
|
+ return response
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ list () {
|
|
|
|
+ return this.model
|
|
|
|
+ .find({})
|
|
|
|
+ .limit(MAX_RESULTS)
|
|
|
|
+ .then(instance => {
|
|
|
|
+ const response = {}
|
|
|
|
+ response[this.modelName] = instance
|
|
|
|
+ return response
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ update (id, data) {
|
|
|
|
+ const filter = {}
|
|
|
|
+ filter[this.key] = id
|
|
|
|
+
|
|
|
|
+ return this.model
|
|
|
|
+ .findOne(filter)
|
|
|
|
+ .then(instance => {
|
|
|
|
+ for (let attribute in data) {
|
|
|
|
+ if (data.hasOwnProperty(attribute) && attribute !== this.key && attribute !== '_id') {
|
|
|
|
+ instance[attribute] = data[attribute]
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return instance.save()
|
|
|
|
+ })
|
|
|
|
+ .then(instance => {
|
|
|
|
+ const response = {}
|
|
|
|
+ response[this.modelName] = instance
|
|
|
|
+ return response
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ route () {
|
|
|
|
+ const router = new Router()
|
|
|
|
+
|
|
|
|
+ router.get('/', (req, res) => {
|
|
|
|
+ this
|
|
|
|
+ .list()
|
|
|
|
+ .then(data => res.json(data))
|
|
|
|
+ .then(null, error => res.status(404).send(error))
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ router.post('/', (req, res) => {
|
|
|
|
+ this
|
|
|
|
+ .create(req.body)
|
|
|
|
+ .then(data => res.json(data))
|
|
|
|
+ .then(null, error => res.status(404).send(error))
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ router.get('/:key', (req, res) => {
|
|
|
|
+ this
|
|
|
|
+ .read(req.params.key)
|
|
|
|
+ .then(data => res.json(data))
|
|
|
|
+ .then(null, error => res.status(404).send(error))
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ router.put('/:key', (req, res) => {
|
|
|
|
+ this
|
|
|
|
+ .update(req.params.key, req.body)
|
|
|
|
+ .then(data => res.json(data))
|
|
|
|
+ .then(null, error => res.status(404).send(error))
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ router.delete('/:key', (req, res) => {
|
|
|
|
+ this
|
|
|
|
+ .delete(req.params.key)
|
|
|
|
+ .then(data => res.json(data))
|
|
|
|
+ .then(null, error => res.status(404).send(error))
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ return router
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+export default RESTController
|
|
|
|
|
|
/**
|
|
/**
|
|
* Generates GET, POST, UPDATE and DELETE handlers which interact with
|
|
* Generates GET, POST, UPDATE and DELETE handlers which interact with
|
|
@@ -97,7 +205,7 @@ function routeGen (Model, versioned = true) {
|
|
console.log('in save', savedItem)
|
|
console.log('in save', savedItem)
|
|
res.status(200)
|
|
res.status(200)
|
|
res.send({ _id: savedItem._id })
|
|
res.send({ _id: savedItem._id })
|
|
- }, err => {
|
|
|
|
|
|
+ }).catch(err => {
|
|
console.log('in save', err)
|
|
console.log('in save', err)
|
|
res.status(422)
|
|
res.status(422)
|
|
res.send(err)
|
|
res.send(err)
|
|
@@ -192,4 +300,4 @@ function routeGen (Model, versioned = true) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-export default routeGen
|
|
|
|
|
|
+export { routeGen }
|