<commit_before>// controllers/posts.js
// Copyright (C) 2014 <NAME> <<EMAIL>>

var mongoose = require('mongoose');
var Posts = mongoose.model('Posts');
var Paginator = require('robcolbert-utils').expressjs.Paginator;

exports.list = function(req, res){
  var query = Posts.find(req.query, 'created title content excerpt').lean(true);
  var paginator = new Paginator(req);
  paginator.paginateQuery(query).sort({'created':-1}).exec(function (err, posts) {
    if (err) {
      res.json(500, err);
      return;
    }
    res.json(200, posts);
  });
};

exports.create = function (req, res) {
  var post = new Posts(req.body);
  post.save(function (err, newPost) {
    if (err) {
      res.json(500, err);
      console.log('posts.create error', err);
      return;
    }
    res.json(200, newPost);
  });
};

exports.get = function (req, res) {
  Posts.findById(req.route.params.postId, function (err, post) {
    if (err) {
      res.json(500, err);
      return;
    }
    res.json(200, post);
  });
};

exports.update = function (req, res) {
  delete req.body._id;
  console.log('posts.update', req.body);
  Posts.findByIdAndUpdate(req.route.params.postId, req.body, function (err, post) {
    if (err) {
      console.log(err);
      res.json(500, err);
      return;
    }
    res.json(200, post);
  });
};

exports.delete = function (req, res) {
  Posts.remove(req.body, function (err) {
    if (err) {
      res.json(500, err);
      return;
    }
    res.json(200);
  });
};

exports.createComment = function (req, res) {
  Posts.findById(req.route.params.postId, function (err, post) {
    if (err) {
      res.json(500, err);
      return;
    }
    post.comments.push(req.body);
    post.save(function (err, newPost) {
      if (err) {
        res.json(500, err);
        console.log(err);
        return;
      }
      res.json(200, newPost.comments[newPost.comments.length - 1]);
    });
  });
};

exports.getComments = function (req, res) {
  var paginator = new Paginator(req);
  var query = Posts.findById(req.route.params.postId, 'comments').lean(true);
  paginator.paginateQuery(query).sort({'posted':1}).exec(function (err, post) {
    if (err) {
      res.json(500, err);
      return;
    }
    res.json(200, post.comments);
  });
};
<commit_msg>Bug fix for posting a comment to a post that does not exist.<commit_after>// controllers/posts.js
// Copyright (C) 2014 <NAME> <<EMAIL>>

var mongoose = require('mongoose');
var Posts = mongoose.model('Posts');
var Paginator = require('robcolbert-utils').expressjs.Paginator;

exports.list = function(req, res){
  var query = Posts.find(req.query, 'created title content excerpt').lean(true);
  var paginator = new Paginator(req);
  paginator.paginateQuery(query).sort({'created':-1}).exec(function (err, posts) {
    if (err) {
      res.json(500, err);
      return;
    }
    res.json(200, posts);
  });
};

exports.create = function (req, res) {
  var post = new Posts(req.body);
  post.save(function (err, newPost) {
    if (err) {
      res.json(500, err);
      console.log('posts.create error', err);
      return;
    }
    res.json(200, newPost);
  });
};

exports.get = function (req, res) {
  Posts.findById(req.route.params.postId, function (err, post) {
    if (err) {
      res.json(500, err);
      return;
    }
    res.json(200, post);
  });
};

exports.update = function (req, res) {
  delete req.body._id;
  console.log('posts.update', req.body);
  Posts.findByIdAndUpdate(req.route.params.postId, req.body, function (err, post) {
    if (err) {
      console.log(err);
      res.json(500, err);
      return;
    }
    res.json(200, post);
  });
};

exports.delete = function (req, res) {
  Posts.remove(req.body, function (err) {
    if (err) {
      res.json(500, err);
      return;
    }
    res.json(200);
  });
};

exports.createComment = function (req, res) {
  Posts.findById(req.route.params.postId, function (err, post) {
    if (err) {
      res.json(500, err);
      return;
    }
    if (!post) {
      res.json(404, {'msg':'post not found'});
      return;
    }
    post.comments.push(req.body);
    post.save(function (err, newPost) {
      if (err) {
        res.json(500, err);
        console.log(err);
        return;
      }
      res.json(200, newPost.comments[newPost.comments.length - 1]);
    });
  });
};

exports.getComments = function (req, res) {
  var paginator = new Paginator(req);
  var query = Posts.findById(req.route.params.postId, 'comments').lean(true);
  paginator.paginateQuery(query).sort({'posted':1}).exec(function (err, post) {
    if (err) {
      res.json(500, err);
      return;
    }
    res.json(200, post.comments);
  });
};

{'fraction_non_alphanumeric': 0.17520909285867467, 'fraction_numerical': 0.0229466009007077, 'mean_word_length': 2.8609271523178808, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2106131', 'n_tokens_mistral': 1745, 'n_tokens_neox': 1648, 'n_words': 335}