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.

314 lines
8.2 KiB

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