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.

99 lines
2.4 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. authorname: req.params.userid
  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. generateddate: req.body.generateddate,
  45. seats: req.body.seats,
  46. icon: req.body.icon,
  47. phone: req.body.phone
  48. });
  49. travel.save(function(err, travel) {
  50. if(err) return res.send(500, err.message);
  51. res.status(200).jsonp(travel);
  52. });
  53. };
  54. //PUT
  55. exports.updateTravel = function(req, res) {
  56. ActivityModel.findById(req.params.id, function(err, tvshow) {
  57. tvshow.title = req.body.petId;
  58. tvshow.year = req.body.year;
  59. tvshow.country = req.body.country;
  60. tvshow.poster = req.body.poster;
  61. tvshow.seasons = req.body.seasons;
  62. tvshow.genre = req.body.genre;
  63. tvshow.summary = req.body.summary;
  64. tvshow.save(function(err) {
  65. if(err) return res.send(500, err.message);
  66. res.status(200).jsonp(tvshow);
  67. });
  68. });
  69. };
  70. //DELETE
  71. exports.deleteTravel = function(req, res) {
  72. ActivityModel.findById(req.params.id, function(err, activity) {
  73. activity.remove(function(err) {
  74. if(err) return res.send(500, err.message);
  75. res.status(200).jsonp(req.params.id);
  76. console.log('DELETE /activities/' + req.params.id);
  77. })
  78. });
  79. };