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.

102 lines
2.5 KiB

8 years ago
  1. //File: controllers/travelController.js
  2. var mongoose = require('mongoose');
  3. var travelModel = mongoose.model('travelModel');
  4. var userModel = mongoose.model('userModel');
  5. //GET
  6. exports.findAllTravels = function(req, res) {
  7. travelModel.find(function(err, travels) {
  8. if(err) res.send(500, err.message);
  9. res.status(200).jsonp(travels);
  10. });
  11. };
  12. //GET
  13. exports.findById = function(req, res) {
  14. travelModel.findById(req.params.id, function(err, travel) {
  15. if(err) return res.send(500, err.message);
  16. console.log('GET /travel/' + req.params.id);
  17. res.status(200).jsonp(travel);
  18. });
  19. };
  20. exports.findAllTravelsFromUsername = function(req, res) {
  21. travelModel.find({
  22. owner: req.params.username
  23. }, function(err, travels) {
  24. if (err) throw err;
  25. if (!travels) {
  26. res.json({ success: false, message: 'no travels for user' });
  27. } else if (travels) {
  28. console.log(travels);
  29. // return the information including token as JSON
  30. res.jsonp(travels);
  31. }
  32. });
  33. };
  34. exports.addTravel = function(req, res) {
  35. console.log('POST new travel, title: ' + req.body.title);
  36. var travel = new travelModel({
  37. title: req.body.title,
  38. description: req.body.description,
  39. owner: req.body.owner,
  40. from: req.body.from,
  41. to: req.body.to,
  42. date: req.body.date,
  43. periodic: req.body.periodic,
  44. generateddate: req.body.generateddate,
  45. seats: req.body.seats,
  46. package: req.body.package,
  47. icon: req.body.icon,
  48. phone: req.body.phone,
  49. telegram: req.body.telegram,
  50. collectivized: req.body.collectivized,
  51. modality: req.body.modality
  52. });
  53. travel.save(function(err, travel) {
  54. if(err) return res.send(500, err.message);
  55. res.status(200).jsonp(travel);
  56. });
  57. };
  58. //PUT
  59. exports.updateTravel = function(req, res) {
  60. ActivityModel.findById(req.params.id, function(err, tvshow) {
  61. tvshow.title = req.body.petId;
  62. tvshow.year = req.body.year;
  63. tvshow.country = req.body.country;
  64. tvshow.poster = req.body.poster;
  65. tvshow.seasons = req.body.seasons;
  66. tvshow.genre = req.body.genre;
  67. tvshow.summary = req.body.summary;
  68. tvshow.save(function(err) {
  69. if(err) return res.send(500, err.message);
  70. res.status(200).jsonp(tvshow);
  71. });
  72. });
  73. };
  74. //DELETE
  75. exports.deleteTravel = function(req, res) {
  76. ActivityModel.findById(req.params.id, function(err, activity) {
  77. activity.remove(function(err) {
  78. if(err) return res.send(500, err.message);
  79. res.status(200).jsonp(req.params.id);
  80. console.log('DELETE /activities/' + req.params.id);
  81. })
  82. });
  83. };