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.

201 lines
5.0 KiB

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