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.

135 lines
3.8 KiB

7 years ago
7 years ago
  1. var express = require("express");
  2. var app = express();
  3. var config = require('../config'); // get our config file
  4. app.set('superSecret', config.secret); // secret variable
  5. var mongoose = require('mongoose');
  6. var userModel = mongoose.model('userModel');
  7. var eventModel = mongoose.model('eventModel');
  8. var pageSize=config.pageSize;
  9. exports.getAllEvents = function(req, res) {
  10. eventModel.find({date: {$gte: new Date()}})
  11. .lean()
  12. .populate('user', 'username')
  13. .sort('date')
  14. .limit(pageSize)
  15. .skip(pageSize * Number(req.query.page))
  16. .exec(function (err, events) {
  17. if (err) return res.send(500, err.message);
  18. res.status(200).jsonp(events);
  19. });
  20. };
  21. exports.getEventById = function (req, res) {
  22. eventModel.findOne({_id: req.params.eventid})
  23. .lean()
  24. .populate('user', 'username img description')
  25. .exec(function (err, event) {
  26. if (err) return res.send(500, err.message);
  27. if (!event) {
  28. res.json({success: false, message: 'event not found.'});
  29. } else if (event) {
  30. res.status(200).jsonp(event);
  31. }
  32. });
  33. };
  34. exports.addEvent = function(req, res) {
  35. userModel.findOne({'tokens.token': req.headers['x-access-token']})
  36. .exec(function(err, user){
  37. if (err) return res.send(500, err.message);
  38. if (!user) {
  39. console.log("user not found");
  40. res.json({success: false, message: 'User not found.'});
  41. } else if (user) {
  42. var event = new eventModel({
  43. title: req.body.title,
  44. description: req.body.description,
  45. img: req.body.img,
  46. date: req.body.date,
  47. categories: req.body.categories,
  48. generateddate: Date(),
  49. user: user._id
  50. });
  51. event.save(function(err, event) {
  52. if(err) return res.send(500, err.message);
  53. user.events.push(event._id);
  54. user.save(function (err, user) {
  55. if (err) return res.send(500, err.message);
  56. exports.getAllEvents(req, res);
  57. });
  58. });//end of event.save
  59. }
  60. });//end of usermodel.find
  61. };
  62. exports.deleteEvent = function(req, res) {
  63. userModel.findOne({'tokens.token': req.headers['x-access-token']})
  64. .exec(function(err, user){
  65. if (err) return res.send(500, err.message);
  66. eventModel.findOne({
  67. _id: req.params.eventid,
  68. user: user._id
  69. })
  70. .exec(function(err, event) {
  71. if (err) return res.send(500, err.message);
  72. if(event.user.equals(user._id))
  73. {
  74. event.remove(function(err) {
  75. if(err) return res.send(500, err.message);
  76. console.log("deleted");
  77. exports.getAllEvents(req, res);
  78. });
  79. }
  80. });
  81. });
  82. };//funciona, pero no esborra les referències dels users als events que s'esborren. Més endavant caldria fer-ho.
  83. /*
  84. un get events by following, que seria:
  85. s'envia un post /events/following
  86. amb la data:
  87. {
  88. "following": ["user1", "user2", "user3"]
  89. }
  90. que bàsicament és una array amb els followings que tens
  91. això retorna els events d'aquests users que segueixes
  92. */
  93. exports.getEventsByFollowingArray = function(req, res) {
  94. eventModel.find({
  95. date: {$gte: new Date()},
  96. 'username': req.body.users
  97. })
  98. .sort('date')
  99. .limit(pageSize)
  100. .skip(pageSize * Number(req.query.page))
  101. .exec(function (err, events) {
  102. if (err) return res.send(500, err.message);
  103. res.status(200).jsonp(events);
  104. });
  105. };
  106. /*
  107. un get events by categories, que seria:
  108. s'envia un get /events/category/:category
  109. això retorna els events d'aquests users que segueixes
  110. */
  111. exports.getEventsByCategory = function(req, res) {
  112. eventModel.find({
  113. date: {$gte: new Date()},
  114. 'categories.name': req.params.category
  115. })
  116. .sort('date')
  117. .limit(pageSize)
  118. .skip(pageSize * Number(req.query.page))
  119. .exec(function (err, events) {
  120. if (err) return res.send(500, err.message);
  121. res.status(200).jsonp(events);
  122. });
  123. };