From b00f4ea7d7df047cf8f4dd81ecfae5c799453e32 Mon Sep 17 00:00:00 2001 From: arnaucode Date: Thu, 2 Feb 2017 15:33:45 +0100 Subject: [PATCH] user likes system implemented --- controllers/userController.js | 98 +++++++++++++++++++++++++++++++++++ models/userModel.js | 4 ++ server.js | 9 ++++ 3 files changed, 111 insertions(+) diff --git a/controllers/userController.js b/controllers/userController.js index a5bea96..9427aa8 100644 --- a/controllers/userController.js +++ b/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; diff --git a/models/userModel.js b/models/userModel.js index 4398c0c..8af7407 100644 --- a/models/userModel.js +++ b/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, diff --git a/server.js b/server.js index ad70680..17d1517 100755 --- a/server.js +++ b/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')