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.

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