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.

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