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