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.

194 lines
4.6 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. travelModel.find(function(err, travels) {
  10. if(err) res.send(500, err.message);
  11. res.status(200).jsonp(travels);
  12. });
  13. };
  14. //GET
  15. exports.findById = function(req, res) {
  16. travelModel.findById(req.params.id, function(err, travel) {
  17. if(err) return res.send(500, err.message);
  18. console.log('GET /travel/' + req.params.id);
  19. res.status(200).jsonp(travel);
  20. });
  21. };
  22. exports.findAllTravelsFromUsername = function(req, res) {
  23. travelModel.find({
  24. owner: req.params.username
  25. }, function(err, travels) {
  26. if (err) throw err;
  27. if (!travels) {
  28. res.json({ success: false, message: 'no travels for user' });
  29. } else if (travels) {
  30. console.log(travels);
  31. // return the information including token as JSON
  32. res.jsonp(travels);
  33. }
  34. });
  35. };
  36. exports.addTravel = function(req, res) {
  37. console.log('POST new travel, title: ' + req.body.title);
  38. var travel = new travelModel({
  39. title: req.body.title,
  40. description: req.body.description,
  41. owner: req.body.owner,
  42. from: req.body.from,
  43. to: req.body.to,
  44. date: req.body.date,
  45. periodic: req.body.periodic,
  46. generateddate: req.body.generateddate,
  47. seats: req.body.seats,
  48. package: req.body.package,
  49. icon: req.body.icon,
  50. phone: req.body.phone,
  51. telegram: req.body.telegram,
  52. collectivized: req.body.collectivized,
  53. modality: req.body.modality
  54. });
  55. travel.save(function(err, travel) {
  56. if(err) return res.send(500, err.message);
  57. res.status(200).jsonp(travel);
  58. });
  59. };
  60. //PUT
  61. exports.updateTravel = function(req, res) {
  62. ActivityModel.findById(req.params.id, function(err, tvshow) {
  63. tvshow.title = req.body.petId;
  64. tvshow.year = req.body.year;
  65. tvshow.country = req.body.country;
  66. tvshow.poster = req.body.poster;
  67. tvshow.seasons = req.body.seasons;
  68. tvshow.genre = req.body.genre;
  69. tvshow.summary = req.body.summary;
  70. tvshow.save(function(err) {
  71. if(err) return res.send(500, err.message);
  72. res.status(200).jsonp(tvshow);
  73. });
  74. });
  75. };
  76. //DELETE
  77. exports.deleteTravel = function(req, res) {
  78. travelModel.findById(req.params.id, function(err, travel) {
  79. travel.remove(function(err) {
  80. if(err) return res.send(500, err.message);
  81. res.status(200).jsonp(req.params.id);
  82. console.log('DELETE /traves/' + req.params.id);
  83. })
  84. });
  85. };
  86. /* join */
  87. exports.addJoin = function(req, res) {
  88. var join = new joinModel({
  89. travelId: req.params.travelId,
  90. joinedUserId: req.body.joinedUserId,
  91. joinedUsername: req.body.joinedUsername,
  92. acceptedUserId: req.body.acceptedUserId,
  93. joinedAvatar: req.body.joinedAvatar
  94. });
  95. join.save(function(err, join) {
  96. if(err) return res.send(500, err.message);
  97. res.status(200).jsonp(join);
  98. });
  99. };
  100. exports.doUnjoin = function(req, res) {
  101. joinModel.find({
  102. travelId: req.params.travelId
  103. }, function(err, joins) {
  104. for(var i=0; i<joins.length; i++)
  105. {
  106. if(joins[i].joinedUsername==req.body.joinedUsername)
  107. {
  108. joins[i].remove(function(err) {
  109. if(err) return res.send(500, err.message);
  110. res.status(200).jsonp('removed');
  111. console.log('DELETE /unjoin/' + req.params.id);
  112. })
  113. }
  114. }
  115. });
  116. };
  117. exports.getJoinsByTravelId = function(req, res) {
  118. joinModel.find({
  119. travelId: req.params.travelId
  120. }, function(err, joins) {
  121. if (err) throw err;
  122. if (!joins) {
  123. res.json({ success: false, message: 'no joins for travelId' });
  124. } else if (joins) {
  125. // return the information including token as JSON
  126. res.jsonp(joins);
  127. }
  128. });
  129. };
  130. /* comment */
  131. exports.addComment = function(req, res) {
  132. var comment = new commentModel({
  133. travelId: req.params.travelId,
  134. commentUserId: req.body.commentUserId,
  135. commentUsername: req.body.commentUsername,
  136. comment: req.body.comment,
  137. commentAvatar: req.body.commentAvatar
  138. });
  139. comment.save(function(err, comment) {
  140. if(err) return res.send(500, err.message);
  141. res.status(200).jsonp(comment);
  142. });
  143. };
  144. exports.getCommentsByTravelId = function(req, res) {
  145. commentModel.find({
  146. travelId: req.params.travelId
  147. }, function(err, comments) {
  148. if (err) throw err;
  149. if (!comments) {
  150. res.json({ success: false, message: 'no comments for travelId' });
  151. } else if (comments) {
  152. // return the information including token as JSON
  153. res.jsonp(comments);
  154. }
  155. });
  156. };