Browse Source

all posts runing by token

master
arnaucode 8 years ago
parent
commit
840d7de000
3 changed files with 171 additions and 119 deletions
  1. +131
    -102
      controllers/travelController.js
  2. +39
    -17
      controllers/userController.js
  3. +1
    -0
      models/userModel.js

+ 131
- 102
controllers/travelController.js

@ -52,55 +52,59 @@ exports.findAllTravelsFromUsername = function(req, res) {
exports.addTravel = function(req, res) { exports.addTravel = function(req, res) {
console.log('POST new travel, title: ' + req.body.title); console.log('POST new travel, title: ' + req.body.title);
var travel = new travelModel({
title: req.body.title,
description: req.body.description,
owner: req.body.owner,
from: req.body.from,
to: req.body.to,
date: req.body.date,
periodic: req.body.periodic,
generateddate: req.body.generateddate,
seats: req.body.seats,
package: req.body.package,
icon: req.body.icon,
phone: req.body.phone,
telegram: req.body.telegram,
collectivized: req.body.collectivized,
modality: req.body.modality
});
if(travel.title==undefined)
{
return res.status(500).jsonp("empty inputs");
}else if(travel.description==undefined)
{
return res.status(500).jsonp("empty inputs");
}else if(travel.from==undefined)
{
return res.status(500).jsonp("empty inputs");
}else if(travel.to==undefined)
{
return res.status(500).jsonp("empty inputs");
}else if(travel.date==undefined)
{
return res.status(500).jsonp("empty inputs");
}else if(travel.seats==undefined)
{
return res.status(500).jsonp("empty inputs");
}else if(travel.title==undefined)
{
return res.status(500).jsonp("empty inputs");
}
travel.save(function(err, travel) {
if(err) return res.send(500, err.message);
//res.status(200).jsonp(travel);
travelModel.find({date: {$gte: new Date()}}, function(err, travels) {
if(err) res.send(500, err.message);
res.status(200).jsonp(travels);
userModel.find({
token: req.headers['x-access-token']
}, function(err, users){
var user=users[0];
var travel = new travelModel({
title: req.body.title,
description: req.body.description,
owner: user.username,
from: req.body.from,
to: req.body.to,
date: req.body.date,
periodic: req.body.periodic,
generateddate: req.body.generateddate,
seats: req.body.seats,
package: req.body.package,
icon: req.body.icon,
phone: user.phone,
telegram: user.telegram,
collectivized: req.body.collectivized,
modality: req.body.modality
}); });
});
if(travel.title==undefined)
{
return res.status(500).jsonp("empty inputs");
}else if(travel.description==undefined)
{
return res.status(500).jsonp("empty inputs");
}else if(travel.from==undefined)
{
return res.status(500).jsonp("empty inputs");
}else if(travel.to==undefined)
{
return res.status(500).jsonp("empty inputs");
}else if(travel.date==undefined)
{
return res.status(500).jsonp("empty inputs");
}else if(travel.title==undefined)
{
return res.status(500).jsonp("empty inputs");
}
travel.save(function(err, travel) {
if(err) return res.send(500, err.message);
//res.status(200).jsonp(travel);
travelModel.find({date: {$gte: new Date()}}, function(err, travels) {
if(err) res.send(500, err.message);
res.status(200).jsonp(travels);
});
});//end of travel.save
});//end of usermodel.find
}; };
@ -124,59 +128,79 @@ exports.updateTravel = function(req, res) {
//DELETE //DELETE
exports.deleteTravel = function(req, res) { exports.deleteTravel = function(req, res) {
travelModel.findById(req.params.id, function(err, travel) {
travel.remove(function(err) {
if(err) return res.send(500, err.message);
userModel.find({
token: req.headers['x-access-token']
}, function(err, users){
var user=users[0];
travelModel.find({date: {$gte: new Date()}}, function(err, travels) {
if(err) res.send(500, err.message);
res.status(200).jsonp(travels);
});
})
travelModel.findById(req.params.id, function(err, travel) {
if(travel.owner==user.username)
{
travel.remove(function(err) {
if(err) return res.send(500, err.message);
travelModel.find({date: {$gte: new Date()}}, function(err, travels) {
if(err) res.send(500, err.message);
res.status(200).jsonp(travels);
});
});
}
});
}); });
}; };
/* join */ /* join */
exports.addJoin = function(req, res) { exports.addJoin = function(req, res) {
travelModel.findById(req.params.travelId, function(err, travel){
console.log(travel.title);
var join = {
joinedUserId: req.body.joinedUserId,
joinedUsername: req.body.joinedUsername,
acceptedUserId: req.body.acceptedUserId,
joinedAvatar: req.body.joinedAvatar
};
travel.joins.push(join);
travel.save(function(err, travel) {
if(err) return res.send(500, err.message);
//res.status(200).jsonp(travel);
travelModel.find({date: {$gte: new Date()}}, function(err, travels) {
if(err) res.send(500, err.message);
res.status(200).jsonp(travels);
userModel.find({
token: req.headers['x-access-token']
}, function(err, users){
var user=users[0];
travelModel.findById(req.params.travelId, function(err, travel){
console.log(travel.title);
var join = {
joinedUserId: user._id,
joinedUsername: user.username,
acceptedUserId: req.body.acceptedUserId,
joinedAvatar: user.avatar
};
travel.joins.push(join);
travel.save(function(err, travel) {
if(err) return res.send(500, err.message);
//res.status(200).jsonp(travel);
travelModel.find({date: {$gte: new Date()}}, function(err, travels) {
if(err) res.send(500, err.message);
res.status(200).jsonp(travels);
});
}); });
}); });
}); });
}; };
exports.doUnjoin = function(req, res) { exports.doUnjoin = function(req, res) {
userModel.find({
token: req.headers['x-access-token']
}, function(err, users){
var user=users[0];
travelModel.findById(req.params.travelId, function(err, travel){
for(var i=0; i<travel.joins.length; i++)
{
if(travel.joins[i].joinedUsername==req.body.joinedUsername)
travelModel.findById(req.params.travelId, function(err, travel){
for(var i=0; i<travel.joins.length; i++)
{ {
travel.joins.splice(i, 1);
if(travel.joins[i].joinedUsername==user.username)
{
travel.joins.splice(i, 1);
}
} }
}
travel.save(function(err, travel) {
if(err) return res.send(500, err.message);
//res.status(200).jsonp(travel);
travelModel.find({date: {$gte: new Date()}}, function(err, travels) {
if(err) res.send(500, err.message);
res.status(200).jsonp(travels);
travel.save(function(err, travel) {
if(err) return res.send(500, err.message);
//res.status(200).jsonp(travel);
travelModel.find({date: {$gte: new Date()}}, function(err, travels) {
if(err) res.send(500, err.message);
res.status(200).jsonp(travels);
});
}); });
}); });
}); });
@ -215,26 +239,31 @@ exports.addComment = function(req, res) {
if(err) return res.send(500, err.message); if(err) return res.send(500, err.message);
res.status(200).jsonp(comment); res.status(200).jsonp(comment);
});*/ });*/
travelModel.findById(req.params.travelId, function(err, travel){
console.log(travel.title);
var comment = {
commentUserId: req.body.commentUserId,
commentUsername: req.body.commentUsername,
comment: req.body.comment,
commentAvatar: req.body.commentAvatar
};
travel.comments.push(comment);
travel.save(function(err, travel) {
if(err) return res.send(500, err.message);
//res.status(200).jsonp(travel);
travelModel.find({date: {$gte: new Date()}}, function(err, travels) {
if(err) res.send(500, err.message);
res.status(200).jsonp(travels);
userModel.find({
token: req.headers['x-access-token']
}, function(err, users){
var user=users[0];
travelModel.findById(req.params.travelId, function(err, travel){
console.log(travel.title);
var comment = {
commentUserId: user._id,
commentUsername: user.username,
comment: req.body.comment,
commentAvatar: user.avatar
};
travel.comments.push(comment);
travel.save(function(err, travel) {
if(err) return res.send(500, err.message);
//res.status(200).jsonp(travel);
travelModel.find({date: {$gte: new Date()}}, function(err, travels) {
if(err) res.send(500, err.message);
res.status(200).jsonp(travels);
});
}); });
}); });
});
});//end of userModel.find
}; };
exports.getCommentsByTravelId = function(req, res) { exports.getCommentsByTravelId = function(req, res) {

+ 39
- 17
controllers/userController.js

@ -21,6 +21,7 @@ exports.findAllUsers = function(req, res) {
for(var i=0; i<users.length; i++) for(var i=0; i<users.length; i++)
{ {
users[i].password=""; users[i].password="";
users[i].token="";
console.log(users[i].password); console.log(users[i].password);
} }
@ -38,6 +39,7 @@ exports.findById = function(req, res) {
//password deletion //password deletion
if(user!=null){ if(user!=null){
user.password=""; user.password="";
users.token="";
} }
res.status(200).jsonp(user); res.status(200).jsonp(user);
}); });
@ -56,6 +58,7 @@ exports.findUserByUsername = function(req, res) {
// return the information including token as JSON // return the information including token as JSON
//res.jsonp(user); //res.jsonp(user);
user.password=""; user.password="";
users.token="";
console.log(user); console.log(user);
res.status(200).jsonp(user[0]); res.status(200).jsonp(user[0]);
@ -95,7 +98,7 @@ exports.addUser = function(req, res) {
{ {
return res.status(500).jsonp("empty inputs"); return res.status(500).jsonp("empty inputs");
} }
user.save(function(err, user) { user.save(function(err, user) {
if(err) return res.send(500, err.message); if(err) return res.send(500, err.message);
res.status(200).jsonp(user); res.status(200).jsonp(user);
@ -104,13 +107,19 @@ exports.addUser = function(req, res) {
/* fav */ /* fav */
exports.addFav = function(req, res) { exports.addFav = function(req, res) {
var tokenuser;
userModel.find({
token: req.headers['x-access-token']
}, function(err, users){
tokenuser=users[0];
});
userModel.findById(req.params.userId, function(err, user){ userModel.findById(req.params.userId, function(err, user){
// first search if user have already said like // first search if user have already said like
var favRepeated=false; var favRepeated=false;
for(var i=0; i<user.favs.length; i++) for(var i=0; i<user.favs.length; i++)
{ {
if(user.favs[i].username==req.body.username)
if(user.favs[i].username==tokenuser.username)
{ {
favRepeated=true; favRepeated=true;
} }
@ -119,9 +128,9 @@ exports.addFav = function(req, res) {
if(favRepeated==false) if(favRepeated==false)
{ {
var fav = { var fav = {
userId: req.body.userId,
username: req.body.username,
avatar: req.body.avatar
userId: tokenuser._id,
username: tokenuser.username,
avatar: tokenuser.avatar
}; };
user.favs.push(fav); user.favs.push(fav);
@ -143,11 +152,17 @@ exports.addFav = function(req, res) {
}); });
}; };
exports.doUnfav = function(req, res) { exports.doUnfav = function(req, res) {
var tokenuser;
userModel.find({
token: req.headers['x-access-token']
}, function(err, users){
tokenuser=users[0];
});
userModel.findById(req.params.userId, function(err, user){ userModel.findById(req.params.userId, function(err, user){
for(var i=0; i<user.favs.length; i++) for(var i=0; i<user.favs.length; i++)
{ {
if(user.favs[i].username==req.body.username)
if(user.favs[i].username==tokenuser.username)
{ {
user.favs.splice(i, 1); user.favs.splice(i, 1);
} }
@ -178,6 +193,7 @@ exports.updateUser = function(req, res) {
user.save(function(err) { user.save(function(err) {
if(err) return res.send(500, err.message); if(err) return res.send(500, err.message);
user.password=""; user.password="";
users.token="";
res.status(200).jsonp(user); res.status(200).jsonp(user);
}); });
}); });
@ -221,17 +237,23 @@ exports.login = function(req, res) {
//expiresInMinutes: 1440 // expires in 24 hours //expiresInMinutes: 1440 // expires in 24 hours
//expiresIn: '60m' //expiresIn: '60m'
}); });
console.log(user);
// return the information including token as JSON
user.password="";
res.json({
success: true,
message: 'Enjoy your token!',
token: token,
avatar: user.avatar,
userid: user._id,
userdata: user
});
user.token=token;
user.save(function(err, user) {
if(err) return res.send(500, err.message);
//res.status(200).jsonp(travel);
console.log(user);
// return the information including token as JSON
user.password="";
res.json({
success: true,
message: 'Enjoy your token!',
token: token,
avatar: user.avatar,
userid: user._id,
userdata: user
});
});
} }
} }

+ 1
- 0
models/userModel.js

@ -7,6 +7,7 @@ var mongooseUniqueValidator = require('mongoose-unique-validator');
var userSchema = new Schema({ var userSchema = new Schema({
username: { type: String, unique: true }, username: { type: String, unique: true },
password: { type: String }, password: { type: String },
token: { type: String },
description: { type: String }, description: { type: String },
avatar: { type: String }, avatar: { type: String },
mail: { type: String }, mail: { type: String },

Loading…
Cancel
Save