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.

183 lines
5.1 KiB

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