1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-06-23 20:00:27 +08:00
2020-05-09 22:08:19 +08:00

72 lines
1.7 KiB
PHP

<?php
namespace App\Services\Frontend\Comment;
use App\Models\Comment as CommentModel;
use App\Models\CommentVote as CommentVoteModel;
use App\Models\User as UserModel;
use App\Repos\CommentVote as CommentVoteRepo;
use App\Repos\User as UserRepo;
use App\Services\Frontend\CommentTrait;
use App\Services\Frontend\Service;
class CommentInfo extends Service
{
use CommentTrait;
public function handle($id)
{
$comment = $this->checkComment($id);
$user = $this->getCurrentUser();
return $this->handleComment($comment, $user);
}
protected function handleComment(CommentModel $comment, UserModel $user)
{
$result = [
'id' => $comment->id,
'content' => $comment->content,
'mentions' => $comment->mentions,
'agree_count' => $comment->agree_count,
'oppose_count' => $comment->oppose_count,
'create_time' => $comment->create_time,
'update_time' => $comment->update_time,
];
$me = [
'agreed' => 0,
'opposed' => 0,
];
if ($user->id > 0) {
$voteRepo = new CommentVoteRepo();
$vote = $voteRepo->findCommentVote($comment->id, $user->id);
if ($vote) {
$me['agreed'] = $vote->type == CommentVoteModel::TYPE_AGREE ? 1 : 0;
$me['opposed'] = $vote->type == CommentVoteModel::TYPE_OPPOSE ? 1 : 0;
}
}
$userRepo = new UserRepo();
$owner = $userRepo->findById($comment->user_id);
$result['owner'] = [
'id' => $owner->id,
'name' => $owner->name,
'avatar' => $owner->avatar,
];
$result['me'] = $me;
return $result;
}
}