Ejemplo n.º 1
0
        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)
            }));
        }
Ejemplo n.º 2
0
        public IActionResult GetComments(int postId, int?parentId = null)
        {
            var user = _userRepository.Users.FirstOrDefault(u => u.UserName == User.Identity.Name);

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

            var post = _postRepository.Posts.FirstOrDefault(p => p.Id == postId);

            if (post == null)
            {
                return(NotFound("Post not found"));
            }

            Comment parentComment = null;

            if (parentId != null)
            {
                parentComment = _commentRepository.Comments.FirstOrDefault(c => c.Id == parentId);
                if (parentComment == null)
                {
                    return(NotFound("Parent comment not found"));
                }
            }

            return(Ok(_commentRepository.Comments.Include(c => c.User)
                      .Where(c => c.Post.Equals(post) && c.ParentComment == parentComment)
                      .Select(c => new ItemViewData <Comment>
            {
                Item = c,
                Likes = c.Likes.Count,
                IsLiked = _commentLikeRepository.IsLiked(User.Identity.Name, c.Id),
                Comments = c.SubComments.Count
            })));
        }