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); }