You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
4.2 KiB

  1. //File: controllers/projectController.js
  2. var mongoose = require('mongoose');
  3. var projectModel = mongoose.model('projectModel');
  4. var userModel = mongoose.model('userModel');
  5. //GET
  6. exports.findAllProjects = function(req, res) {
  7. projectModel.find(function(err, projects) {
  8. if(err) res.send(500, err.message);
  9. res.status(200).jsonp(projects);
  10. });
  11. };
  12. //GET
  13. exports.findById = function(req, res) {
  14. projectModel.findById(req.params.id, function(err, project) {
  15. if(err) return res.send(500, err.message);
  16. console.log('GET /project/' + req.params.id);
  17. res.status(200).jsonp(project);
  18. });
  19. };
  20. exports.findAllProjectsFromUsername = function(req, res) {
  21. projectModel.find({
  22. owner: req.params.username
  23. }, function(err, projects) {
  24. if (err) throw err;
  25. if (!projects) {
  26. res.json({ success: false, message: 'no projects for user' });
  27. } else if (projects) {
  28. console.log(projects);
  29. // return the information including token as JSON
  30. res.jsonp(projects);
  31. }
  32. });
  33. };
  34. exports.addProject = function(req, res) {
  35. console.log('POST new project, title: ' + req.body.title);
  36. var project = new projectModel({
  37. title: req.body.title,
  38. description: req.body.description,
  39. icon: req.body.icon,
  40. users: req.body.users,
  41. chart: req.body.chart,
  42. dateCreation: req.body.dateCreation,
  43. github: req.body.github,
  44. refnum: req.body.refnum
  45. });
  46. project.save(function(err, project) {
  47. if(err) return res.send(500, err.message);
  48. /*res.status(200).jsonp(project);*/
  49. projectModel.find(function(err, projects) {
  50. if(err) res.send(500, err.message);
  51. res.status(200).jsonp(projects);
  52. });
  53. });
  54. };
  55. exports.addUserToProject = function(req, res) {
  56. console.log("addUserToProject");
  57. console.log(req.body);
  58. projectModel.findById(req.params.id, function(err, project) {
  59. console.log(project);
  60. project.users.push(req.body.username);
  61. console.log(project.users);
  62. project.save(function(err) {
  63. if(err) return res.send(500, err.message);
  64. projectModel.find(function(err, projects) {
  65. if(err) res.send(500, err.message);
  66. res.status(200).jsonp(projects);
  67. });
  68. });
  69. });
  70. };
  71. exports.userStartWorking = function(req, res) {
  72. console.log("userStartWorking");
  73. projectModel.findById(req.params.id, function(err, project) {
  74. var workstrike={
  75. username: req.body.username,
  76. start: new Date(),
  77. end: "",
  78. };
  79. project.workStrikes.push(workstrike);
  80. console.log(project);
  81. project.save(function(err) {
  82. if(err) return res.send(500, err.message);
  83. projectModel.find(function(err, projects) {
  84. if(err) res.send(500, err.message);
  85. res.status(200).jsonp(projects);
  86. });
  87. });
  88. });
  89. };
  90. exports.userStopWorking = function(req, res) {
  91. console.log("userStopWorking");
  92. projectModel.findById(req.params.id, function(err, project) {
  93. for(var i=0; i<project.workStrikes.length; i++)
  94. {
  95. if((project.workStrikes[i].username==req.body.username)&&(project.workStrikes[i].end==null))
  96. {
  97. project.workStrikes[i].end= new Date();
  98. }
  99. }
  100. console.log(project);
  101. project.save(function(err) {
  102. if(err) return res.send(500, err.message);
  103. projectModel.find(function(err, projects) {
  104. if(err) res.send(500, err.message);
  105. res.status(200).jsonp(projects);
  106. });
  107. });
  108. });
  109. };
  110. //PUT
  111. exports.updateProject = function(req, res) {
  112. ActivityModel.findById(req.params.id, function(err, tvshow) {
  113. tvshow.title = req.body.petId;
  114. tvshow.year = req.body.year;
  115. tvshow.country = req.body.country;
  116. tvshow.poster = req.body.poster;
  117. tvshow.seasons = req.body.seasons;
  118. tvshow.genre = req.body.genre;
  119. tvshow.summary = req.body.summary;
  120. tvshow.save(function(err) {
  121. if(err) return res.send(500, err.message);
  122. res.status(200).jsonp(tvshow);
  123. });
  124. });
  125. };
  126. //DELETE
  127. exports.deleteProject = function(req, res) {
  128. projectModel.findById(req.params.id, function(err, project) {
  129. project.remove(function(err) {
  130. if(err) return res.send(500, err.message);
  131. /*res.status(200).jsonp(req.params.id);*/
  132. console.log('DELETE /projects/' + req.params.id);
  133. projectModel.find(function(err, projects) {
  134. if(err) res.send(500, err.message);
  135. res.status(200).jsonp(projects);
  136. });
  137. })
  138. });
  139. };