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.

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