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