//File: controllers/tvshows.js var mongoose = require('mongoose'); var thoughtModel = mongoose.model('thoughtModel'); var userModel = mongoose.model('userModel'); //GET - Return all tvshows in the DB exports.findAllThoughts = function(req, res) { thoughtModel.find(function(err, thoughts) { if(err) res.send(500, err.message); console.log(thoughts.length); thoughts=JSON.parse(JSON.stringify(thoughts)); thoughts.forEach(function(thought, index, array){ array=JSON.parse(JSON.stringify(array)); userModel.find({ username: thought.authorname }, function(err, user) { if (err) throw err; if (!user) { } else if (user) { /*console.log(thought); console.log(user);*/ array[index].usericon=user[0].icon; console.log("icon: " + array[index].usericon); //thought.set('icon', user.icon); //console.log(thought); } if(index === array.length-1) { console.log('GET /thoughts'); console.log(array); res.status(200).jsonp(array); } }); }); }); }; //GET - Return a TVShow with specified ID exports.findById = function(req, res) { thoughtModel.findById(req.params.id, function(err, thought) { if(err) return res.send(500, err.message); console.log('GET /thought/' + req.params.id); res.status(200).jsonp(thought); }); }; exports.findAllThoughtsFromUsername = function(req, res) { thoughtModel.find({ authorname: req.params.userid }, function(err, thoughts) { if (err) throw err; if (!thoughts) { res.json({ success: false, message: 'no thoughts for user' }); } else if (thoughts) { console.log(thoughts); // return the information including token as JSON res.jsonp(thoughts); } }); }; //POST - Insert a new TVShow in the DB exports.addThought = function(req, res) { console.log('POST new thought, content: ' + req.body.content); console.log(req.body); var thought = new thoughtModel({ time: req.body.time, content: req.body.content, authorname: req.body.authorname }); thought.save(function(err, thought) { if(err) return res.send(500, err.message); res.status(200).jsonp(thought); }); }; //PUT - Update a register already exists exports.updateActivity = function(req, res) { ActivityModel.findById(req.params.id, function(err, tvshow) { tvshow.title = req.body.petId; tvshow.year = req.body.year; tvshow.country = req.body.country; tvshow.poster = req.body.poster; tvshow.seasons = req.body.seasons; tvshow.genre = req.body.genre; tvshow.summary = req.body.summary; tvshow.save(function(err) { if(err) return res.send(500, err.message); res.status(200).jsonp(tvshow); }); }); }; //DELETE - Delete a TVShow with specified ID exports.deleteActivity = function(req, res) { ActivityModel.findById(req.params.id, function(err, activity) { activity.remove(function(err) { if(err) return res.send(500, err.message); res.status(200).jsonp(req.params.id); console.log('DELETE /activities/' + req.params.id); }) }); };