//File: controllers/projectController.js var mongoose = require('mongoose'); var projectModel = mongoose.model('projectModel'); var userModel = mongoose.model('userModel'); //GET exports.findAllProjects = function(req, res) { projectModel.find(function(err, projects) { if(err) res.send(500, err.message); res.status(200).jsonp(projects); }); }; //GET exports.findById = function(req, res) { projectModel.findById(req.params.id, function(err, project) { if(err) return res.send(500, err.message); console.log('GET /project/' + req.params.id); res.status(200).jsonp(project); }); }; exports.findAllProjectsFromUsername = function(req, res) { projectModel.find({ owner: req.params.username }, function(err, projects) { if (err) throw err; if (!projects) { res.json({ success: false, message: 'no projects for user' }); } else if (projects) { console.log(projects); // return the information including token as JSON res.jsonp(projects); } }); }; exports.addProject = function(req, res) { console.log('POST new project, title: ' + req.body.title); var project = new projectModel({ title: req.body.title, description: req.body.description, icon: req.body.icon, users: req.body.users, chart: req.body.chart, dateCreation: req.body.dateCreation, github: req.body.github, refnum: req.body.refnum }); project.save(function(err, project) { if(err) return res.send(500, err.message); /*res.status(200).jsonp(project);*/ projectModel.find(function(err, projects) { if(err) res.send(500, err.message); res.status(200).jsonp(projects); }); }); }; exports.addUserToProject = function(req, res) { console.log("addUserToProject"); console.log(req.body); projectModel.findById(req.params.id, function(err, project) { console.log(project); project.users.push(req.body.username); console.log(project.users); project.save(function(err) { if(err) return res.send(500, err.message); projectModel.find(function(err, projects) { if(err) res.send(500, err.message); res.status(200).jsonp(projects); }); }); }); }; exports.userStartWorking = function(req, res) { console.log("userStartWorking"); projectModel.findById(req.params.id, function(err, project) { var workstrike={ username: req.body.username, start: new Date(), end: "", }; project.workStrikes.push(workstrike); console.log(project); project.save(function(err) { if(err) return res.send(500, err.message); projectModel.find(function(err, projects) { if(err) res.send(500, err.message); res.status(200).jsonp(projects); }); }); }); }; exports.userStopWorking = function(req, res) { console.log("userStopWorking"); projectModel.findById(req.params.id, function(err, project) { for(var i=0; i