diff --git a/controllers/travelController.js b/controllers/travelController.js index c43776e..d0bbc98 100644 --- a/controllers/travelController.js +++ b/controllers/travelController.js @@ -1,6 +1,7 @@ //File: controllers/travelController.js var mongoose = require('mongoose'); var userModel = mongoose.model('userModel'); +var notificationModel = mongoose.model('notificationModel'); var travelModel = mongoose.model('travelModel'); var commentModel = mongoose.model('commentModel'); @@ -8,28 +9,28 @@ var commentModel = mongoose.model('commentModel'); exports.getAllTravels = function(req, res) { //get travels with futures dates ($gte - greater than and equal than) travelModel.find({date: {$gte: new Date()}}) - .limit(Number(req.query.pageSize)) - .skip(Number(req.query.pageSize) * Number(req.query.page)) - .exec(function (err, travels) { - if (err) return res.send(500, err.message); - res.status(200).jsonp(travels); - }); + .limit(Number(req.query.pageSize)) + .skip(Number(req.query.pageSize) * Number(req.query.page)) + .exec(function (err, travels) { + if (err) return res.send(500, err.message); + res.status(200).jsonp(travels); + }); }; exports.getTravelById = function (req, res) { travelModel.findOne({_id: req.params.travelid}) - .lean() - .populate('joins', 'username avatar') - .populate('comments', 'comment user') - .exec(function (err, travel) { - if (err) return res.send(500, err.message); - if (!travel) { - res.json({success: false, message: 'travel not found.'}); - } else if (travel) { + .lean() + .populate('joins', 'username avatar') + .populate('comments', 'comment user') + .exec(function (err, travel) { + if (err) return res.send(500, err.message); + if (!travel) { + res.json({success: false, message: 'travel not found.'}); + } else if (travel) { - res.status(200).jsonp(travel); - } - }); + res.status(200).jsonp(travel); + } + }); }; exports.addTravel = function(req, res) { @@ -141,19 +142,22 @@ exports.addJoinPetition = function(req, res) { res.json({success: false, message: 'User not found.'}); } else if (user) { //notification - var notification = { + var notification = new notificationModel({ type: "join", message: "user "+userJoining.username+" joins your travel "+travel.title, date: new Date(), icon: 'join.png', link: "" - }; - user.notifications.push(notification); - user.save(function(err, user) { + }); + notification.save(function(err, notification) { if (err) return res.send(500, err.message); + user.notifications.push(notification._id); + user.save(function(err, user) { + if (err) return res.send(500, err.message); - console.log("notification saved"); - exports.getTravelById(req, res); + console.log("notification saved"); + exports.getTravelById(req, res); + }); }); } });//end saving notification @@ -197,43 +201,22 @@ exports.unJoin = function(req, res) { }); }; -exports.getJoinsByTravelId = function(req, res) { - joinModel.find({ - travelId: req.params.travelId - }, function(err, joins) { - - if (err) throw err; - - if (!joins) { - res.json({ success: false, message: 'no joins for travelId' }); - } else if (joins) { - // return the information including token as JSON - res.jsonp(joins); - - } - - }); -}; - -exports.findAllTravelsFromUsername = function(req, res) { - travelModel.find({ - owner: req.params.username, - date: {$gte: new Date()} - }, function(err, travels) { - - if (err) throw err; - - if (!travels) { - res.json({ success: false, message: 'no travels for user' }); - } else if (travels) { - console.log(travels); - // return the information including token as JSON - res.jsonp(travels); - - - } +exports.getTravelsByUserId = function(req, res) { + travelModel.find({ + user: req.params.userid + }) + .lean() + .populate('joins', 'username avatar') + .populate('comments', 'comment user') + .exec(function (err, travels) { + if (err) return res.send(500, err.message); + if (!travels) { + res.json({success: false, message: 'travel not found.'}); + } else if (travels) { - }); + res.status(200).jsonp(travels); + } + }); }; diff --git a/controllers/userController.js b/controllers/userController.js index 946427d..1aed7b2 100644 --- a/controllers/userController.js +++ b/controllers/userController.js @@ -1,6 +1,7 @@ //File: controllers/userController.js var mongoose = require('mongoose'); var userModel = mongoose.model('userModel'); +var notificationModel = mongoose.model('notificationModel'); /* */ diff --git a/models/notificationModel.js b/models/notificationModel.js new file mode 100644 index 0000000..4ef0e85 --- /dev/null +++ b/models/notificationModel.js @@ -0,0 +1,21 @@ +var mongoose = require('mongoose'), + Schema = mongoose.Schema; + +var mongooseUniqueValidator = require('mongoose-unique-validator'); + + +var notificationSchema = new Schema({ + user: { + type: mongoose.Schema.Types.ObjectId, + ref: 'userModel' + }, + state: {type: String, default: "pendent"},//viewed, pendent + message: {type: String}, + link: {type: String},//aquí oju, a la app i a la web calen links diferents, però ho podem fer posant sempre a la app i a la web el prefix del link (#!/app) o (#/app/), i després afegint-hi la pàgina on volem enviar el routing, per exemple (dashboard) + icon: {type: String}, + date: {type: Date}, + dateviewed: {type: Date} +}) + +notificationSchema.plugin(mongooseUniqueValidator); +module.exports = mongoose.model('notificationModel', notificationSchema); diff --git a/models/userModel.js b/models/userModel.js index 3c40289..9a02e13 100644 --- a/models/userModel.js +++ b/models/userModel.js @@ -33,12 +33,8 @@ var userSchema = new Schema({ ref: 'travelModel' }], notifications: [{ - state: {type: String, default: "pendent"},//viewed, pendent - message: {type: String}, - link: {type: String},//aquí oju, a la app i a la web calen links diferents, però ho podem fer posant sempre a la app i a la web el prefix del link (#!/app) o (#/app/), i després afegint-hi la pàgina on volem enviar el routing, per exemple (dashboard) - icon: {type: String}, - date: {type: Date}, - dateviewed: {type: Date} + type: mongoose.Schema.Types.ObjectId, + ref: 'notificationModel' }] }) diff --git a/server.js b/server.js index 6cb3506..0e622a1 100755 --- a/server.js +++ b/server.js @@ -29,10 +29,10 @@ app.use(morgan('dev')); // Import Models and controllers var userMdl = require('./models/userModel')(app, mongoose); -var userCtrl = require('./controllers/userController'); - +var notificationMdl = require('./models/notificationModel')(app, mongoose); var travelMdl = require('./models/travelModel')(app, mongoose); var commentMdl = require('./models/commentModel')(app, mongoose); +var userCtrl = require('./controllers/userController'); var travelCtrl = require('./controllers/travelController'); /*// Example Route @@ -61,11 +61,11 @@ apiRoutes.route('/signup') .post(userCtrl.signup); apiRoutes.route('/users') .get(userCtrl.getAllUsers); -apiRoutes.route('/users/getById/:userid') +apiRoutes.route('/users/getByUserId/:userid') .get(userCtrl.getUserById); apiRoutes.route('/travels') .get(travelCtrl.getAllTravels); -apiRoutes.route('/travels/getById/:travelid') +apiRoutes.route('/travels/getByTravelId/:travelid') .get(travelCtrl.getTravelById); @@ -120,6 +120,8 @@ apiRoutes.route('/travels/join/:travelid') .get(travelCtrl.addJoinPetition); apiRoutes.route('/travels/unjoin/:travelid') .get(travelCtrl.unJoin); +apiRoutes.route('/travels/getByUserId/:userid') + .get(travelCtrl.getTravelsByUserId); //FINS AQUÏ COMPROVAT apiRoutes.route('/travels/comment/:travelid')