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.

233 lines
5.7 KiB

  1. //File: controllers/travelController.js
  2. var mongoose = require('mongoose');
  3. var travelModel = mongoose.model('travelModel');
  4. var userModel = mongoose.model('userModel');
  5. var joinModel = mongoose.model('joinModel');
  6. var commentModel = mongoose.model('commentModel');
  7. //GET
  8. exports.findAllTravels = function(req, res) {
  9. //get travels with futures dates ($gte - greater than and equal than)
  10. travelModel.find({date: {$gte: new Date()}}, function(err, travels) {
  11. if(err) res.send(500, err.message);
  12. res.status(200).jsonp(travels);
  13. });
  14. };
  15. //GET
  16. exports.findById = function(req, res) {
  17. travelModel.findById(req.params.id, function(err, travel) {
  18. if(err) return res.send(500, err.message);
  19. console.log('GET /travel/' + req.params.id);
  20. res.status(200).jsonp(travel);
  21. });
  22. };
  23. exports.findAllTravelsFromUsername = function(req, res) {
  24. travelModel.find({
  25. owner: req.params.username
  26. }, function(err, travels) {
  27. if (err) throw err;
  28. if (!travels) {
  29. res.json({ success: false, message: 'no travels for user' });
  30. } else if (travels) {
  31. console.log(travels);
  32. // return the information including token as JSON
  33. res.jsonp(travels);
  34. }
  35. });
  36. };
  37. exports.addTravel = function(req, res) {
  38. console.log('POST new travel, title: ' + req.body.title);
  39. var travel = new travelModel({
  40. title: req.body.title,
  41. description: req.body.description,
  42. owner: req.body.owner,
  43. from: req.body.from,
  44. to: req.body.to,
  45. date: req.body.date,
  46. periodic: req.body.periodic,
  47. generateddate: req.body.generateddate,
  48. seats: req.body.seats,
  49. package: req.body.package,
  50. icon: req.body.icon,
  51. phone: req.body.phone,
  52. telegram: req.body.telegram,
  53. collectivized: req.body.collectivized,
  54. modality: req.body.modality
  55. });
  56. travel.save(function(err, travel) {
  57. if(err) return res.send(500, err.message);
  58. //res.status(200).jsonp(travel);
  59. travelModel.find(function(err, travels) {
  60. if(err) res.send(500, err.message);
  61. res.status(200).jsonp(travels);
  62. });
  63. });
  64. };
  65. //PUT
  66. exports.updateTravel = function(req, res) {
  67. ActivityModel.findById(req.params.id, function(err, tvshow) {
  68. tvshow.title = req.body.petId;
  69. tvshow.year = req.body.year;
  70. tvshow.country = req.body.country;
  71. tvshow.poster = req.body.poster;
  72. tvshow.seasons = req.body.seasons;
  73. tvshow.genre = req.body.genre;
  74. tvshow.summary = req.body.summary;
  75. tvshow.save(function(err) {
  76. if(err) return res.send(500, err.message);
  77. res.status(200).jsonp(tvshow);
  78. });
  79. });
  80. };
  81. //DELETE
  82. exports.deleteTravel = function(req, res) {
  83. travelModel.findById(req.params.id, function(err, travel) {
  84. travel.remove(function(err) {
  85. if(err) return res.send(500, err.message);
  86. travelModel.find(function(err, travels) {
  87. if(err) res.send(500, err.message);
  88. res.status(200).jsonp(travels);
  89. });
  90. })
  91. });
  92. };
  93. /* join */
  94. exports.addJoin = function(req, res) {
  95. travelModel.findById(req.params.travelId, function(err, travel){
  96. console.log(travel.title);
  97. var join = {
  98. joinedUserId: req.body.joinedUserId,
  99. joinedUsername: req.body.joinedUsername,
  100. acceptedUserId: req.body.acceptedUserId,
  101. joinedAvatar: req.body.joinedAvatar
  102. };
  103. travel.joins.push(join);
  104. travel.save(function(err, travel) {
  105. if(err) return res.send(500, err.message);
  106. //res.status(200).jsonp(travel);
  107. travelModel.find(function(err, travels) {
  108. if(err) res.send(500, err.message);
  109. res.status(200).jsonp(travels);
  110. });
  111. });
  112. });
  113. };
  114. exports.doUnjoin = function(req, res) {
  115. travelModel.findById(req.params.travelId, function(err, travel){
  116. for(var i=0; i<travel.joins.length; i++)
  117. {
  118. if(travel.joins[i].joinedUsername==req.body.joinedUsername)
  119. {
  120. travel.joins.splice(i, 1);
  121. }
  122. }
  123. travel.save(function(err, travel) {
  124. if(err) return res.send(500, err.message);
  125. //res.status(200).jsonp(travel);
  126. travelModel.find(function(err, travels) {
  127. if(err) res.send(500, err.message);
  128. res.status(200).jsonp(travels);
  129. });
  130. });
  131. });
  132. };
  133. exports.getJoinsByTravelId = function(req, res) {
  134. joinModel.find({
  135. travelId: req.params.travelId
  136. }, function(err, joins) {
  137. if (err) throw err;
  138. if (!joins) {
  139. res.json({ success: false, message: 'no joins for travelId' });
  140. } else if (joins) {
  141. // return the information including token as JSON
  142. res.jsonp(joins);
  143. }
  144. });
  145. };
  146. /* comment */
  147. exports.addComment = function(req, res) {
  148. /*var comment = new commentModel({
  149. travelId: req.params.travelId,
  150. commentUserId: req.body.commentUserId,
  151. commentUsername: req.body.commentUsername,
  152. comment: req.body.comment,
  153. commentAvatar: req.body.commentAvatar
  154. });
  155. comment.save(function(err, comment) {
  156. if(err) return res.send(500, err.message);
  157. res.status(200).jsonp(comment);
  158. });*/
  159. travelModel.findById(req.params.travelId, function(err, travel){
  160. console.log(travel.title);
  161. var comment = {
  162. commentUserId: req.body.commentUserId,
  163. commentUsername: req.body.commentUsername,
  164. comment: req.body.comment,
  165. commentAvatar: req.body.commentAvatar
  166. };
  167. travel.comments.push(comment);
  168. travel.save(function(err, travel) {
  169. if(err) return res.send(500, err.message);
  170. //res.status(200).jsonp(travel);
  171. travelModel.find(function(err, travels) {
  172. if(err) res.send(500, err.message);
  173. res.status(200).jsonp(travels);
  174. });
  175. });
  176. });
  177. };
  178. exports.getCommentsByTravelId = function(req, res) {
  179. commentModel.find({
  180. travelId: req.params.travelId
  181. }, function(err, comments) {
  182. if (err) throw err;
  183. if (!comments) {
  184. res.json({ success: false, message: 'no comments for travelId' });
  185. } else if (comments) {
  186. // return the information including token as JSON
  187. res.jsonp(comments);
  188. }
  189. });
  190. };