diff --git a/controllers/searchController.js b/controllers/searchController.js new file mode 100644 index 0000000..0f1dfb7 --- /dev/null +++ b/controllers/searchController.js @@ -0,0 +1,49 @@ +//File: controllers/userController.js +var mongoose = require('mongoose'); +var userModel = mongoose.model('userModel'); +var notificationModel = mongoose.model('notificationModel'); +var travelModel = mongoose.model('travelModel'); + + +/* */ +var jwt = require('jsonwebtoken'); // used to create, sign, and verify tokens +var express = require("express"); +var app = express(); +var config = require('../config'); // get our config file +app.set('superSecret', config.secret); // secret variable + +var crypto = require('crypto'); +/* */ + + +exports.searchByString = function (req, res) { + console.log(req.params.searchstring); + userModel.find({ + username: new RegExp(req.params.searchstring, "i") + })//perquè retorni tots els objectes que continguin l'string sense necessitat de que sigui exactament la mateixa string + .limit(Number(req.query.pageSize)) + .skip(Number(req.query.pageSize) * Number(req.query.page)) + .lean() + .select('username avatar') + .exec(function (err, users) { + if (err) return res.send(500, err.message); + travelModel.find({ + $or:[ + {from: new RegExp(req.params.searchstring, "i")}, + {to: new RegExp(req.params.searchstring, "i")}, + {title: new RegExp(req.params.searchstring, "i")} + ] + })//perquè retorni tots els objectes que continguin l'string sense necessitat de que sigui exactament la mateixa string + .limit(Number(req.query.pageSize)) + .skip(Number(req.query.pageSize) * Number(req.query.page)) + .lean() + .select('title from to date type') + .exec(function (err, travels) { + if (err) return res.send(500, err.message); + res.json({ + users: users, + travels: travels + }); + });//travels + });//users +}; diff --git a/controllers/travelController.js b/controllers/travelController.js index d80d2b7..4ad7cb3 100644 --- a/controllers/travelController.js +++ b/controllers/travelController.js @@ -144,11 +144,11 @@ exports.addJoinPetition = function(req, res) { } else if (user) { //notification var notification = new notificationModel({ - type: "join", + concept: "join", message: "user "+userJoining.username+" joins your travel "+travel.title, date: new Date(), icon: 'join.png', - link: "" + link: "travels/" + travel._id }); notification.save(function(err, notification) { if (err) return res.send(500, err.message); @@ -171,13 +171,13 @@ exports.addJoinPetition = function(req, res) { exports.unJoin = function(req, res) { userModel.findOne({'token': req.headers['x-access-token']}) - .exec(function(err, user){ - if (!user) { - res.json({success: false, message: 'User not found.'}); - } else if (user) { + .exec(function(err, userJoining){ + if (!userJoining) { + res.json({success: false, message: 'userJoining not found.'}); + } else if (userJoining) { travelModel.findOne({ _id: req.params.travelid, - joinPetitions: user._id + joinPetitions: userJoining._id }) .exec(function(err, travel){ if (err) return res.send(500, err.message); @@ -186,14 +186,40 @@ exports.unJoin = function(req, res) { } else if (travel) { for(var i=0; i