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.

89 lines
2.4 KiB

  1. //File: controllers/tvshows.js
  2. var mongoose = require('mongoose');
  3. var thoughtModel = mongoose.model('thoughtModel');
  4. //GET - Return all tvshows in the DB
  5. exports.findAllThoughts = function(req, res) {
  6. thoughtModel.find(function(err, thoughts) {
  7. if(err) res.send(500, err.message);
  8. console.log('GET /thoughts');
  9. res.status(200).jsonp(thoughts);
  10. });
  11. };
  12. //GET - Return a TVShow with specified ID
  13. exports.findById = function(req, res) {
  14. thoughtModel.findById(req.params.id, function(err, thought) {
  15. if(err) return res.send(500, err.message);
  16. console.log('GET /thought/' + req.params.id);
  17. res.status(200).jsonp(thought);
  18. });
  19. };
  20. exports.findAllThoughtsFromUsername = function(req, res) {
  21. thoughtModel.find({
  22. authorname: req.params.userid
  23. }, function(err, thoughts) {
  24. if (err) throw err;
  25. if (!thoughts) {
  26. res.json({ success: false, message: 'no thoughts for user' });
  27. } else if (thoughts) {
  28. console.log(thoughts);
  29. // return the information including token as JSON
  30. res.jsonp(thoughts);
  31. }
  32. });
  33. };
  34. //POST - Insert a new TVShow in the DB
  35. exports.addThought = function(req, res) {
  36. console.log('POST new thought, content: ' + req.body.content);
  37. console.log(req.body);
  38. var thought = new thoughtModel({
  39. time: req.body.time,
  40. content: req.body.content,
  41. authorname: req.body.authorname
  42. });
  43. thought.save(function(err, thought) {
  44. if(err) return res.send(500, err.message);
  45. res.status(200).jsonp(thought);
  46. });
  47. };
  48. //PUT - Update a register already exists
  49. exports.updateActivity = function(req, res) {
  50. ActivityModel.findById(req.params.id, function(err, tvshow) {
  51. tvshow.title = req.body.petId;
  52. tvshow.year = req.body.year;
  53. tvshow.country = req.body.country;
  54. tvshow.poster = req.body.poster;
  55. tvshow.seasons = req.body.seasons;
  56. tvshow.genre = req.body.genre;
  57. tvshow.summary = req.body.summary;
  58. tvshow.save(function(err) {
  59. if(err) return res.send(500, err.message);
  60. res.status(200).jsonp(tvshow);
  61. });
  62. });
  63. };
  64. //DELETE - Delete a TVShow with specified ID
  65. exports.deleteActivity = function(req, res) {
  66. ActivityModel.findById(req.params.id, function(err, activity) {
  67. activity.remove(function(err) {
  68. if(err) return res.send(500, err.message);
  69. res.status(200).jsonp(req.params.id);
  70. console.log('DELETE /activities/' + req.params.id);
  71. })
  72. });
  73. };