From af09a87c1389a425145cd0f875da849e6b5ba274 Mon Sep 17 00:00:00 2001 From: gaba Date: Fri, 4 Nov 2016 08:44:53 -0700 Subject: [PATCH] Through error instead of process.exit(1) http://eslint.org/docs/rules/no-process-exit --- .gitignore | 1 + bin/init.js | 3 +-- models/action.js | 2 -- models/comment.js | 5 +---- routes/api/comments/index.js | 35 +++++++++++++++++++++++++++++++++-- 5 files changed, 36 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index d52442b57..acd1ba775 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ dist .DS_Store *.iml .env +gaba.cfg diff --git a/bin/init.js b/bin/init.js index d114d4819..2b9e7a943 100644 --- a/bin/init.js +++ b/bin/init.js @@ -2,6 +2,5 @@ const Setting = require('../models/setting'); const defaults = {id: '1', moderation: 'pre'}; Setting.update({id: '1'}, {$setOnInsert: defaults}, {upsert: true}).then(() => { - console.log('created settings object.'); - process.exit(); + throw new Error('It was not able to update settings.'); }).catch(console.error); diff --git a/models/action.js b/models/action.js index 581108a71..a6889814a 100644 --- a/models/action.js +++ b/models/action.js @@ -1,5 +1,3 @@ -'use strict'; - const mongoose = require('../mongoose'); const uuid = require('uuid'); const Schema = mongoose.Schema; diff --git a/models/comment.js b/models/comment.js index 107708a3b..95865c728 100644 --- a/models/comment.js +++ b/models/comment.js @@ -1,5 +1,3 @@ -'use strict'; - const mongoose = require('../mongoose'); const uuid = require('uuid'); const Schema = mongoose.Schema; @@ -13,7 +11,7 @@ const CommentSchema = new Schema({ body: { type: String, required: [true, 'The body is required.'], - minlength: 50 + minlength: 10 }, asset_id: String, author_id: String, @@ -24,7 +22,6 @@ const CommentSchema = new Schema({ }, parent_id: String },{ - _id: false, timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' diff --git a/routes/api/comments/index.js b/routes/api/comments/index.js index dac53b755..a884c01e2 100644 --- a/routes/api/comments/index.js +++ b/routes/api/comments/index.js @@ -1,7 +1,23 @@ const express = require('express'); +const Comment = require('../../../models/comment'); const router = express.Router(); +//============================================================================== +// Validations on parameters +//============================================================================== + +router.param('comment_id', function(res, req, next, comment_id) { + console.log('validations on comment id '); + req.comment_id = comment_id; + next(); +}); + +//============================================================================== +// Routes +//============================================================================== + + router.get('/', (req, res) => { res.send('Read all of the comments ever'); }); @@ -10,8 +26,23 @@ router.get('/:comment_id', (req, res) => { res.send('Read a comment'); }); -router.post('/', (req, res) => { - res.send('Write a comment'); +router.post('/', (req, res, next) => { + let comment = new Comment({ + body: req.query.comment, + author_id: req.query.author_id, + asset_id: req.query.asset_id, + parent_id: req.query.parent_id, + status: req.query.status + }); + comment.save(function(err, comment) { + if(err) { + res.status(500); + return next(err); + } + res.status(201); + res.send(comment.id); + next(); + }); }); router.put('/:comment_id', (req, res) => {