Exemple #1
0
        protected void like_Click(object sender, EventArgs e)
        {
            LikesManager       likesManager       = new LikesManager();
            CalculationManager calculationManager = new CalculationManager();
            int UserId    = int.Parse(((Session["user"] as User).Id).ToString());
            int ProjectId = int.Parse(Request.QueryString["Id"]);

            Model.Likes likes = likesManager.GetModelByUserIdAndProjectId(UserId, ProjectId);
            if (likes == null)
            {
                if (likesManager.Insert(UserId, ProjectId) > 0 && calculationManager.LikeCountAddition(ProjectId) > 0)
                {
                    Response.Write("<script>alert('关注成功!!');loaction.href='Projectinfo.aspx';</script>");
                    this.like.Text = "取消关注";
                    Response.Write("<script> window.location.href = document.URL; </script>");
                }
                else
                {
                    Response.Write("<script>alert('关注失败!!');</script>");
                }
            }
            else
            {
                if (likesManager.Delete(UserId, ProjectId) > 0 && calculationManager.LikeCountSubtraction(ProjectId) > 0)
                {
                    Response.Write("<script>alert('已取消关注!!');</script>");
                    this.like.Text = "关注";
                    Response.Write("<script> window.location.href = document.URL; </script>");
                }
                else
                {
                    Response.Write("<script>alert('取消失败!!');</script>");
                }
            }
        }
        public ActionResult SetLikeState(int photoid, bool liked)
        {
            int res = 0;

            Liked like = likedManager.Find(x => x.Photo.Id == photoid && x.LikedUser.Id == CurrentSession.User.Id);

            Photo photo = photomanager.Find(x => x.Id == photoid);

            if (like != null && liked == false)
            {
                res = likedManager.Delete(like);
            }

            else if (like == null && liked == true)
            {
                res = likedManager.Insert(new Liked()
                {
                    LikedUser = CurrentSession.User,
                    Photo     = photo
                });
            }

            if (res > 0)
            {
                if (liked)
                {
                    photo.LikeCount++;
                }

                else
                {
                    photo.LikeCount--;
                }

                res = photomanager.Update(photo);

                return(Json(new { hasError = false, errormassege = string.Empty, result = photo.LikeCount }));
            }

            return(Json(new { hasError = true, errormassege = "Beğenme işlemi gerçekleştrilemedi.", result = photo.LikeCount }));
        }