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.

175 lines
4.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. 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. });
  59. };
  60. //PUT
  61. exports.updateTravel = function(req, res) {
  62. ActivityModel.findById(req.params.id, function(err, tvshow) {
  63. tvshow.title = req.body.petId;
  64. tvshow.year = req.body.year;
  65. tvshow.country = req.body.country;
  66. tvshow.poster = req.body.poster;
  67. tvshow.seasons = req.body.seasons;
  68. tvshow.genre = req.body.genre;
  69. tvshow.summary = req.body.summary;
  70. tvshow.save(function(err) {
  71. if(err) return res.send(500, err.message);
  72. res.status(200).jsonp(tvshow);
  73. });
  74. });
  75. };
  76. //DELETE
  77. exports.deleteTravel = function(req, res) {
  78. travelModel.findById(req.params.id, function(err, travel) {
  79. travel.remove(function(err) {
  80. if(err) return res.send(500, err.message);
  81. res.status(200).jsonp(req.params.id);
  82. console.log('DELETE /traves/' + req.params.id);
  83. })
  84. });
  85. };
  86. /* join */
  87. exports.addJoin = function(req, res) {
  88. var join = new joinModel({
  89. travelId: req.params.travelId,
  90. joinedUserId: req.body.joinedUserId,
  91. joinedUsername: req.body.joinedUsername,
  92. acceptedUserId: req.body.acceptedUserId,
  93. joinedAvatar: req.body.joinedAvatar
  94. });
  95. join.save(function(err, join) {
  96. if(err) return res.send(500, err.message);
  97. res.status(200).jsonp(join);
  98. });
  99. };
  100. exports.getJoinsByTravelId = function(req, res) {
  101. joinModel.find({
  102. travelId: req.params.travelId
  103. }, function(err, joins) {
  104. if (err) throw err;
  105. if (!joins) {
  106. res.json({ success: false, message: 'no joins for travelId' });
  107. } else if (joins) {
  108. // return the information including token as JSON
  109. res.jsonp(joins);
  110. }
  111. });
  112. };
  113. /* comment */
  114. exports.addComment = function(req, res) {
  115. var comment = new commentModel({
  116. travelId: req.params.travelId,
  117. commentUserId: req.body.commentUserId,
  118. commentUsername: req.body.commentUsername,
  119. comment: req.body.comment,
  120. commentAvatar: req.body.commentAvatar
  121. });
  122. comment.save(function(err, comment) {
  123. if(err) return res.send(500, err.message);
  124. res.status(200).jsonp(comment);
  125. });
  126. };
  127. exports.getCommentsByTravelId = function(req, res) {
  128. commentModel.find({
  129. travelId: req.params.travelId
  130. }, function(err, comments) {
  131. if (err) throw err;
  132. if (!comments) {
  133. res.json({ success: false, message: 'no comments for travelId' });
  134. } else if (comments) {
  135. // return the information including token as JSON
  136. res.jsonp(comments);
  137. }
  138. });
  139. };