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.

133 lines
3.7 KiB

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