public LikeDislikeModel LikeDislike(int id, bool likeordislike)
        {
            try
            {
                var likedislike = _dbcontext.LikeDislike.SingleOrDefault(c => c.PostId == id && c.UserId == UserId);
                int status;

                if (likedislike == null)
                {
                    _dbcontext.LikeDislike.Add(new LikeDislike()
                    {
                        PostId        = id,
                        UserId        = UserId,
                        LikeOrDislike = likeordislike
                    });
                    status = 1;
                }

                else
                {
                    bool flag = likedislike.LikeOrDislike;
                    if (flag == likeordislike)
                    {
                        _dbcontext.LikeDislike.Remove(likedislike);
                        status = 0;
                    }
                    else
                    {
                        likedislike.LikeOrDislike = likeordislike;
                        status = 2;
                    }
                }


                _dbcontext.SaveChanges();

                LikeDislikeModel likedislikemodel = new LikeDislikeModel()
                {
                    LikeCount    = _dbcontext.LikeDislike.Where(c => c.PostId == id && c.LikeOrDislike).Count(),
                    DislikeCount = _dbcontext.LikeDislike.Where(c => c.PostId == id && !c.LikeOrDislike).Count(),
                    Status       = status
                };


                return(likedislikemodel);
            }
            catch (Exception e)
            {
                return(null);
            }
        }
        public async Task <IActionResult> LikeDislike([FromBody] LikeDislikeModel model)
        {
            var user = await this.userManager.GetUserAsync(this.User);

            if (model.Type == "Like")
            {
                await this.postsService.LikeAsync(int.Parse(model.Id), user.Id);

                return(this.Json(true));
            }
            else
            {
                await this.postsService.DislikeAsync(int.Parse(model.Id), user.Id);

                return(this.Json(false));
            }

            return(this.Ok());
        }
        public List <LikeDislikeModel> GetVideoLikes(int id, bool withChilds)
        {
            var likes = new List <LikeDislikeModel>();

            using (SqlConnection DBConn = new SqlConnection(WebConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
            {
                SqlCommand sqlCmd = new SqlCommand("spGetVideoLikes", DBConn)
                {
                    CommandType = CommandType.StoredProcedure
                };

                sqlCmd.Parameters.Add("@VideoID", SqlDbType.Int).Value = id;

                DBConn.Open();

                using (var DB = sqlCmd.ExecuteReader())
                {
                    var like = new LikeDislikeModel();

                    while (DB.Read())
                    {
                        like = new LikeDislikeModel()
                        {
                            ID      = (int)DB["Id"],
                            VideoID = (int)DB["VideoID"],
                            UserID  = (long)DB["UserID"],
                            Like    = (bool)DB["Like"]
                        };

                        if (withChilds)
                        {
                            like.User = _userRepo.GetUser(like.UserID);
                        }

                        likes.Add(like);
                    }
                }
            }

            return(likes);
        }
示例#4
0
        // [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);
        }