|
@ -10,7 +10,24 @@ var eventModel = mongoose.model('eventModel'); |
|
|
var pageSize=config.pageSize; |
|
|
var pageSize=config.pageSize; |
|
|
|
|
|
|
|
|
exports.getAllEvents = function(req, res) { |
|
|
exports.getAllEvents = function(req, res) { |
|
|
eventModel.find({date: {$gte: new Date()}}) |
|
|
|
|
|
|
|
|
eventModel.find({ |
|
|
|
|
|
date: {$gte: new Date()}//cal filtrar per type d'event, de si es alert o no
|
|
|
|
|
|
}) |
|
|
|
|
|
.lean() |
|
|
|
|
|
.populate('user', 'username img shortDescription') |
|
|
|
|
|
.sort('date') |
|
|
|
|
|
.limit(pageSize) |
|
|
|
|
|
.skip(pageSize * Number(req.query.page)) |
|
|
|
|
|
.exec(function (err, events) { |
|
|
|
|
|
if (err) return res.send(500, err.message); |
|
|
|
|
|
res.status(200).jsonp(events); |
|
|
|
|
|
}); |
|
|
|
|
|
}; |
|
|
|
|
|
exports.getAllAlerts = function(req, res) { |
|
|
|
|
|
eventModel.find({ |
|
|
|
|
|
date: {$gte: new Date()}, |
|
|
|
|
|
type: "alert" |
|
|
|
|
|
}) |
|
|
.lean() |
|
|
.lean() |
|
|
.populate('user', 'username img shortDescription') |
|
|
.populate('user', 'username img shortDescription') |
|
|
.sort('date') |
|
|
.sort('date') |
|
@ -21,6 +38,7 @@ exports.getAllEvents = function(req, res) { |
|
|
res.status(200).jsonp(events); |
|
|
res.status(200).jsonp(events); |
|
|
}); |
|
|
}); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
exports.getEventById = function (req, res) { |
|
|
exports.getEventById = function (req, res) { |
|
|
eventModel.findOne({_id: req.params.eventid}) |
|
|
eventModel.findOne({_id: req.params.eventid}) |
|
|
.lean() |
|
|
.lean() |
|
@ -67,6 +85,35 @@ exports.addEvent = function(req, res) { |
|
|
} |
|
|
} |
|
|
});//end of usermodel.find
|
|
|
});//end of usermodel.find
|
|
|
}; |
|
|
}; |
|
|
|
|
|
exports.addAlert = function(req, res) { |
|
|
|
|
|
userModel.findOne({'tokens.token': req.headers['x-access-token']}) |
|
|
|
|
|
.exec(function(err, user){ |
|
|
|
|
|
if (err) return res.send(500, err.message); |
|
|
|
|
|
if (!user) { |
|
|
|
|
|
console.log("user not found"); |
|
|
|
|
|
res.json({success: false, message: 'User not found.'}); |
|
|
|
|
|
} else if (user) { |
|
|
|
|
|
var event = new eventModel({ |
|
|
|
|
|
title: req.body.title, |
|
|
|
|
|
description: req.body.description, |
|
|
|
|
|
date: req.body.date, |
|
|
|
|
|
generateddate: Date(), |
|
|
|
|
|
user: user._id, |
|
|
|
|
|
type: "alert" |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
event.save(function(err, event) { |
|
|
|
|
|
if(err) return res.send(500, err.message); |
|
|
|
|
|
|
|
|
|
|
|
user.events.push(event._id); |
|
|
|
|
|
user.save(function (err, user) { |
|
|
|
|
|
if (err) return res.send(500, err.message); |
|
|
|
|
|
exports.getAllEvents(req, res); |
|
|
|
|
|
}); |
|
|
|
|
|
});//end of event.save
|
|
|
|
|
|
} |
|
|
|
|
|
});//end of usermodel.find
|
|
|
|
|
|
}; |
|
|
exports.deleteEvent = function(req, res) { |
|
|
exports.deleteEvent = function(req, res) { |
|
|
userModel.findOne({'tokens.token': req.headers['x-access-token']}) |
|
|
userModel.findOne({'tokens.token': req.headers['x-access-token']}) |
|
|
.exec(function(err, user){ |
|
|
.exec(function(err, user){ |
|
|