/** * @param User $user * @param Model $model * @param $value * @return mixed */ public function vote(User $user, Model $model, $value) { $vote = $this->comment->getVote($model, $user->id); if (is_null($vote) && $value != 0) { $this->comment->vote($model, $user->id, $value); event(new CommentWasVoted($user, $model, $value)); } else { if ($value == 0 || $value == $vote->value) { $this->vote->delete($vote); event(new CommentWasUnvoted($user, $model, $value)); } else { $this->vote->update($vote, $value); event(new CommentWasVoted($user, $model, $value)); } } return $this->comment->updateVotes($model); }
/** * Vote post * * @param User $user * @param Post $post * @param $value * @return mixed */ public function vote(User $user, Post $post, $value) { $vote = $this->post->getVote($post, $user->id); if (is_null($vote) && $value) { $this->post->vote($post, $user->id, $value); event(new PostWasVoted($user, $post, $value)); } else { if ($value == 0 || $value == $vote->value) { $this->vote->delete($vote); event(new PostWasUnvoted($user, $post, $value)); } else { $this->vote->update($vote, $value); event(new PostWasVoted($user, $post, $value)); } } $post = $this->post->updateVotes($post); $owner = $post->user; $this->user->update($owner, ['points' => $this->post->userPoints($owner)]); return $post; }