public IActionResult CommentLike(int commentId)
        {
            var currentUser = _userRepository.Users.FirstOrDefault(u => u.UserName == User.Identity.Name);

            if (currentUser == null)
            {
                return(NotFound("User not found"));
            }

            var comment = _commentRepository.Comments.FirstOrDefault(c => c.Id == commentId);

            if (comment == null)
            {
                return(NotFound("Comment not found"));
            }

            var commentLike = _commentLikeRepository.CommentLikes
                              .FirstOrDefault(c => c.User.Equals(currentUser) && c.Comment.Equals(comment));

            if (commentLike == null) //like is not exist
            {
                _commentLikeRepository.SaveCommentLike(new CommentLike
                {
                    Comment = comment,
                    User    = currentUser
                });
            }
            else //unlike comment
            {
                _commentLikeRepository.DeleteCommentLike(commentLike);
            }

            return(Ok(new ItemViewData <Comment>
            {
                Likes = _commentLikeRepository.GetLikes(commentId),
                IsLiked = _commentLikeRepository.IsLiked(User.Identity.Name, commentId)
            }));
        }
Example #2
0
        public async Task <IActionResult> SaveCommentLike(CommentLikes commentLikes)
        {
            var isAdded = await _commentLikeRepository.SaveCommentLike(commentLikes);

            return(Json(isAdded));
        }