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.

104 lines
2.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. //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, content: ' + req.body.content);
  36. console.log(req.body);
  37. var travel = new travelModel({
  38. title: req.body.title,
  39. description: req.body.description,
  40. owner: req.body.owner,
  41. from: req.body.from,
  42. to: req.body.to,
  43. date: req.body.date,
  44. periodic: req.body.periodic,
  45. generateddate: req.body.generateddate,
  46. seats: req.body.seats,
  47. package: req.body.package,
  48. icon: req.body.icon,
  49. phone: req.body.phone,
  50. telegram: req.body.telegram,
  51. collectivized: req.body.collectivized,
  52. modality: req.body.modality
  53. });
  54. travel.save(function(err, travel) {
  55. if(err) return res.send(500, err.message);
  56. res.status(200).jsonp(travel);
  57. });
  58. };
  59. //PUT
  60. exports.updateTravel = function(req, res) {
  61. ActivityModel.findById(req.params.id, function(err, tvshow) {
  62. tvshow.title = req.body.petId;
  63. tvshow.year = req.body.year;
  64. tvshow.country = req.body.country;
  65. tvshow.poster = req.body.poster;
  66. tvshow.seasons = req.body.seasons;
  67. tvshow.genre = req.body.genre;
  68. tvshow.summary = req.body.summary;
  69. tvshow.save(function(err) {
  70. if(err) return res.send(500, err.message);
  71. res.status(200).jsonp(tvshow);
  72. });
  73. });
  74. };
  75. //DELETE
  76. exports.deleteTravel = function(req, res) {
  77. ActivityModel.findById(req.params.id, function(err, activity) {
  78. activity.remove(function(err) {
  79. if(err) return res.send(500, err.message);
  80. res.status(200).jsonp(req.params.id);
  81. console.log('DELETE /activities/' + req.params.id);
  82. })
  83. });
  84. };