From 8e98c356a4bfbdccd7913187cb22079693a3a107 Mon Sep 17 00:00:00 2001 From: nau Date: Mon, 5 Sep 2016 17:45:25 +0200 Subject: [PATCH] delete travel and join travel implemented --- controllers/travelController.js | 37 +++++++++++++++++++++++++++++++++ controllers/userController.js | 6 ++++-- models/joinModel.js | 13 ++++++++++++ models/travelModel.js | 4 +--- server.js | 6 ++++++ 5 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 models/joinModel.js diff --git a/controllers/travelController.js b/controllers/travelController.js index 7b232ca..74d51a7 100644 --- a/controllers/travelController.js +++ b/controllers/travelController.js @@ -4,6 +4,8 @@ var travelModel = mongoose.model('travelModel'); var userModel = mongoose.model('userModel'); +var joinModel = mongoose.model('joinModel'); + //GET exports.findAllTravels = function(req, res) { @@ -100,3 +102,38 @@ exports.deleteTravel = function(req, res) { }) }); }; + + +/* join */ +exports.addJoin = function(req, res) { + var join = new joinModel({ + travelId: req.body.travelId, + joinedUserId: req.body.joinedUserId, + joinedUsername: req.body.joinedUsername, + acceptedUserId: req.body.acceptedUserId, + comment: req.body.comment + }); + + join.save(function(err, join) { + if(err) return res.send(500, err.message); + res.status(200).jsonp(join); + }); +}; + +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); + + } + + }); +}; diff --git a/controllers/userController.js b/controllers/userController.js index 54fac03..f7d4401 100644 --- a/controllers/userController.js +++ b/controllers/userController.js @@ -34,8 +34,9 @@ exports.findById = function(req, res) { console.log('GET /users/' + req.params.id); //password deletion - + if(user!=null){ user.password=""; + } res.status(200).jsonp(user); }); }; @@ -144,7 +145,8 @@ console.log(user); success: true, message: 'Enjoy your token!', token: token, - avatar: user.avatar + avatar: user.avatar, + userid: user._id }); } diff --git a/models/joinModel.js b/models/joinModel.js new file mode 100644 index 0000000..d59fd2e --- /dev/null +++ b/models/joinModel.js @@ -0,0 +1,13 @@ +var mongoose = require('mongoose'), + Schema = mongoose.Schema; + + +var joinSchema = new Schema({ + travelId: { type: String }, + joinedUserId: { type: String }, + joinedUsername: { type: String }, + acceptedUserId: { type: String }, + comment: { type: String } + +}); +module.exports = mongoose.model('joinModel', joinSchema); diff --git a/models/travelModel.js b/models/travelModel.js index c163199..2a99030 100644 --- a/models/travelModel.js +++ b/models/travelModel.js @@ -17,9 +17,7 @@ var travelSchema = new Schema({ phone: { type: Number }, telegram: { type: String }, collectivized: { type: Boolean }, - modality: { type: String }, //if is an offering travel or asking for travel - joinedusers: { type: String }, - acceptedusers: { type: String } + modality: { type: String } //if is an offering travel or asking for travel }) module.exports = mongoose.model('travelModel', travelSchema); diff --git a/server.js b/server.js index fd1fba5..e83ae03 100755 --- a/server.js +++ b/server.js @@ -30,6 +30,7 @@ var userMdl = require('./models/userModel')(app, mongoose); var userCtrl = require('./controllers/userController'); var travelMdl = require('./models/travelModel')(app, mongoose); +var joinMdl = require('./models/joinModel')(app, mongoose); var travelCtrl = require('./controllers/travelController'); /*// Example Route @@ -70,6 +71,8 @@ apiRoutes.route('/travels') apiRoutes.route('/travels/:id') .get(travelCtrl.findById); +apiRoutes.route('/travels/join/:travelId') + .get(travelCtrl.getJoinsByTravelId); // OJU AQUƏ TREC la verificaciĆ³ de token temporalment, per fer les proves des de l'app // route middleware to verify a token @@ -116,6 +119,9 @@ apiRoutes.route('/travels/:id') .put(travelCtrl.updateTravel) .delete(travelCtrl.deleteTravel); +apiRoutes.route('/travels/join/:id') + .post(travelCtrl.addJoin); + app.use('/api', apiRoutes); // end of API routes -------------------------------------