You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

304 lines
8.6 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. //File: controllers/travelController.js
  2. var mongoose = require('mongoose');
  3. var userModel = mongoose.model('userModel');
  4. var notificationModel = mongoose.model('notificationModel');
  5. var travelModel = mongoose.model('travelModel');
  6. var commentModel = mongoose.model('commentModel');
  7. //GET
  8. exports.getAllTravels = function(req, res) {
  9. //get travels with futures dates ($gte - greater than and equal than)
  10. travelModel.find({date: {$gte: new Date()}})
  11. .limit(Number(req.query.pageSize))
  12. .skip(Number(req.query.pageSize) * Number(req.query.page))
  13. .exec(function (err, travels) {
  14. if (err) return res.send(500, err.message);
  15. res.status(200).jsonp(travels);
  16. });
  17. };
  18. exports.getTravelById = function (req, res) {
  19. travelModel.findOne({_id: req.params.travelid})
  20. .lean()
  21. .populate('joins', 'username avatar')
  22. .populate('comments', 'comment user')
  23. .exec(function (err, travel) {
  24. if (err) return res.send(500, err.message);
  25. if (!travel) {
  26. res.json({success: false, message: 'travel not found.'});
  27. } else if (travel) {
  28. res.status(200).jsonp(travel);
  29. }
  30. });
  31. };
  32. exports.addTravel = function(req, res) {
  33. userModel.findOne({'token': req.headers['x-access-token']})
  34. .exec(function(err, user){
  35. if (err) return res.send(500, err.message);
  36. if (!user) {
  37. console.log("user not found");
  38. res.json({success: false, message: 'User not found.'});
  39. } else if (user) {
  40. var travel = new travelModel({
  41. title: req.body.title,
  42. description: req.body.description,
  43. user: user._id,
  44. from: req.body.from,
  45. to: req.body.to,
  46. date: req.body.date,
  47. periodic: req.body.periodic,
  48. generateddate: Date(),
  49. seats: req.body.seats,
  50. package: req.body.package,
  51. collectivized: req.body.collectivized,
  52. type: req.body.modality
  53. });
  54. travel.save(function(err, travel) {
  55. if(err) return res.send(500, err.message);
  56. user.travels.push(travel._id);
  57. user.save(function (err, user) {
  58. if (err) return res.send(500, err.message);
  59. exports.getAllTravels(req, res);
  60. });
  61. });//end of travel.save
  62. }
  63. });//end of usermodel.find
  64. };
  65. exports.updateTravel = function(req, res) {
  66. userModel.findOne({'token': req.headers['x-access-token']})
  67. .exec(function(err, user){
  68. if (err) return res.send(500, err.message);
  69. console.log(travel);
  70. travelModel.findOne({_id: travel._id})
  71. .lean()
  72. .populate('travels', 'title from to date')
  73. .exec(function (err, travel) {
  74. if (err) return res.send(500, err.message);
  75. if (!travel) {
  76. res.json({success: false, message: 'travel not found.'});
  77. } else if (travel) {
  78. res.status(200).jsonp(travel);
  79. }
  80. });
  81. });
  82. };
  83. //DELETE
  84. exports.deleteTravel = function(req, res) {
  85. userModel.findOne({'token': req.headers['x-access-token']})
  86. .exec(function(err, user){
  87. if (err) return res.send(500, err.message);
  88. travelModel.findById(req.params.travelid, function(err, travel) {
  89. if (err) return res.send(500, err.message);
  90. if(travel.user.equals(user._id))
  91. {
  92. travel.remove(function(err) {
  93. if(err) return res.send(500, err.message);
  94. console.log("deleted");
  95. exports.getAllTravels(req, res);
  96. });
  97. }
  98. });
  99. });
  100. };
  101. /* join */
  102. exports.addJoinPetition = function(req, res) {
  103. userModel.findOne({'token': req.headers['x-access-token']})
  104. .exec(function(err, userJoining){
  105. if (err) return res.send(500, err.message);
  106. if (!userJoining) {
  107. res.json({success: false, message: 'User not found.'});
  108. } else if (userJoining) {
  109. travelModel.findOne({
  110. _id: req.params.travelid,
  111. user: {'$ne': userJoining._id},
  112. joins: {'$ne': userJoining._id},
  113. joinPetitions: {'$ne': userJoining._id}
  114. })
  115. .exec(function(err, travel){
  116. if (err) return res.send(500, err.message);
  117. if (!travel) {
  118. res.json({success: false, message: 'travel not found. You can not join a travel if you have created it, or if you have already joined'});
  119. } else if (travel) {
  120. travel.joinPetitions.push(userJoining._id);
  121. travel.save(function(err, travel) {
  122. if(err) return res.send(500, err.message);
  123. //start saving notification, get user owner of travel
  124. userModel.findOne({_id: travel.user})
  125. .exec(function(err, user){
  126. if (err) return res.send(500, err.message);
  127. if (!user) {
  128. res.json({success: false, message: 'User not found.'});
  129. } else if (user) {
  130. //notification
  131. var notification = new notificationModel({
  132. type: "join",
  133. message: "user "+userJoining.username+" joins your travel "+travel.title,
  134. date: new Date(),
  135. icon: 'join.png',
  136. link: ""
  137. });
  138. notification.save(function(err, notification) {
  139. if (err) return res.send(500, err.message);
  140. user.notifications.push(notification._id);
  141. user.save(function(err, user) {
  142. if (err) return res.send(500, err.message);
  143. console.log("notification saved");
  144. exports.getTravelById(req, res);
  145. });
  146. });
  147. }
  148. });//end saving notification
  149. });
  150. }//end of else if travel
  151. });
  152. }//end of else if user
  153. });
  154. };
  155. exports.unJoin = function(req, res) {
  156. userModel.findOne({'token': req.headers['x-access-token']})
  157. .exec(function(err, user){
  158. if (!user) {
  159. res.json({success: false, message: 'User not found.'});
  160. } else if (user) {
  161. travelModel.findOne({
  162. _id: req.params.travelid,
  163. joinPetitions: user._id
  164. })
  165. .exec(function(err, travel){
  166. if (err) return res.send(500, err.message);
  167. if (!travel) {
  168. res.json({success: false, message: 'can not unjoin this travel'});
  169. } else if (travel) {
  170. for(var i=0; i<travel.joinPetitions.length; i++)
  171. {
  172. if(travel.joinPetitions[i].equals(user._id))
  173. {
  174. travel.joinPetitions.splice(i, 1);
  175. }
  176. }
  177. travel.save(function(err, travel) {
  178. if(err) return res.send(500, err.message);
  179. exports.getTravelById(req, res);
  180. });
  181. }
  182. });
  183. }
  184. });
  185. };
  186. exports.getTravelsByUserId = function(req, res) {
  187. travelModel.find({
  188. user: req.params.userid
  189. })
  190. .lean()
  191. .populate('joins', 'username avatar')
  192. .populate('comments', 'comment user')
  193. .exec(function (err, travels) {
  194. if (err) return res.send(500, err.message);
  195. if (!travels) {
  196. res.json({success: false, message: 'travel not found.'});
  197. } else if (travels) {
  198. res.status(200).jsonp(travels);
  199. }
  200. });
  201. };
  202. /* comment */
  203. exports.addComment = function(req, res) {
  204. /*var comment = new commentModel({
  205. travelId: req.params.travelId,
  206. commentUserId: req.body.commentUserId,
  207. commentUsername: req.body.commentUsername,
  208. comment: req.body.comment,
  209. commentAvatar: req.body.commentAvatar
  210. });
  211. comment.save(function(err, comment) {
  212. if(err) return res.send(500, err.message);
  213. res.status(200).jsonp(comment);
  214. });*/
  215. userModel.find({
  216. token: req.headers['x-access-token']
  217. }, function(err, users){
  218. var user=users[0];
  219. travelModel.findById(req.params.travelId, function(err, travel){
  220. console.log(travel.title);
  221. var comment = {
  222. commentUserId: user._id,
  223. commentUsername: user.username,
  224. comment: req.body.comment,
  225. commentAvatar: user.avatar
  226. };
  227. travel.comments.push(comment);
  228. travel.save(function(err, travel) {
  229. if(err) return res.send(500, err.message);
  230. //res.status(200).jsonp(travel);
  231. travelModel.find({date: {$gte: new Date()}}, function(err, travels) {
  232. if(err) res.send(500, err.message);
  233. res.status(200).jsonp(travels);
  234. });
  235. });
  236. //start saving notification, get user owner of travel
  237. userModel.find({
  238. username: travel.owner
  239. }, function(err, userowners) {
  240. var userowner=userowners[0];
  241. //notification
  242. var notification = {
  243. type: "comment",
  244. otherusername: user.username,
  245. description: "user "+user.username+" comments your travel "+travel.title,
  246. date: new Date(),
  247. link: ""
  248. };
  249. userowner.notifications.push(notification);
  250. userowner.save(function(err, userowner) {
  251. console.log("notification saved");
  252. });
  253. });//end saving notification
  254. });
  255. });//end of userModel.find
  256. };
  257. exports.getCommentsByTravelId = function(req, res) {
  258. commentModel.find({
  259. travelId: req.params.travelId
  260. }, function(err, comments) {
  261. if (err) throw err;
  262. if (!comments) {
  263. res.json({ success: false, message: 'no comments for travelId' });
  264. } else if (comments) {
  265. // return the information including token as JSON
  266. res.jsonp(comments);
  267. }
  268. });
  269. };