1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-06-24 04:01:31 +08:00
2020-05-16 19:49:58 +08:00

74 lines
1.8 KiB
PHP

<?php
namespace App\Services\Frontend\Comment;
use App\Models\CommentVote as CommentVoteModel;
use App\Repos\CommentVote as CommentVoteRepo;
use App\Services\Frontend\CommentTrait;
use App\Services\Frontend\Service as FrontendService;
use App\Validators\UserDailyLimit as UserDailyLimitValidator;
class AgreeVote extends FrontendService
{
use CommentTrait, VoteTrait;
public function handle($id)
{
$comment = $this->checkComment($id);
$user = $this->getLoginUser();
$validator = new UserDailyLimitValidator();
$validator->checkCommentVoteLimit($user);
$commentVoteRepo = new CommentVoteRepo();
$commentVote = $commentVoteRepo->findCommentVote($comment->id, $user->id);
if (!$commentVote) {
$commentVote = new CommentVoteModel();
$commentVote->comment_id = $comment->id;
$commentVote->user_id = $user->id;
$commentVote->type = CommentVoteModel::TYPE_AGREE;
$commentVote->create();
$this->incrAgreeCount($comment);
} else {
if ($commentVote->type == CommentVoteModel::TYPE_AGREE) {
$commentVote->type = CommentVoteModel::TYPE_NONE;
$this->decrAgreeCount($comment);
} elseif ($commentVote->type == CommentVoteModel::TYPE_OPPOSE) {
$commentVote->type = CommentVoteModel::TYPE_AGREE;
$this->incrAgreeCount($comment);
$this->decrOpposeCount($comment);
} elseif ($commentVote->type == CommentVoteModel::TYPE_NONE) {
$commentVote->type = CommentVoteModel::TYPE_AGREE;
$this->incrAgreeCount($comment);
}
$commentVote->update();
}
$this->incrUserDailyCommentVoteCount($user);
return $comment;
}
}