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.

329 lines
8.2 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. //start saving notification, get user owner of travel
  150. userModel.find({
  151. username: travel.owner
  152. }, function(err, userowners) {
  153. var userowner=userowners[0];
  154. //notification
  155. var notification = {
  156. type: "join",
  157. otherusername: user.username,
  158. description: "user "+user.username+" joins your travel "+travel.title,
  159. date: new Date(),
  160. link: ""
  161. };
  162. userowner.notifications.push(notification);
  163. userowner.save(function(err, userowner) {
  164. console.log("notification saved");
  165. });
  166. });//end saving notification
  167. });
  168. });
  169. };
  170. exports.doUnjoin = function(req, res) {
  171. userModel.find({
  172. token: req.headers['x-access-token']
  173. }, function(err, users){
  174. var user=users[0];
  175. travelModel.findById(req.params.travelId, function(err, travel){
  176. for(var i=0; i<travel.joins.length; i++)
  177. {
  178. if(travel.joins[i].joinedUsername==user.username)
  179. {
  180. travel.joins.splice(i, 1);
  181. }
  182. }
  183. travel.save(function(err, travel) {
  184. if(err) return res.send(500, err.message);
  185. //res.status(200).jsonp(travel);
  186. travelModel.find({date: {$gte: new Date()}}, function(err, travels) {
  187. if(err) res.send(500, err.message);
  188. res.status(200).jsonp(travels);
  189. });
  190. });
  191. });
  192. });
  193. };
  194. exports.getJoinsByTravelId = function(req, res) {
  195. joinModel.find({
  196. travelId: req.params.travelId
  197. }, function(err, joins) {
  198. if (err) throw err;
  199. if (!joins) {
  200. res.json({ success: false, message: 'no joins for travelId' });
  201. } else if (joins) {
  202. // return the information including token as JSON
  203. res.jsonp(joins);
  204. }
  205. });
  206. };
  207. /* comment */
  208. exports.addComment = function(req, res) {
  209. /*var comment = new commentModel({
  210. travelId: req.params.travelId,
  211. commentUserId: req.body.commentUserId,
  212. commentUsername: req.body.commentUsername,
  213. comment: req.body.comment,
  214. commentAvatar: req.body.commentAvatar
  215. });
  216. comment.save(function(err, comment) {
  217. if(err) return res.send(500, err.message);
  218. res.status(200).jsonp(comment);
  219. });*/
  220. userModel.find({
  221. token: req.headers['x-access-token']
  222. }, function(err, users){
  223. var user=users[0];
  224. travelModel.findById(req.params.travelId, function(err, travel){
  225. console.log(travel.title);
  226. var comment = {
  227. commentUserId: user._id,
  228. commentUsername: user.username,
  229. comment: req.body.comment,
  230. commentAvatar: user.avatar
  231. };
  232. travel.comments.push(comment);
  233. travel.save(function(err, travel) {
  234. if(err) return res.send(500, err.message);
  235. //res.status(200).jsonp(travel);
  236. travelModel.find({date: {$gte: new Date()}}, function(err, travels) {
  237. if(err) res.send(500, err.message);
  238. res.status(200).jsonp(travels);
  239. });
  240. });
  241. //start saving notification, get user owner of travel
  242. userModel.find({
  243. username: travel.owner
  244. }, function(err, userowners) {
  245. var userowner=userowners[0];
  246. //notification
  247. var notification = {
  248. type: "comment",
  249. otherusername: user.username,
  250. description: "user "+user.username+" comments your travel "+travel.title,
  251. date: new Date(),
  252. link: ""
  253. };
  254. userowner.notifications.push(notification);
  255. userowner.save(function(err, userowner) {
  256. console.log("notification saved");
  257. });
  258. });//end saving notification
  259. });
  260. });//end of userModel.find
  261. };
  262. exports.getCommentsByTravelId = function(req, res) {
  263. commentModel.find({
  264. travelId: req.params.travelId
  265. }, function(err, comments) {
  266. if (err) throw err;
  267. if (!comments) {
  268. res.json({ success: false, message: 'no comments for travelId' });
  269. } else if (comments) {
  270. // return the information including token as JSON
  271. res.jsonp(comments);
  272. }
  273. });
  274. };