Esempio n. 1
0
        public JsonResult VotePost()
        {
            if (Request.Form["post_id"] != null)
            {
                if (Int32.TryParse(Request.Form["value"], out var value))
                {
                    value = value > 0 ? 1 : -1;
                    var postId             = new Guid(Request.Form["post_id"]);
                    var currentUserProfile = _userProfileService.GetUserProfileByUserId(new Guid(User.Identity.GetUserId()));
                    var post         = _postService.GetPost(postId);
                    var existingLike = _likeService.GetLike(postId, currentUserProfile.Id);
                    if (existingLike == null)
                    {
                        _likeService.AddLike(currentUserProfile, value, post);
                        return(Json(new { Message = "vote_registered" }));
                    }

                    if (existingLike.Value != value)
                    {
                        existingLike.Value = value;
                        _likeService.ChangeLikeValue(existingLike.Id, value);
                        return(Json(new { Message = "vote_registered" }));
                    }

                    return(Json(new { Message = "already_voted" }));
                }
                else
                {
                    return(Json(new { Message = "invalid_parameter" }));
                }
            }
            else
            {
                return(Json(new { Message = "invalid_parameter" }));
            }
        }