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.

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