public function likeActivityComment($id) { $comment = ActivityComment::findOrFail($id); if (Auth::user()->id != $comment->user->id && !Auth::user()->isFriendWith($comment->user)) { return new UnauthorizedException(); } // On check que l'utilisateur n'a pas liké le commentaire if (Auth::user()->hasLikedActivityComment($comment)) { $likeToRemove = $this->repository->getLikeByActivityCommentId(Auth::user(), $comment); $removeLike = Like::findOrFail($likeToRemove->id); $removeLike->delete(); $type = 'remove'; } else { $comment->likes()->create(['user_id' => Auth::user()->id]); $type = 'add'; } return new JsonResponse(['status' => 'success', 'type' => $type]); }
/** * Retourne l'id d'un "like" en fonction du commentaire * * @param User $user * @param ActivityComment $comment * @return mixed */ public function getLikeByActivityCommentId(User $user, ActivityComment $comment) { return Like::where('user_id', $user->id)->where('like_id', $comment->id)->where('like_type', get_class($comment))->first(); }