From cb3d7eabba9c3d2b2bab474fd07a744b3a1e390a Mon Sep 17 00:00:00 2001 From: nau Date: Mon, 5 Sep 2016 20:28:14 +0200 Subject: [PATCH] travel comments system added --- controllers/travelController.js | 40 ++++++++++++++++++++++++++++++--- models/commentModel.js | 12 ++++++++++ models/joinModel.js | 3 +-- server.js | 7 +++++- 4 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 models/commentModel.js diff --git a/controllers/travelController.js b/controllers/travelController.js index 74d51a7..c005a16 100644 --- a/controllers/travelController.js +++ b/controllers/travelController.js @@ -5,6 +5,7 @@ var travelModel = mongoose.model('travelModel'); var userModel = mongoose.model('userModel'); var joinModel = mongoose.model('joinModel'); +var commentModel = mongoose.model('commentModel'); //GET exports.findAllTravels = function(req, res) { @@ -107,11 +108,10 @@ exports.deleteTravel = function(req, res) { /* join */ exports.addJoin = function(req, res) { var join = new joinModel({ - travelId: req.body.travelId, + travelId: req.params.travelId, joinedUserId: req.body.joinedUserId, joinedUsername: req.body.joinedUsername, - acceptedUserId: req.body.acceptedUserId, - comment: req.body.comment + acceptedUserId: req.body.acceptedUserId }); join.save(function(err, join) { @@ -137,3 +137,37 @@ exports.getJoinsByTravelId = function(req, res) { }); }; + + +/* comment */ +exports.addComment = function(req, res) { + var comment = new commentModel({ + travelId: req.params.travelId, + commentUserId: req.body.commentUserId, + commentUsername: req.body.commentUsername, + comment: req.body.comment + }); + + comment.save(function(err, comment) { + if(err) return res.send(500, err.message); + res.status(200).jsonp(comment); + }); +}; + +exports.getCommentsByTravelId = function(req, res) { + commentModel.find({ + travelId: req.params.travelId + }, function(err, comments) { + + if (err) throw err; + + if (!comments) { + res.json({ success: false, message: 'no comments for travelId' }); + } else if (comments) { + // return the information including token as JSON + res.jsonp(comments); + + } + + }); +}; diff --git a/models/commentModel.js b/models/commentModel.js new file mode 100644 index 0000000..37650cc --- /dev/null +++ b/models/commentModel.js @@ -0,0 +1,12 @@ +var mongoose = require('mongoose'), + Schema = mongoose.Schema; + + +var commentSchema = new Schema({ + travelId: { type: String }, + commentUserId: { type: String }, + commentUsername: { type: String }, + comment: { type: String } + +}); +module.exports = mongoose.model('commentModel', commentSchema); diff --git a/models/joinModel.js b/models/joinModel.js index d59fd2e..1b26d64 100644 --- a/models/joinModel.js +++ b/models/joinModel.js @@ -6,8 +6,7 @@ var joinSchema = new Schema({ travelId: { type: String }, joinedUserId: { type: String }, joinedUsername: { type: String }, - acceptedUserId: { type: String }, - comment: { type: String } + acceptedUserId: { type: String } }); module.exports = mongoose.model('joinModel', joinSchema); diff --git a/server.js b/server.js index e83ae03..55c6bcb 100755 --- a/server.js +++ b/server.js @@ -31,6 +31,7 @@ var userCtrl = require('./controllers/userController'); var travelMdl = require('./models/travelModel')(app, mongoose); var joinMdl = require('./models/joinModel')(app, mongoose); +var commentMdl = require('./models/commentModel')(app, mongoose); var travelCtrl = require('./controllers/travelController'); /*// Example Route @@ -73,6 +74,8 @@ apiRoutes.route('/travels/:id') .get(travelCtrl.findById); apiRoutes.route('/travels/join/:travelId') .get(travelCtrl.getJoinsByTravelId); +apiRoutes.route('/travels/comment/:travelId') + .get(travelCtrl.getCommentsByTravelId); // OJU AQUƏ TREC la verificaciĆ³ de token temporalment, per fer les proves des de l'app // route middleware to verify a token @@ -119,8 +122,10 @@ apiRoutes.route('/travels/:id') .put(travelCtrl.updateTravel) .delete(travelCtrl.deleteTravel); -apiRoutes.route('/travels/join/:id') +apiRoutes.route('/travels/join/:travelId') .post(travelCtrl.addJoin); +apiRoutes.route('/travels/comment/:travelId') + .post(travelCtrl.addComment); app.use('/api', apiRoutes); // end of API routes -------------------------------------