TwoToc code
[YouAndWeb_TwoToc] / server / api / comment / comment.controller.js
1 /**
2  * Using Rails-like standard naming convention for endpoints.
3  * GET     /api/comments              ->  index
4  * POST    /api/comments              ->  create
5  * GET     /api/comments/:id          ->  show
6  * PUT     /api/comments/:id          ->  update
7  * DELETE  /api/comments/:id          ->  destroy
8  */
9
10 'use strict';
11
12 var _ = require('lodash');
13 var Comment = require('./comment.model');
14 var User = require('../user/user.model');
15
16 function handleError(res, statusCode) {
17   statusCode = statusCode || 500;
18   return function(err) {
19     res.status(statusCode).send(err);
20   };
21 }
22
23 function responseWithResult(res, dig, statusCode) {
24   statusCode = statusCode || 200;
25   var promise = [];
26   return function(entity) {
27     if (entity) {
28       if (dig) {
29         entity.forEach(function(obj, index) {
30           promise.push(new Promise(function(resolve, reject) {
31             User.findById(obj.setterId, function(err, userObj) {
32               obj.user = userObj;
33               resolve(obj);
34             });
35           }));
36         });
37         Promise.all(promise).then(function(data) {
38           console.log(data);
39           res.status(statusCode).json(data);
40         });
41       } else {
42         User.findById(entity.userId, function(err, userObj) {
43           entity.user = userObj;
44           res.status(statusCode).json(entity);
45         });
46       }
47     }
48   };
49 }
50
51 function handleEntityNotFound(res) {
52   return function(entity) {
53     if (!entity) {
54       res.status(404).end();
55       return null;
56     }
57     return entity;
58   };
59 }
60
61 function saveUpdates(updates) {
62   return function(entity) {
63     var updated = _.merge(entity, updates);
64     return updated.saveAsync()
65       .spread(function(updated) {
66         return updated;
67       });
68   };
69 }
70
71 function removeEntity(res) {
72   return function(entity) {
73     if (entity) {
74       return entity.removeAsync()
75         .then(function() {
76           res.status(204).end();
77         });
78     }
79   };
80 }
81
82 // Gets a list of Comments
83 exports.index = function(req, res) {
84   var limit = req.query.limit;
85   var userId = req.query.user;
86   var active = req.query.active;
87   var sq = {};
88   if (userId) {
89     sq.getterId = userId
90   }
91   if (active) {
92     sq.active = active
93   }
94   Comment.find(sq).sort( { dateAdded: -1 } ).lean().limit(limit)
95     .then(responseWithResult(res, true))
96     .catch(handleError(res));
97 };
98
99 // Gets a single Comment from the DB
100 exports.show = function(req, res) {
101   Comment.findByIdAsync(req.params.id)
102     .then(handleEntityNotFound(res))
103     .then(responseWithResult(res))
104     .catch(handleError(res));
105 };
106
107 // Creates a new Comment in the DB
108 exports.create = function(req, res) {
109   Comment.createAsync(req.body)
110     .then(responseWithResult(res, 201))
111     .catch(handleError(res));
112 };
113
114 // Updates an existing Comment in the DB
115 exports.update = function(req, res) {
116   if (req.body._id) {
117     delete req.body._id;
118   }
119   Comment.findByIdAsync(req.params.id)
120     .then(handleEntityNotFound(res))
121     .then(saveUpdates(req.body))
122     .then(responseWithResult(res))
123     .catch(handleError(res));
124 };
125
126 // Deletes a Comment from the DB
127 exports.destroy = function(req, res) {
128   Comment.findByIdAsync(req.params.id)
129     .then(handleEntityNotFound(res))
130     .then(removeEntity(res))
131     .catch(handleError(res));
132 };