Exemplo n.º 1
0
        public IActionResult Like(int postId)
        {
            if (postId > 0)
            {
                PostModel post = _postService.GetById(postId, "");

                if (post != null)
                {
                    LikesModel alreadyLiked = _likesService.AlreadyLikedData(postId, Convert.ToInt32(User.FindFirst("Id").Value));

                    if (alreadyLiked == null)
                    {
                        LikesModel like = new LikesModel
                        {
                            PostId           = postId,
                            LikedById        = Convert.ToInt32(User.FindFirst("Id").Value),
                            CreationDate     = DateTime.Now,
                            ModificationDate = DateTime.Now,
                        };
                        _likesService.Add(like);
                        return(Ok("add"));
                    }

                    else
                    {
                        _likesService.Remove(alreadyLiked.Id);
                        return(Ok("remove"));
                    }
                }
                return(BadRequest());
            }
            return(BadRequest());
        }