1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-06-24 12:05:39 +08:00
2021-04-20 21:16:48 +08:00

96 lines
2.2 KiB
PHP

<?php
namespace App\Services\Logic\Chapter;
use App\Models\Chapter as ChapterModel;
use App\Models\ChapterLike as ChapterLikeModel;
use App\Models\User as UserModel;
use App\Repos\ChapterLike as ChapterLikeRepo;
use App\Services\Logic\ChapterTrait;
use App\Services\Logic\Service as LogicService;
use App\Validators\UserLimit as UserLimitValidator;
class ChapterLike extends LogicService
{
use ChapterTrait;
public function handle($id)
{
$chapter = $this->checkChapter($id);
$user = $this->getLoginUser();
$validator = new UserLimitValidator();
$validator->checkDailyChapterLikeLimit($user);
$likeRepo = new ChapterLikeRepo();
$chapterLike = $likeRepo->findChapterLike($chapter->id, $user->id);
if (!$chapterLike) {
$action = 'do';
$chapterLike = new ChapterLikeModel();
$chapterLike->chapter_id = $chapter->id;
$chapterLike->user_id = $user->id;
$chapterLike->create();
$this->incrChapterLikeCount($chapter);
} else {
$action = 'undo';
$chapterLike->delete();
$this->decrChapterLikeCount($chapter);
}
$this->incrUserDailyChapterLikeCount($user);
return [
'action' => $action,
'count' => $chapter->like_count,
];
}
protected function incrChapterLikeCount(ChapterModel $chapter)
{
$chapter->like_count += 1;
$chapter->update();
$parent = $this->checkChapter($chapter->parent_id);
$parent->like_count += 1;
$parent->update();
}
protected function decrChapterLikeCount(ChapterModel $chapter)
{
if ($chapter->like_count > 0) {
$chapter->like_count -= 1;
$chapter->update();
}
$parent = $this->checkChapter($chapter->parent_id);
if ($parent->like_count > 0) {
$parent->like_count -= 1;
$parent->update();
}
}
protected function incrUserDailyChapterLikeCount(UserModel $user)
{
$this->eventsManager->fire('UserDailyCounter:incrChapterLikeCount', $this, $user);
}
}