123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- <?php
- namespace App\Repositories\Criteria\Exam;
- use App\Repositories\Enums\ModelStatusEnum;
- use Carbon\Carbon;
- use Illuminate\Http\Request;
- use Prettus\Repository\Contracts\CriteriaInterface;
- use Prettus\Repository\Contracts\RepositoryInterface;
- /**
- * Class PaperCriteria.
- *
- * @package namespace App\Repositories\Criteria\Exam;
- */
- class PaperCriteria implements CriteriaInterface
- {
- /**
- * Apply criteria in query repository
- *
- * @param string $model
- * @param RepositoryInterface $repository
- *
- * @return mixed
- */
- public function __construct(Request $request = null)
- {
- if (is_null($request)) {
- $this->request = \request();
- } else {
- $this->request = $request;
- }
- }
- /**
- * Apply criteria in query repository
- *
- * @param string $model
- * @param RepositoryInterface $repository
- *
- * @return mixed
- */
- public function apply($model, RepositoryInterface $repository)
- {
- if ($this->request->filled('title')) {
- $title = $this->request->get('title');
- $model = $model->where('title', 'like', "%{$title}%");
- }
- if ($this->request->filled('status')) {
- $status = $this->request->get('status');
- $model = $model->where('status', '=', $status);
- } else {
- $model = $model->where('status', '=', ModelStatusEnum::OK);
- }
- if ($this->request->filled('category_id')) {
- $category_id = $this->request->get('category_id', false);
- $model = $model->where('category_id', '=', $category_id);
- }
- if ($this->request->filled('course_id')) {
- $course_id = $this->request->get('course_id', false);
- $model = $model->where('course_id', '=', $course_id);
- }
- if ($this->request->filled('video_id')) {
- $video_id = $this->request->get('video_id', false);
- $model = $model->where('video_id', '=', $video_id);
- }
- if ($this->request->filled('is_published')) {
- $is_published = $this->request->get('is_published', false);
- if ($is_published) {
- $model = $model->where('published_at', '<', Carbon::now());
- }
- }
- if ($this->request->filled('is_me') && $this->request->get('is_me')) {
- $model = $model->where('user_id', '=', login_user_id());
- }
- $model = $model->orderByDesc('id');
- return $model;
- }
- }
|