Browse Source

updated

master
arnaucode 7 years ago
parent
commit
58ee9bd985
5 changed files with 110 additions and 17 deletions
  1. +49
    -0
      controllers/searchController.js
  2. +36
    -10
      controllers/travelController.js
  3. +16
    -3
      controllers/userController.js
  4. +2
    -2
      models/notificationModel.js
  5. +7
    -2
      server.js

+ 49
- 0
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
};

+ 36
- 10
controllers/travelController.js

@ -144,11 +144,11 @@ exports.addJoinPetition = function(req, res) {
} else if (user) { } else if (user) {
//notification //notification
var notification = new notificationModel({ var notification = new notificationModel({
type: "join",
concept: "join",
message: "user "+userJoining.username+" joins your travel "+travel.title, message: "user "+userJoining.username+" joins your travel "+travel.title,
date: new Date(), date: new Date(),
icon: 'join.png', icon: 'join.png',
link: ""
link: "travels/" + travel._id
}); });
notification.save(function(err, notification) { notification.save(function(err, notification) {
if (err) return res.send(500, err.message); if (err) return res.send(500, err.message);
@ -171,13 +171,13 @@ exports.addJoinPetition = function(req, res) {
exports.unJoin = function(req, res) { exports.unJoin = function(req, res) {
userModel.findOne({'token': req.headers['x-access-token']}) 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({ travelModel.findOne({
_id: req.params.travelid, _id: req.params.travelid,
joinPetitions: user._id
joinPetitions: userJoining._id
}) })
.exec(function(err, travel){ .exec(function(err, travel){
if (err) return res.send(500, err.message); if (err) return res.send(500, err.message);
@ -186,14 +186,40 @@ exports.unJoin = function(req, res) {
} else if (travel) { } else if (travel) {
for(var i=0; i<travel.joinPetitions.length; i++) for(var i=0; i<travel.joinPetitions.length; i++)
{ {
if(travel.joinPetitions[i].equals(user._id))
if(travel.joinPetitions[i].equals(userJoining._id))
{ {
travel.joinPetitions.splice(i, 1); travel.joinPetitions.splice(i, 1);
} }
} }
travel.save(function(err, travel) { travel.save(function(err, travel) {
if(err) return res.send(500, err.message); if(err) return res.send(500, err.message);
exports.getTravelById(req, res);
//start saving notification, get user owner of travel
userModel.findOne({_id: travel.user})
.exec(function(err, user){
if (err) return res.send(500, err.message);
if (!user) {
res.json({success: false, message: 'User not found.'});
} else if (user) {
//notification
var notification = new notificationModel({
concept: "unjoin",
message: "user "+userJoining.username+" unjoins your travel "+travel.title,
date: new Date(),
icon: 'unjoin.png',
link: "travels/" + travel._id
});
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);
});
});
}
});//end saving notification
}); });
} }
}); });
@ -270,7 +296,7 @@ exports.addComment = function(req, res) {
var userowner=userowners[0]; var userowner=userowners[0];
//notification //notification
var notification = { var notification = {
type: "comment",
concept: "comment",
otherusername: user.username, otherusername: user.username,
description: "user "+user.username+" comments your travel "+travel.title, description: "user "+user.username+" comments your travel "+travel.title,
date: new Date(), date: new Date(),

+ 16
- 3
controllers/userController.js

@ -107,7 +107,7 @@ exports.getAllUsers = function(req, res) {
}); });
}; };
//GET - Return a User with specified ID
exports.getUserById = function (req, res) { exports.getUserById = function (req, res) {
userModel.findOne({_id: req.params.userid}) userModel.findOne({_id: req.params.userid})
.lean() .lean()
@ -122,7 +122,6 @@ exports.getUserById = function (req, res) {
} }
}); });
}; };
//GET - Return a User with specified ID
exports.getUserByToken = function (req, res) { exports.getUserByToken = function (req, res) {
userModel.findOne({'token': req.headers['x-access-token']}) userModel.findOne({'token': req.headers['x-access-token']})
.lean() .lean()
@ -137,6 +136,20 @@ exports.getUserByToken = function (req, res) {
} }
}); });
}; };
exports.getNotifications = function (req, res) {
userModel.findOne({'token': req.headers['x-access-token']})
.lean()
.populate('notifications')
.exec(function (err, user) {
if (err) return res.send(500, err.message);
if (!user) {
res.json({success: false, message: 'User not found.'});
} else if (user) {
res.status(200).jsonp(user.notifications);
}
});
};
exports.updateUser = function (req, res) { exports.updateUser = function (req, res) {
userModel.update({'token': req.headers['x-access-token']}, req.body, userModel.update({'token': req.headers['x-access-token']}, req.body,
@ -197,7 +210,7 @@ exports.addFav = function(req, res) {
//notification //notification
var notification = { var notification = {
type: "fav",
concept: "like",
otherusername: tokenuser.username, otherusername: tokenuser.username,
description: "user " + tokenuser.username + " favs you", description: "user " + tokenuser.username + " favs you",
date: new Date(), date: new Date(),

+ 2
- 2
models/notificationModel.js

@ -10,9 +10,9 @@ var notificationSchema = new Schema({
ref: 'userModel' ref: 'userModel'
}, },
state: {type: String, default: "pendent"},//viewed, pendent state: {type: String, default: "pendent"},//viewed, pendent
concept: {type: String},
message: {type: String}, 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},
link: {type: String},//aquí oju, a la app i a la web calen links diferents
date: {type: Date}, date: {type: Date},
dateviewed: {type: Date} dateviewed: {type: Date}
}) })

+ 7
- 2
server.js

@ -33,6 +33,7 @@ var notificationMdl = require('./models/notificationModel')(app, mongoose);
var travelMdl = require('./models/travelModel')(app, mongoose); var travelMdl = require('./models/travelModel')(app, mongoose);
var commentMdl = require('./models/commentModel')(app, mongoose); var commentMdl = require('./models/commentModel')(app, mongoose);
var userCtrl = require('./controllers/userController'); var userCtrl = require('./controllers/userController');
var searchCtrl = require('./controllers/searchController');
var travelCtrl = require('./controllers/travelController'); var travelCtrl = require('./controllers/travelController');
/*// Example Route /*// Example Route
@ -107,6 +108,10 @@ apiRoutes.use(function(req, res, next) {
}); //fi verificació de token }); //fi verificació de token
apiRoutes.route('/search/:searchstring')
.get(searchCtrl.searchByString);
apiRoutes.route('/notifications')
.get(userCtrl.getNotifications);
apiRoutes.route('/users/token') apiRoutes.route('/users/token')
.get(userCtrl.getUserByToken); .get(userCtrl.getUserByToken);
apiRoutes.route('/users')//agafa l'user a partir del token apiRoutes.route('/users')//agafa l'user a partir del token
@ -118,9 +123,9 @@ apiRoutes.route('/travels/id/modify/:travelid')
.put(travelCtrl.updateTravel)//no comprovat .put(travelCtrl.updateTravel)//no comprovat
.delete(travelCtrl.deleteTravel); .delete(travelCtrl.deleteTravel);
apiRoutes.route('/travels/join/:travelid') apiRoutes.route('/travels/join/:travelid')
.get(travelCtrl.addJoinPetition);
.post(travelCtrl.addJoinPetition);
apiRoutes.route('/travels/unjoin/:travelid') apiRoutes.route('/travels/unjoin/:travelid')
.get(travelCtrl.unJoin);
.post(travelCtrl.unJoin);
apiRoutes.route('/travels/byuser/id/:userid') apiRoutes.route('/travels/byuser/id/:userid')
.get(travelCtrl.getTravelsByUserId); .get(travelCtrl.getTravelsByUserId);
//FINS AQUÏ COMPROVAT //FINS AQUÏ COMPROVAT

Loading…
Cancel
Save