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.

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