Browse Source

travel comments system added

pull/2/head
nau 8 years ago
parent
commit
cb3d7eabba
4 changed files with 56 additions and 6 deletions
  1. +37
    -3
      controllers/travelController.js
  2. +12
    -0
      models/commentModel.js
  3. +1
    -2
      models/joinModel.js
  4. +6
    -1
      server.js

+ 37
- 3
controllers/travelController.js

@ -5,6 +5,7 @@ var travelModel = mongoose.model('travelModel');
var userModel = mongoose.model('userModel'); var userModel = mongoose.model('userModel');
var joinModel = mongoose.model('joinModel'); var joinModel = mongoose.model('joinModel');
var commentModel = mongoose.model('commentModel');
//GET //GET
exports.findAllTravels = function(req, res) { exports.findAllTravels = function(req, res) {
@ -107,11 +108,10 @@ exports.deleteTravel = function(req, res) {
/* join */ /* join */
exports.addJoin = function(req, res) { exports.addJoin = function(req, res) {
var join = new joinModel({ var join = new joinModel({
travelId: req.body.travelId,
travelId: req.params.travelId,
joinedUserId: req.body.joinedUserId, joinedUserId: req.body.joinedUserId,
joinedUsername: req.body.joinedUsername, joinedUsername: req.body.joinedUsername,
acceptedUserId: req.body.acceptedUserId,
comment: req.body.comment
acceptedUserId: req.body.acceptedUserId
}); });
join.save(function(err, join) { 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);
}
});
};

+ 12
- 0
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);

+ 1
- 2
models/joinModel.js

@ -6,8 +6,7 @@ var joinSchema = new Schema({
travelId: { type: String }, travelId: { type: String },
joinedUserId: { type: String }, joinedUserId: { type: String },
joinedUsername: { type: String }, joinedUsername: { type: String },
acceptedUserId: { type: String },
comment: { type: String }
acceptedUserId: { type: String }
}); });
module.exports = mongoose.model('joinModel', joinSchema); module.exports = mongoose.model('joinModel', joinSchema);

+ 6
- 1
server.js

@ -31,6 +31,7 @@ var userCtrl = require('./controllers/userController');
var travelMdl = require('./models/travelModel')(app, mongoose); var travelMdl = require('./models/travelModel')(app, mongoose);
var joinMdl = require('./models/joinModel')(app, mongoose); var joinMdl = require('./models/joinModel')(app, mongoose);
var commentMdl = require('./models/commentModel')(app, mongoose);
var travelCtrl = require('./controllers/travelController'); var travelCtrl = require('./controllers/travelController');
/*// Example Route /*// Example Route
@ -73,6 +74,8 @@ apiRoutes.route('/travels/:id')
.get(travelCtrl.findById); .get(travelCtrl.findById);
apiRoutes.route('/travels/join/:travelId') apiRoutes.route('/travels/join/:travelId')
.get(travelCtrl.getJoinsByTravelId); .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 // OJU AQUÏ TREC la verificació de token temporalment, per fer les proves des de l'app
// route middleware to verify a token // route middleware to verify a token
@ -119,8 +122,10 @@ apiRoutes.route('/travels/:id')
.put(travelCtrl.updateTravel) .put(travelCtrl.updateTravel)
.delete(travelCtrl.deleteTravel); .delete(travelCtrl.deleteTravel);
apiRoutes.route('/travels/join/:id')
apiRoutes.route('/travels/join/:travelId')
.post(travelCtrl.addJoin); .post(travelCtrl.addJoin);
apiRoutes.route('/travels/comment/:travelId')
.post(travelCtrl.addComment);
app.use('/api', apiRoutes); app.use('/api', apiRoutes);
// end of API routes ------------------------------------- // end of API routes -------------------------------------

Loading…
Cancel
Save