Beispiel #1
0
        public async Task <IActionResult> AddLike(int id)
        {
            var user = await _userManager.GetUserAsync(HttpContext.User);

            var comm = await db.Comments.FirstOrDefaultAsync(c => c.Id == id);

            Likes like = new Likes
            {
                CommentsId = comm.Id,
                User       = user,
                Comments   = comm
            };

            var find = likesRepository.FindLike(user, comm);

            if (find == null)
            {
                likesRepository.AddLike(like, comm);
            }
            else
            {
                likesRepository.RemoveLike(find, comm);
            }

            return(RedirectToAction("CommentsList", new { id = comm.CarLotId }));
        }
        public async Task <bool> LikeChanged(int id, [FromBody] bool isLiked)
        {
            if (ModelState.IsValid)
            {
                var isTokenValid = await _tokenValidator.IsTokenValid(Request.Headers, HttpContext);

                if (isTokenValid)
                {
                    var user = await GetCurrentUser();

                    var like = await _likesRepository.GetLikeByTopicAndUser(id, user.Id);

                    var topic = await _topicRepository.GetTopicById(id);

                    if (like != null)
                    {
                        topic.Rating = isLiked ?
                                       like.IsLiked ? topic.Rating : topic.Rating + 1 :
                                       like.IsLiked ? topic.Rating - 1 : topic.Rating;
                        await _topicRepository.UpdateTopic(topic.Id, topic);

                        like.IsLiked = isLiked;

                        await _likesRepository.UpdateLike(like.Id, like);

                        return(like.IsLiked);
                    }
                    else
                    {
                        var newLike = new Like()
                        {
                            IsLiked = true,
                            TopicId = id,
                            UserId  = user.Id
                        };
                        await _likesRepository.AddLike(newLike);

                        topic.Rating++;
                        await _topicRepository.UpdateTopic(topic.Id, topic);

                        return(true);
                    }
                }
            }

            return(false);
        }
 public void LikePost([FromQuery] int postId, int userId)
 {
     likesRepo.AddLike(postId, userId);
 }