mirror of
https://gitee.com/koogua/course-tencent-cloud.git
synced 2025-06-23 03:50:56 +08:00
80 lines
1.8 KiB
PHP
80 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Validators;
|
|
|
|
use App\Exceptions\BadRequest as BadRequestException;
|
|
use App\Library\Validator\Common as CommonValidator;
|
|
use App\Repos\Course as CourseRepo;
|
|
use App\Repos\CourseUser as CourseUserRepo;
|
|
use App\Repos\User as UserRepo;
|
|
|
|
class CourseUser extends Validator
|
|
{
|
|
|
|
public function checkCourseUser($id)
|
|
{
|
|
$courseUserRepo = new CourseUserRepo();
|
|
|
|
$courseUser = $courseUserRepo->findById($id);
|
|
|
|
if (!$courseUser) {
|
|
throw new BadRequestException('course_user.not_found');
|
|
}
|
|
|
|
return $courseUser;
|
|
}
|
|
|
|
public function checkCourseId($courseId)
|
|
{
|
|
$value = $this->filter->sanitize($courseId, ['trim', 'int']);
|
|
|
|
$courseRepo = new CourseRepo();
|
|
|
|
$course = $courseRepo->findById($value);
|
|
|
|
if (!$course) {
|
|
throw new BadRequestException('course_user.course_not_found');
|
|
}
|
|
|
|
return $course->id;
|
|
}
|
|
|
|
public function checkUserId($userId)
|
|
{
|
|
$value = $this->filter->sanitize($userId, ['trim', 'int']);
|
|
|
|
$userRepo = new UserRepo();
|
|
|
|
$user = $userRepo->findById($value);
|
|
|
|
if (!$user) {
|
|
throw new BadRequestException('course_user.user_not_found');
|
|
}
|
|
|
|
return $user->id;
|
|
}
|
|
|
|
public function checkExpiryTime($expiryTime)
|
|
{
|
|
$value = $this->filter->sanitize($expiryTime, ['trim', 'string']);
|
|
|
|
if (!CommonValidator::date($value, 'Y-m-d H:i:s')) {
|
|
throw new BadRequestException('course_user.invalid_expiry_time');
|
|
}
|
|
|
|
return strtotime($value);
|
|
}
|
|
|
|
public function checkIfJoined($courseId, $userId)
|
|
{
|
|
$repo = new CourseUserRepo();
|
|
|
|
$courseUser = $repo->findCourseStudent($courseId, $userId);
|
|
|
|
if ($courseUser) {
|
|
throw new BadRequestException('course_user.has_joined_course');
|
|
}
|
|
}
|
|
|
|
}
|