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.

94 lines
2.4 KiB

  1. //File: controllers/tvshows.js
  2. var mongoose = require('mongoose');
  3. var thoughtModel = mongoose.model('thoughtModel');
  4. var userModel = mongoose.model('userModel');
  5. //GET - Return all tvshows in the DB
  6. exports.findAllThoughts = function(req, res) {
  7. thoughtModel.find(function(err, thoughts) {
  8. if(err) res.send(500, err.message);
  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. username: req.body.username,
  42. avatar: req.body.avatar
  43. });
  44. thought.save(function(err, thought) {
  45. if(err) return res.send(500, err.message);
  46. res.status(200).jsonp(thought);
  47. });
  48. };
  49. //PUT - Update a register already exists
  50. exports.updateActivity = function(req, res) {
  51. ActivityModel.findById(req.params.id, function(err, tvshow) {
  52. tvshow.title = req.body.petId;
  53. tvshow.year = req.body.year;
  54. tvshow.country = req.body.country;
  55. tvshow.poster = req.body.poster;
  56. tvshow.seasons = req.body.seasons;
  57. tvshow.genre = req.body.genre;
  58. tvshow.summary = req.body.summary;
  59. tvshow.save(function(err) {
  60. if(err) return res.send(500, err.message);
  61. res.status(200).jsonp(tvshow);
  62. });
  63. });
  64. };
  65. //DELETE - Delete a TVShow with specified ID
  66. exports.deleteActivity = function(req, res) {
  67. ActivityModel.findById(req.params.id, function(err, activity) {
  68. activity.remove(function(err) {
  69. if(err) return res.send(500, err.message);
  70. res.status(200).jsonp(req.params.id);
  71. console.log('DELETE /activities/' + req.params.id);
  72. })
  73. });
  74. };