Created
November 11, 2014 09:22
-
-
Save chriswitko/cfed68bd15a5c31c824c to your computer and use it in GitHub Desktop.
Comments controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var _ = require('lodash'); | |
| var async = require('async'); | |
| var crypto = require('crypto'); | |
| var nodemailer = require('nodemailer'); | |
| var passport = require('passport'); | |
| var secrets = require('../config/secrets'); | |
| var moment = require('moment'); | |
| var User = require('../models/User'); | |
| var Product = require('../models/Product'); | |
| var Comment = require('../models/Comment'); | |
| exports.postCreate = function(req, res) { | |
| var locales = {} | |
| async.series({ | |
| createComment: function(done) { | |
| locales.comment = new Comment(); | |
| locales.comment.user = req.body.user; | |
| locales.comment.product = req.body.product; | |
| locales.comment.body = req.body.body; | |
| locales.comment.save(function() { | |
| done(); | |
| }); | |
| }, | |
| updateProduct: function(done) { | |
| Product.update({_id: req.body.product}, {$inc: {'meta.comments': 1}}).exec(function() { | |
| return done(); | |
| }) | |
| } | |
| }, function() { | |
| res.json({ | |
| code: 200, | |
| status: 'success', | |
| comment_id: locales.comment._id | |
| }) | |
| }) | |
| } | |
| exports.list = function(req, res) { | |
| var locales = {} | |
| var criteria = {} | |
| var page = req.query.page || 1 | |
| var limit = req.query.limit || 12 | |
| async.series({ | |
| getComment: function(done) { | |
| if(req.query.product_id) criteria.product = req.query.product_id; | |
| Comment.paginate(criteria, page, limit, function(err, pages, comments, total) { | |
| locales.total = total; | |
| locales.pages = pages; | |
| if(comments) locales.comments = _.map(comments, function(comment) { | |
| comment = comment.toJSON(); | |
| comment.fromNow = moment(comment.createdAt).fromNow() | |
| return {comment: comment} | |
| }) | |
| done() | |
| }, {populate: ['user','product'], sortBy: { createdAt : -1 }}) | |
| } | |
| }, function() { | |
| res.json({ | |
| code: 200, | |
| status: 'success', | |
| comments: locales.comments, | |
| paginate: { | |
| total: locales.total, | |
| pages: locales.pages | |
| } | |
| }); | |
| }) | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment