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.

107 lines
4.2 KiB

7 years ago
  1. var express = require('express');
  2. var app = express();
  3. var runModel = require('../models/runModel');
  4. var publicationModel = require('../models/publicationModel');
  5. var userModel = require('../models/userModel');
  6. //var trainerModel = require('../models/trainerModel');
  7. var crypto = require('crypto');
  8. /**POST '/publications' **/
  9. exports.postRun = function (req, res) {
  10. userModel.findOne({'tokens.token': req.headers['x-access-token']}, function (err, user) {
  11. if (err) return res.send(500, err.message);
  12. if (!user) {
  13. res.json({success: false, message: 'user not found.'});
  14. } else if (user) {
  15. //aquí ja hem agafat el user a partir del seu token
  16. var run = new runModel(req.body.newRun);
  17. run.user=user._id;
  18. //fins aquí tenim la variable publication amb els continguts
  19. //ara cal 1r guardar el model publication a la base de dades
  20. run.save(function (err, run) {
  21. if (err) return res.send(500, err.message);
  22. //i 2n, afegir la id de la publicació generada al user.publications
  23. user.runs.push(run._id);
  24. /* gamification */
  25. var reward = {
  26. concept: "added new run to user",
  27. date: Date(),
  28. value: +1
  29. };
  30. user.points.history.push(reward);
  31. user.points.total = user.points.total + 1;
  32. /* end of gamification */
  33. user.save(function (err, user) {
  34. if (err) return res.send(500, err.message);
  35. //res.status(200).jsonp(user);
  36. //ara farem una publicació ensenyant que ha fet aquest run
  37. var publication = new publicationModel({
  38. title: "new run '" + run.title + "'!",
  39. content: "distance of: " + run.distance + ". View my runs at my profile",
  40. date: new Date(),
  41. user: user._id,
  42. photo: run.photo
  43. });
  44. //fins aquí tenim la variable publication amb els continguts
  45. //ara cal 1r guardar el model publication a la base de dades
  46. publication.save(function (err, publication) {
  47. if (err) return res.send(500, err.message);
  48. //i 2n, afegir la id de la publicació generada al user.publications
  49. user.publications.push(publication._id);
  50. /* gamification */
  51. var reward = {
  52. concept: "added new publication to Timeline",
  53. date: Date(),
  54. value: +1
  55. };
  56. user.points.history.push(reward);
  57. user.points.total = user.points.total + 1;
  58. /* end of gamification */
  59. if(!user.totalkm)
  60. {
  61. user.totalkm=0;
  62. }
  63. user.totalkm=user.totalkm + run.distance;
  64. user.save(function (err, user) {
  65. if (err) return res.send(500, err.message);
  66. res.status(200).jsonp(user);
  67. });
  68. });
  69. });
  70. });
  71. }//end else if
  72. });
  73. };
  74. /**GET '/users/:userid/publications' **/
  75. exports.getRunsByUserId = function (req, res) {
  76. userModel.findOne({
  77. _id: req.params.userid
  78. })
  79. .populate('runs')
  80. .exec(function (error, user) {
  81. if (error !== null) res.send(500, error.message);
  82. res.status(200).jsonp(user);
  83. });
  84. };
  85. /** GET '/run/getById/:publicationid' **/
  86. exports.getRunByRunId = function (req, res) {
  87. runModel.findOne({_id: req.params.runid})
  88. .lean()
  89. .populate('user', 'name avatar')
  90. .exec(function (err, run) {
  91. if (err) return res.send(500, err.message);
  92. if (!run) {
  93. res.json({success: false, message: 'run not found.'});
  94. } else if (run) {
  95. res.status(200).jsonp(run);
  96. }
  97. });
  98. };