'use strict';

var log4js = require('log4js');
var logger = log4js.getLogger('controllers/post');

var PostModel = require('../models/post');
var Session = require('../util/session');

function PostController() {

}

PostController.prototype.readPost = function(req, res) {
  var postPromise = PostModel.findOne({_id: req.params.id});
  postPromise.then(function(post) {
    if (!Session.isAllow(req, post.author)) {
      return res.status(401).send('permission denied');
    }
    res.status(200).send(post);
  }).catch(function(err) {
    logger.error(err);
    return res.status(400).send(err);
  });
};

PostController.prototype.readPosts = function(req, res) {
  var query = {};
  var result = {};
  var postPromise, postCountPromise;
  if (!Session.hasSession(req)) {
    return res.status(401).send('permission denied');
  }
  query.author = req.session.userId;
  if (req.query.emotionStatus) {
    query.emotionStatus = req.query.emotionStatus;
  }
  postPromise = PostModel.find(query, {}, {sort:{openDate:-1}});
  postCountPromise = PostModel.aggregate([
    {$match: {author: query.author}},
    {$group:{_id:"$emotionStatus",total:{$sum:1}}},
    {$sort:{_id:1}}
  ]).exec();

  postPromise.then(function(posts) {
    result.posts = posts || [];
    return postCountPromise;
  }).then(function(postCount) {
    var emotionCount = {
      "1": 0,
      "2": 0,
      "3": 0,
      "4": 0,
      "5": 0
    };
    var i, len;
    for (i=0, len=postCount.length; i<len; i++) {
      if (postCount[i] && postCount[i]._id) {
        emotionCount[postCount[i]._id] = postCount[i].total;
      }
    }
    result.count = emotionCount;
    res.status(200).send(result);
  }).catch(function(err) {
    logger.error(err);
    return res.status(400).send(err);
  });
};

PostController.prototype.createPost = function(req, res) {
  var postPromise;
  if (!Session.hasSession(req)) {
    return res.status(401).send('permission denied');
  }
  req.body.author = req.session.userId;
  postPromise = PostModel.create(req.body);
  if (!Session.isAllow(req, req.body.author)) {
    return res.status(401).send('permission denied');
  }
  postPromise.then(function(post) {
    res.status(200).send(post);
  }).catch(function(err) {
    logger.error(err);
    return res.status(400).send(err);
  });
};

PostController.prototype.updatePost = function(req, res) {
  var postPromise;
  if (!Session.hasSession(req)) {
    return res.status(401).send('permission denied');
  }
  postPromise = PostModel.update({_id: req.params.id},{$set:{emotionStatus: req.body.emotionStatus}});
  //TODO: 유저 권한 체크 필요!
  postPromise.then(function() {
    res.status(200).send('success');
  }).catch(function(err) {
    logger.error(err);
    return res.status(400).send(err);
  });
};


module.exports = new PostController();
@@ -1634,16 +1634,344 @@
 nCount;%0A
+%0A    var sum =%0A      (emotionCount%5B%221%22%5D * 1) +%0A      (emotionCount%5B%222%22%5D * 2) +%0A      (emotionCount%5B%223%22%5D * 3) +%0A      (emotionCount%5B%224%22%5D * 4) +%0A      (emotionCount%5B%225%22%5D * 5);%0A    var avg = Math.ceil(sum / 5.0);%0A%0A    PostModel.model('User').update(%7B_id: req.session.userId%7D, %7B$set: %7BemotionStatus:avg%7D%7D, function(err, result) %7B%0A  
     res.
@@ -1992,24 +1992,32 @@
 nd(result);%0A
+    %7D);%0A
   %7D).catch(f

Update server