Skip to content

Instantly share code, notes, and snippets.

@irkanu
Last active June 10, 2016 03:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save irkanu/0090c8b42db774d69b5d to your computer and use it in GitHub Desktop.
Save irkanu/0090c8b42db774d69b5d to your computer and use it in GitHub Desktop.
working on comment creation v2
'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
/**
* Article Schema
*/
var ArticleSchema = new Schema({
created: {
type: Date,
default: Date.now
},
title: {
type: String,
default: '',
trim: true,
required: 'Title cannot be blank'
},
imageUrl: {
type: String,
default: '',
trim: true
},
content: {
type: String,
default: '',
trim: true
},
comments: [{
type: Schema.ObjectId,
ref: 'Comment'
}],
user: {
type: Schema.ObjectId,
ref: 'User'
}
});
mongoose.model('Article', ArticleSchema);
'use strict';
/**
* Module dependencies.
*/
var users = require('../../app/controllers/users.server.controller'),
articles = require('../../app/controllers/articles.server.controller'),
comments = require('../../app/controllers/comments.server.controller');
module.exports = function(app) {
// Article Routes
app.route('/articles')
.get(articles.list)
.post(users.requiresLogin, articles.create);
app.route('/articles/:articleId')
.get(articles.read, comments.read)
.put(users.requiresLogin, articles.hasAuthorization, articles.update, comments.update)
.delete(users.requiresLogin, articles.hasAuthorization, articles.delete, comments.delete);
// Finish by binding the article middleware
app.param('articleId', articles.articleByID);
};
'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
/**
* Comment Schema
*/
var CommentSchema = new Schema({
body: {
type: String,
default: '',
required: 'Please fill Comment body.',
trim: true
},
created: {
type: Date,
default: Date.now
},
user: {
type: Schema.ObjectId,
ref: 'User'
},
article: {
type: Schema.ObjectId,
ref: 'Article'
}
});
mongoose.model('Comment', CommentSchema);
// Create new Comment
$scope.create = function() {
// Create new Comment object
var comment = new Comments ({
body: this.body
});
var article = $scope.article;
// Redirect after save
comment.$save(function(response) {
$location.path('articles/' + article._id);
console.log('This is saving via the comments controller...');
// Clear form fields
$scope.body = '';
}, function(errorResponse) {
$scope.error = errorResponse.data.message;
});
};
'use strict';
//Comments service used to communicate Comments REST endpoints
angular.module('comments').factory('Comments', ['$resource',
function($resource) {
return $resource('/articles/:articleId/comments/:commentId', {
// I think I'll need something here to make this work.
commentId: '@_id'
}, {
update: {
method: 'PUT'
}
});
}
]);
/**
* Create a Comment
*/
exports.create = function(req, res) {
var comment = new Comment(req.body);
/**
* attach comment.article to request article
* NOTE: this is currently logging out "undefined"
*/
comment.article = req.article;
console.log('req.article: ' + comment.article );
/**
* attach comment.user to request user
* NOTE: this is logging my user ID perfectly! :)
*/
comment.user = req.user;
console.log('req.user: '+ comment.user);
comment.save(function(err,comment){
if (err) return res.status(400).send({ message: errorHandler.getErrorMessage(err) });
/**
* push comment id to articles comments array
* NOTE: console logging Article.findByIdAndUpdate logs out this:
*
* Comment ID: { __v: 0,
* user: 54df97ae22df47c5052e46aa,
* _id: 550dfd3f0dcfa46e9c18a15b,
* created: Sat Mar 21 2015 19:22:39 GMT-0400 (EDT),
* body: 'test comment' }
* Article ID: undefined
*/
Article.findByIdAndUpdate(
comment.article,
{'$push': {comments : {'_id' : comment._id } } },
function(err,article){
article.save();
},
console.log('article saved via comment.server.controller\nComment ID: ' + comment + '\nArticle ID: ' + comment.article)
);
/**
* This works perfectly
*/
User.findByIdAndUpdate(
comment.user,
{'$push': {comments: {'_id': comment._id}}},
{safe:true},
console.log('user updated via comment.server.controller')
);
res.json(comment);
});
};
'use strict';
module.exports = function(app) {
var users = require('../../app/controllers/users.server.controller');
var comments = require('../../app/controllers/comments.server.controller');
var articles = require('../../app/controllers/articles.server.controller');
// Comments Routes
app.route('/articles/:articleId/comments')
.get(articles.list, comments.list)
.post(users.requiresLogin, articles.create, comments.create);
app.route('/articles/:articleId/comments/:commentId')
.get(articles.read, comments.read)
.put(users.requiresLogin, comments.hasAuthorization, articles.update, comments.update)
.delete(users.requiresLogin, comments.hasAuthorization, articles.delete, comments.delete);
// Finish by binding the Comment middleware
app.param('articleId', articles.articleByID);
app.param('commentId', comments.commentByID);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment