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.

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