Browse Source

user likes system implemented

master
arnaucode 7 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) { exports.getNotifications = function (req, res) {
userModel.findOne({'token': req.headers['x-access-token']}) userModel.findOne({'token': req.headers['x-access-token']})
.lean() .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 */ /* fav */
exports.addFav = function(req, res) { exports.addFav = function(req, res) {
var tokenuser; var tokenuser;

+ 4
- 0
models/userModel.js

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

+ 9
- 0
server.js

@ -135,6 +135,15 @@ apiRoutes.route('/travels/declineJoin/:travelid')
.post(travelCtrl.declineJoin); .post(travelCtrl.declineJoin);
apiRoutes.route('/travels/acceptJoin/:travelid') apiRoutes.route('/travels/acceptJoin/:travelid')
.post(travelCtrl.acceptJoin); .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 //FINS AQUÏ COMPROVAT
apiRoutes.route('/travels/comment/:travelid') apiRoutes.route('/travels/comment/:travelid')

Loading…
Cancel
Save