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		console.log(thoughts.length);
 14		thoughts=JSON.parse(JSON.stringify(thoughts));
 15
 16		thoughts.forEach(function(thought, index, array){
 17
 18array=JSON.parse(JSON.stringify(array));
 19
 20			userModel.find({
 21		      username: thought.authorname
 22			  }, function(err, user) {
 23
 24			      if (err) throw err;
 25
 26			      if (!user) {
 27
 28			    } else if (user) {
 29
 30						/*console.log(thought);
 31						console.log(user);*/
 32			        array[index].usericon=user[0].icon;
 33
 34				console.log("icon: " + array[index].usericon);
 35					//thought.set('icon', user.icon);
 36					//console.log(thought);
 37
 38
 39			      }
 40
 41				  if(index === array.length-1)
 42				  {
 43					  console.log('GET /thoughts');
 44					  console.log(array);
 45					  res.status(200).jsonp(array);
 46				  }
 47			    });
 48
 49
 50		});
 51
 52	});
 53};
 54
 55//GET - Return a TVShow with specified ID
 56exports.findById = function(req, res) {
 57	thoughtModel.findById(req.params.id, function(err, thought) {
 58    if(err) return res.send(500, err.message);
 59
 60    console.log('GET /thought/' + req.params.id);
 61		res.status(200).jsonp(thought);
 62	});
 63};
 64
 65exports.findAllThoughtsFromUsername = function(req, res) {
 66    thoughtModel.find({
 67      authorname: req.params.userid
 68  }, function(err, thoughts) {
 69
 70      if (err) throw err;
 71
 72      if (!thoughts) {
 73        res.json({ success: false, message: 'no thoughts for user' });
 74    } else if (thoughts) {
 75        console.log(thoughts);
 76          // return the information including token as JSON
 77          res.jsonp(thoughts);
 78
 79
 80      }
 81
 82    });
 83};
 84
 85//POST - Insert a new TVShow in the DB
 86exports.addThought = function(req, res) {
 87	console.log('POST new thought, content: ' + req.body.content);
 88	console.log(req.body);
 89
 90	var thought = new thoughtModel({
 91		time: req.body.time,
 92	    content:   req.body.content,
 93	    authorname:   req.body.authorname
 94	});
 95
 96	thought.save(function(err, thought) {
 97		if(err) return res.send(500, err.message);
 98    res.status(200).jsonp(thought);
 99	});
100};
101
102//PUT - Update a register already exists
103exports.updateActivity = function(req, res) {
104	ActivityModel.findById(req.params.id, function(err, tvshow) {
105		tvshow.title   = req.body.petId;
106		tvshow.year    = req.body.year;
107		tvshow.country = req.body.country;
108		tvshow.poster  = req.body.poster;
109		tvshow.seasons = req.body.seasons;
110		tvshow.genre   = req.body.genre;
111		tvshow.summary = req.body.summary;
112
113		tvshow.save(function(err) {
114			if(err) return res.send(500, err.message);
115      res.status(200).jsonp(tvshow);
116		});
117	});
118};
119
120//DELETE - Delete a TVShow with specified ID
121exports.deleteActivity = function(req, res) {
122	ActivityModel.findById(req.params.id, function(err, activity) {
123		activity.remove(function(err) {
124			if(err) return res.send(500, err.message);
125      		res.status(200).jsonp(req.params.id);
126		    console.log('DELETE /activities/' + req.params.id);
127		})
128	});
129};