update routes and paths. add init script

This commit is contained in:
Riley Davis
2016-11-03 13:36:31 -06:00
parent c2d4e1255b
commit afc3e82d60
6 changed files with 25 additions and 7 deletions
+1
View File
@@ -3,3 +3,4 @@ npm-debug.log
dist
.DS_Store
*.iml
.env
+16
View File
@@ -0,0 +1,16 @@
const MongoClient = require('mongodb').MongoClient;
const url = process.env.TALK_MONGO_URL;
if (!url) {
throw new Error('environment variable TALK_MONGO_URL must be defined');
}
MongoClient.connect(url).then(db => {
const Setting = db.collection('settings');
const defaults = {id: 1, moderation: 'pre'};
Setting.update({id: 1}, {$setOnInsert: defaults}, {upsert: true}).then(() => {
console.log('created settings object.');
process.exit();
});
}).catch(console.error);
+3 -3
View File
@@ -3,8 +3,7 @@ const Schema = mongoose.Schema;
const SettingSchema = new Schema({
id: {type: String, default: '1'},
moderation: {type: String, enum: ['pre', 'post'], default: 'pre'},
moderation: {type: String, enum: ['pre', 'post'], default: 'pre'}
}, {
_id: false,
timestamps: {
@@ -18,7 +17,8 @@ SettingSchema.statics.getSettings = function () {
};
SettingSchema.statics.updateSettings = function (setting) {
return this.findOneAndUpdate({}, {$set: setting}, {new: true});
// there should only ever be one record unless something has gone wrong.
return this.findOneAndUpdate({id: '1'}, {$set: setting}, {new: true});
};
const Setting = mongoose.model('Setting', SettingSchema);
+1
View File
@@ -64,6 +64,7 @@
"imports-loader": "^0.6.5",
"json-loader": "^0.5.4",
"mocha": "^3.1.2",
"mongodb": "^2.2.11",
"pre-git": "^3.10.0",
"pym.js": "^1.1.1",
"react": "15.3.2",
+1
View File
@@ -3,5 +3,6 @@ const express = require('express');
const router = express.Router();
router.use('/comments', require('./comments'));
router.use('/settings', require('./settings'));
module.exports = router;
+3 -4
View File
@@ -1,13 +1,12 @@
const express = require('express');
const router = express.Router();
const path = require('path');
const Setting = require(path.resolve(__dirname, 'models/setting'));
const Setting = require('../../../models/setting');
router.get('/settings', (req, res, next) => {
router.get('/', (req, res, next) => {
Setting.getSettings().then(res.json).catch(next);
});
router.put('/settings', (req, res, next) => {
router.put('/', (req, res, next) => {
Setting.updateSettings(req.body).then(res.json).catch(next);
});