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.

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