Browse Source

user likes system implemented

master
arnaucode 8 years ago
parent
commit
b00f4ea7d7
3 changed files with 111 additions and 0 deletions
  1. +98
    -0
      controllers/userController.js
  2. +4
    -0
      models/userModel.js
  3. +9
    -0
      server.js

+ 98
- 0
controllers/userController.js

@ -164,6 +164,19 @@ exports.getTravelsByUserId = function (req, res) {
});
});
};
exports.getUserLikes = function (req, res) {
userModel.findOne({_id: req.params.userid})
.lean()
.populate('likes', 'username avatar description')
.exec(function (err, user) {
if (err) return res.send(500, err.message);
if (!user) {
res.json({success: false, message: 'User not found.'});
} else if (user) {
res.status(200).jsonp(user.likes);
}
});
};
exports.getNotifications = function (req, res) {
userModel.findOne({'token': req.headers['x-access-token']})
.lean()
@ -197,6 +210,91 @@ exports.deleteUser = function(req, res) {
})
});
};
exports.likeUser = function(req, res) {
userModel.findOne({'token': req.headers['x-access-token']})
.exec(function (err, userL) {
if (err) return res.send(500, err.message);
if (!userL) {
res.json({success: false, message: 'no user with that token, login again'});
} else if (userL) {
userModel.findOne({
_id: req.params.userid,
likes: {'$ne': userL._id}
})
.exec(function (err, user) {
if (err) return res.send(500, err.message);
if (!user) {
res.json({success: false, message: 'Like not posible, user not exist, or like was already done'});
} else if (user) {
//res.status(200).jsonp(user);
var notification = new notificationModel({
concept: "like",
message: "user "+userL.username+" adds a like to you",
date: new Date(),
icon: 'like.png',
link: "users/" + user._id
});
notification.save(function(err, notification) {
if (err) return res.send(500, err.message);
user.likes.push(userL._id);
user.notifications.push(notification._id);
user.save(function(err, user) {
if (err) return res.send(500, err.message);
exports.getUserById(req, res);
});
});
}//end of else if user
});
}//end of else if userL
});
};
exports.unlikeUser = function(req, res) {
userModel.findOne({'token': req.headers['x-access-token']})
.exec(function (err, userL) {
if (err) return res.send(500, err.message);
if (!userL) {
res.json({success: false, message: 'no user with that token, login again'});
} else if (userL) {
userModel.findOne({
_id: req.params.userid,
likes: userL._id
})
.exec(function (err, user) {
if (err) return res.send(500, err.message);
if (!user) {
res.json({success: false, message: 'Unlike not posible'});
} else if (user) {
//res.status(200).jsonp(user);
var notification = new notificationModel({
concept: "like",
message: "user "+userL.username+" removes like on you",
date: new Date(),
icon: 'like.png',
link: "users/" + user._id
});
notification.save(function(err, notification) {
if (err) return res.send(500, err.message);
var indexOf= user.likes.indexOf(userL._id);
user.likes.splice(indexOf, 1);
user.notifications.push(notification._id);
user.save(function(err, user) {
if (err) return res.send(500, err.message);
exports.getUserById(req, res);
});
});
}//end of else if user
});
}//end of else if userL
});
};
/* fav */
exports.addFav = function(req, res) {
var tokenuser;

+ 4
- 0
models/userModel.js

@ -22,6 +22,10 @@ var userSchema = new Schema({
value: { type: Number },
comment: { type: String }
}],
likes: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'userModel'
}],
favs: [{
user: {
type: mongoose.Schema.Types.ObjectId,

+ 9
- 0
server.js

@ -135,6 +135,15 @@ apiRoutes.route('/travels/declineJoin/:travelid')
.post(travelCtrl.declineJoin);
apiRoutes.route('/travels/acceptJoin/:travelid')
.post(travelCtrl.acceptJoin);
apiRoutes.route('/users/id/likes/:userid')
.get(userCtrl.getUserLikes);
apiRoutes.route('/users/id/like/:userid')
.post(userCtrl.likeUser);
apiRoutes.route('/users/id/unlike/:userid')
.post(userCtrl.unlikeUser);
//FINS AQUÏ COMPROVAT
apiRoutes.route('/travels/comment/:travelid')

Loading…
Cancel
Save