From 840d7de000b3d2ce5dd85e1769986c052ecf4e4a Mon Sep 17 00:00:00 2001 From: arnaucode Date: Sun, 16 Oct 2016 23:25:39 +0200 Subject: [PATCH] all posts runing by token --- controllers/travelController.js | 233 ++++++++++++++++++-------------- controllers/userController.js | 56 +++++--- models/userModel.js | 1 + 3 files changed, 171 insertions(+), 119 deletions(-) diff --git a/controllers/travelController.js b/controllers/travelController.js index 6d6d63f..4c898aa 100644 --- a/controllers/travelController.js +++ b/controllers/travelController.js @@ -52,55 +52,59 @@ exports.findAllTravelsFromUsername = function(req, res) { exports.addTravel = function(req, res) { 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 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 */ 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) { + 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