Browse Source

user fav system implemented

master
idoctnef 8 years ago
parent
commit
29091426a4
4 changed files with 59 additions and 5 deletions
  1. +4
    -4
      config.js
  2. +45
    -1
      controllers/userController.js
  3. +5
    -0
      models/userModel.js
  4. +5
    -0
      server.js

+ 4
- 4
config.js

@ -1,8 +1,8 @@
module.exports = {
/*'secret': process.env.SECRET,// production version
'database': process.env.MONGO_DSN,*/
'secret': 'secretfortoken',// local version
'database': 'mongodb://localhost/comunalcar',
'secret': process.env.SECRET,// production version
'database': process.env.MONGO_DSN,
/*'secret': 'secretfortoken',// local version
'database': 'mongodb://localhost/comunalcar',*/
"port" : process.env.PORT || 3000
};

+ 45
- 1
controllers/userController.js

@ -86,6 +86,48 @@ exports.addUser = function(req, res) {
});
};
/* fav */
exports.addFav = function(req, res) {
userModel.findById(req.params.userId, function(err, user){
var fav = {
userId: req.body.userId,
username: req.body.username,
avatar: req.body.avatar
};
user.favs.push(fav);
user.save(function(err, user) {
if(err) return res.send(500, err.message);
//res.status(200).jsonp(travel);
userModel.find(function(err, users) {
if(err) res.send(500, err.message);
res.status(200).jsonp(users);
});
});
});
};
exports.doUnfav = function(req, res) {
userModel.findById(req.params.userId, function(err, user){
for(var i=0; i<user.favs.length; i++)
{
if(user.favs[i].username==req.body.username)
{
user.favs.splice(i, 1);
}
}
user.save(function(err, travel) {
if(err) return res.send(500, err.message);
//res.status(200).jsonp(travel);
userModel.find(function(err, users) {
if(err) res.send(500, err.message);
res.status(200).jsonp(users);
});
});
});
};
//PUT - Update a user already exists
exports.updateUser = function(req, res) {
userModel.findById(req.params.id, function(err, user) {
@ -145,12 +187,14 @@ exports.login = function(req, res) {
});
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
userid: user._id,
userdata: user
});
}

+ 5
- 0
models/userModel.js

@ -16,6 +16,11 @@ var userSchema = new Schema({
username: { type: String },
value: { type: Number },
comment: { type: String }
}],
favs: [{
username: { type: String },
userId: { type: String },
avatar: { type: String }
}]
})

+ 5
- 0
server.js

@ -132,6 +132,11 @@ apiRoutes.route('/travels/:travelId/join')
apiRoutes.route('/travels/:travelId/unjoin')
.post(travelCtrl.doUnjoin);
apiRoutes.route('/users/:userId/fav')
.post(userCtrl.addFav);
apiRoutes.route('/users/:userId/unfav')
.post(userCtrl.doUnfav);
apiRoutes.route('/travels/:travelId/comment')
.post(travelCtrl.addComment);

Loading…
Cancel
Save