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	    user_id:   req.body.user_id
58	});
59
60	thought.save(function(err, thought) {
61		if(err) return res.send(500, err.message);
62    res.status(200).jsonp(thought);
63	});
64};
65
66//PUT - Update a register already exists
67exports.updateActivity = function(req, res) {
68	ActivityModel.findById(req.params.id, function(err, tvshow) {
69		tvshow.title   = req.body.petId;
70		tvshow.year    = req.body.year;
71		tvshow.country = req.body.country;
72		tvshow.poster  = req.body.poster;
73		tvshow.seasons = req.body.seasons;
74		tvshow.genre   = req.body.genre;
75		tvshow.summary = req.body.summary;
76
77		tvshow.save(function(err) {
78			if(err) return res.send(500, err.message);
79      res.status(200).jsonp(tvshow);
80		});
81	});
82};
83
84//DELETE - Delete a TVShow with specified ID
85exports.deleteActivity = function(req, res) {
86	ActivityModel.findById(req.params.id, function(err, activity) {
87		activity.remove(function(err) {
88			if(err) return res.send(500, err.message);
89      		res.status(200).jsonp(req.params.id);
90		    console.log('DELETE /activities/' + req.params.id);
91		})
92	});
93};