public bool checkIfUserSelectSameAction(int userId, bool isLike) { LikeDislikeBoat like = db.LikeDislikeBoat.Where(x => x.isLike == isLike).Where(x => x.userid == userId).FirstOrDefault(); if (like != null) { return(false); } return(true); }
//public bool Like(DAL.LikeDislikeBoat model) //{ // LikeDislikeBoat likeDislike = db.LikeDislikeBoats.FirstOrDefault(x => x.boatId == model.boatId && x.userid == model.userid); // if (likeDislike == null) // { // db.LikeDislikeBoats.Add(model); // db.SaveChanges(); // return true; // } // else // { // if (likeDislike.action == (int)PostAction.Like) // { // // user has previously Liked the post, remove the Like // db.LikeDislikeBoats.Remove(model); // db.SaveChanges(); // return true; // } // return false; // } //} //public bool DisLike(DAL.LikeDislikeBoat model) //{ // LikeDislikeBoat likeDislike = db.LikeDislikeBoats.FirstOrDefault(x => x.boatId == model.boatId && x.userid == model.userid); // if (likeDislike == null) // { // db.LikeDislikeBoats.Add(model); // db.SaveChanges(); // return true; // } // else // { // if (likeDislike.action == (int)PostAction.Like) // { // // user has previously Liked the post, remove the Like // db.LikeDislikeBoats.Remove(model); // db.SaveChanges(); // return true; // } // return false; // } //} public string Like(int boatId, bool status, int userId) { var boat = db.Boat.FirstOrDefault(x => x.id == boatId); var toggle = false; LikeDislikeBoat like = db.LikeDislikeBoat.FirstOrDefault(x => x.boatId == boatId && x.userid == userId); // here we are checking whether user have done like or dislike if (like == null) { like = new LikeDislikeBoat(); like.userid = userId; like.isLike = status; like.boatId = boatId; if (status) { if (boat.likeCount == null) // if no one has done like or dislike and first time any one doing like and dislike then assigning 1 and 0 { boat.likeCount = boat.likeCount < 1 ? 1 : boat.likeCount; boat.disLikeCount = boat.disLikeCount < 0 ? 1 : boat.disLikeCount; } else { boat.likeCount = boat.likeCount + 1; } } else { if (boat.disLikeCount == null) { boat.disLikeCount = boat.disLikeCount < 1 ? 1 : boat.disLikeCount; boat.likeCount = boat.likeCount < 1 ? 1 : boat.likeCount; } else { boat.disLikeCount = boat.disLikeCount + 1; } } db.LikeDislikeBoat.Add(like); } else { toggle = true; } if (toggle) { like.userid = userId; like.isLike = status; like.boatId = boatId; if (status) { // if user has click like button then need to increase +1 in like and -1 in Dislike boat.likeCount = boat.likeCount + 1; if (boat.disLikeCount == 0 || boat.disLikeCount < 0) { boat.disLikeCount = 0; } else { boat.disLikeCount = boat.disLikeCount - 1; } } else { // if user has click dislike then need to increase +1 in dislike and -1 in like boat.disLikeCount = boat.disLikeCount + 1; if (boat.likeCount == 0 || boat.likeCount < 0) { boat.likeCount = 0; } else { boat.likeCount = boat.likeCount - 1; } } } db.SaveChanges(); return(boat.likeCount + "/" + boat.disLikeCount); }
// [Route("api/BoatApi/{boatId:int}")] //[ActionName("likeboat")] public string Post(LikeDislikeModel likeDislikeModel) { //if (!SessionManager.userIsLoggedIn()) // return new HttpStatusCodeResult(403).ToString() ; var boat = db.Boat.FirstOrDefault(x => x.id == likeDislikeModel.boatId); var toggle = false; LikeDislikeBoat like = db.LikeDislikeBoat.FirstOrDefault(x => x.boatId == likeDislikeModel.boatId && x.userid == likeDislikeModel.userId); // here we are checking whether user have done like or dislike if (like == null) { like = new LikeDislikeBoat(); like.userid = likeDislikeModel.userId; like.isLike = likeDislikeModel.status; like.boatId = likeDislikeModel.boatId; if (likeDislikeModel.status) { if (boat.likeCount == null) // if no one has done like or dislike and first time any one doing like and dislike then assigning 1 and 0 { boat.likeCount = boat.likeCount < 1 ? 1 : boat.likeCount; boat.disLikeCount = boat.disLikeCount < 0 ? 1 : boat.disLikeCount; } else { boat.likeCount = boat.likeCount + 1; } } else { if (boat.disLikeCount == null) { boat.disLikeCount = boat.disLikeCount < 1 ? 1 : boat.disLikeCount; boat.likeCount = boat.likeCount < 1 ? 1 : boat.likeCount; } else { boat.disLikeCount = boat.disLikeCount + 1; } } db.LikeDislikeBoat.Add(like); } else { toggle = true; } if (toggle) { like.userid = likeDislikeModel.userId; like.isLike = likeDislikeModel.status; like.boatId = likeDislikeModel.boatId; if (likeDislikeModel.status) { // if user has click like button then need to increase +1 in like and -1 in Dislike boat.likeCount = boat.likeCount + 1; if (boat.disLikeCount == 0 || boat.disLikeCount < 0) { boat.disLikeCount = 0; } else { boat.disLikeCount = boat.disLikeCount - 1; } } else { // if user has click dislike then need to increase +1 in dislike and -1 in like boat.disLikeCount = boat.disLikeCount + 1; if (boat.likeCount == 0 || boat.likeCount < 0) { boat.likeCount = 0; } else { boat.likeCount = boat.likeCount - 1; } } } db.SaveChanges(); return(boat.likeCount + "/" + boat.disLikeCount); }