1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-07-16 13:16:14 +08:00
2020-07-12 19:40:31 +08:00

72 lines
1.9 KiB
PHP

<?php
namespace App\Services\Frontend\Review;
use App\Models\Course as CourseModel;
use App\Models\Review as ReviewModel;
use App\Models\User as UserModel;
use App\Services\Frontend\CourseTrait;
use App\Services\Frontend\Service as FrontendService;
use App\Validators\Review as ReviewValidator;
use App\Validators\UserDailyLimit as UserDailyLimitValidator;
class ReviewCreate extends FrontendService
{
use CourseTrait;
public function handle()
{
$post = $this->request->getPost();
$course = $this->checkCourseCache($post['course_id']);
$user = $this->getLoginUser();
$validator = new UserDailyLimitValidator();
$validator->checkReviewLimit($user);
$validator = new ReviewValidator();
$data = [];
$data['content'] = $validator->checkContent($post['content']);
$data['rating1'] = $validator->checkRating($post['rating1']);
$data['rating2'] = $validator->checkRating($post['rating2']);
$data['rating3'] = $validator->checkRating($post['rating3']);
$validator->checkIfReviewed($course->id, $user->id);
$data['rating'] = $this->getAvgRating($data['rating1'], $data['rating2'], $data['rating3']);
$data['course_id'] = $course->id;
$data['user_id'] = $user->id;
$review = new ReviewModel();
$review->create($data);
$this->incrCourseReviewCount($course);
$this->incrUserDailyReviewCount($user);
return $review;
}
protected function getAvgRating($rating1, $rating2, $rating3)
{
return round(($rating1 + $rating2 + $rating3) / 3, 2);
}
protected function incrCourseReviewCount(CourseModel $course)
{
$this->eventsManager->fire('courseCounter:incrReviewCount', $this, $course);
}
protected function incrUserDailyReviewCount(UserModel $user)
{
$this->eventsManager->fire('userDailyCounter:incrReviewCount', $this, $user);
}
}