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.

230 lines
5.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. travelModel.find(function(err, travels) {
  59. if(err) res.send(500, err.message);
  60. res.status(200).jsonp(travels);
  61. });
  62. });
  63. };
  64. //PUT
  65. exports.updateTravel = function(req, res) {
  66. ActivityModel.findById(req.params.id, function(err, tvshow) {
  67. tvshow.title = req.body.petId;
  68. tvshow.year = req.body.year;
  69. tvshow.country = req.body.country;
  70. tvshow.poster = req.body.poster;
  71. tvshow.seasons = req.body.seasons;
  72. tvshow.genre = req.body.genre;
  73. tvshow.summary = req.body.summary;
  74. tvshow.save(function(err) {
  75. if(err) return res.send(500, err.message);
  76. res.status(200).jsonp(tvshow);
  77. });
  78. });
  79. };
  80. //DELETE
  81. exports.deleteTravel = function(req, res) {
  82. travelModel.findById(req.params.id, function(err, travel) {
  83. travel.remove(function(err) {
  84. if(err) return res.send(500, err.message);
  85. res.status(200).jsonp(req.params.id);
  86. console.log('DELETE /traves/' + req.params.id);
  87. })
  88. });
  89. };
  90. /* join */
  91. exports.addJoin = function(req, res) {
  92. travelModel.findById(req.params.travelId, function(err, travel){
  93. console.log(travel.title);
  94. var join = {
  95. joinedUserId: req.body.joinedUserId,
  96. joinedUsername: req.body.joinedUsername,
  97. acceptedUserId: req.body.acceptedUserId,
  98. joinedAvatar: req.body.joinedAvatar
  99. };
  100. travel.joins.push(join);
  101. travel.save(function(err, travel) {
  102. if(err) return res.send(500, err.message);
  103. //res.status(200).jsonp(travel);
  104. travelModel.find(function(err, travels) {
  105. if(err) res.send(500, err.message);
  106. res.status(200).jsonp(travels);
  107. });
  108. });
  109. });
  110. };
  111. exports.doUnjoin = function(req, res) {
  112. travelModel.findById(req.params.travelId, function(err, travel){
  113. for(var i=0; i<travel.joins.length; i++)
  114. {
  115. if(travel.joins[i].joinedUsername==req.body.joinedUsername)
  116. {
  117. travel.joins.splice(i, 1);
  118. }
  119. }
  120. travel.save(function(err, travel) {
  121. if(err) return res.send(500, err.message);
  122. //res.status(200).jsonp(travel);
  123. travelModel.find(function(err, travels) {
  124. if(err) res.send(500, err.message);
  125. res.status(200).jsonp(travels);
  126. });
  127. });
  128. });
  129. };
  130. exports.getJoinsByTravelId = function(req, res) {
  131. joinModel.find({
  132. travelId: req.params.travelId
  133. }, function(err, joins) {
  134. if (err) throw err;
  135. if (!joins) {
  136. res.json({ success: false, message: 'no joins for travelId' });
  137. } else if (joins) {
  138. // return the information including token as JSON
  139. res.jsonp(joins);
  140. }
  141. });
  142. };
  143. /* comment */
  144. exports.addComment = function(req, res) {
  145. /*var comment = new commentModel({
  146. travelId: req.params.travelId,
  147. commentUserId: req.body.commentUserId,
  148. commentUsername: req.body.commentUsername,
  149. comment: req.body.comment,
  150. commentAvatar: req.body.commentAvatar
  151. });
  152. comment.save(function(err, comment) {
  153. if(err) return res.send(500, err.message);
  154. res.status(200).jsonp(comment);
  155. });*/
  156. travelModel.findById(req.params.travelId, function(err, travel){
  157. console.log(travel.title);
  158. var comment = {
  159. commentUserId: req.body.commentUserId,
  160. commentUsername: req.body.commentUsername,
  161. comment: req.body.comment,
  162. commentAvatar: req.body.commentAvatar
  163. };
  164. travel.comments.push(comment);
  165. travel.save(function(err, travel) {
  166. if(err) return res.send(500, err.message);
  167. //res.status(200).jsonp(travel);
  168. travelModel.find(function(err, travels) {
  169. if(err) res.send(500, err.message);
  170. res.status(200).jsonp(travels);
  171. });
  172. });
  173. });
  174. };
  175. exports.getCommentsByTravelId = function(req, res) {
  176. commentModel.find({
  177. travelId: req.params.travelId
  178. }, function(err, comments) {
  179. if (err) throw err;
  180. if (!comments) {
  181. res.json({ success: false, message: 'no comments for travelId' });
  182. } else if (comments) {
  183. // return the information including token as JSON
  184. res.jsonp(comments);
  185. }
  186. });
  187. };