1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-06-24 04:01:31 +08:00
2020-03-16 15:33:36 +08:00

137 lines
3.4 KiB
PHP

<?php
namespace App\Services\Frontend;
use App\Models\CommentVote as CommentVoteModel;
use App\Models\User as UserModel;
use App\Repos\CommentVote as CommentVoteRepo;
use App\Validators\UserDailyLimit as UserDailyLimitValidator;
class CommentVote extends Service
{
use CommentTrait;
public function agree($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();
$comment->agree_count += 1;
} else {
if ($commentVote->type == CommentVoteModel::TYPE_AGREE) {
$commentVote->type = CommentVoteModel::TYPE_NONE;
$comment->agree_count -= 1;
} elseif ($commentVote->type == CommentVoteModel::TYPE_OPPOSE) {
$commentVote->type = CommentVoteModel::TYPE_AGREE;
$comment->agree_count += 1;
$comment->oppose_count -= 1;
} elseif ($commentVote->type == CommentVoteModel::TYPE_NONE) {
$commentVote->type = CommentVoteModel::TYPE_AGREE;
$comment->agree_count += 1;
}
$commentVote->update();
}
$comment->update();
$this->incrUserDailyCommentVoteCount($user);
return $comment;
}
public function oppose($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_OPPOSE;
$commentVote->create();
$comment->oppose_count += 1;
} else {
if ($commentVote->type == CommentVoteModel::TYPE_AGREE) {
$commentVote->type = CommentVoteModel::TYPE_OPPOSE;
$comment->agree_count -= 1;
$comment->oppose_count += 1;
} elseif ($commentVote->type == CommentVoteModel::TYPE_OPPOSE) {
$commentVote->type = CommentVoteModel::TYPE_NONE;
$comment->oppose_count -= 1;
} elseif ($commentVote->type == CommentVoteModel::TYPE_NONE) {
$commentVote->type = CommentVoteModel::TYPE_OPPOSE;
$comment->oppose_count += 1;
}
$commentVote->update();
}
$comment->update();
$this->incrUserDailyCommentVoteCount($user);
return $comment;
}
protected function incrUserDailyCommentVoteCount(UserModel $user)
{
$this->eventsManager->fire('userDailyCounter:incrCommentVoteCount', $this, $user);
}
}