PaperCriteria.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace App\Repositories\Criteria\Exam;
  3. use App\Repositories\Enums\ModelStatusEnum;
  4. use Carbon\Carbon;
  5. use Illuminate\Http\Request;
  6. use Prettus\Repository\Contracts\CriteriaInterface;
  7. use Prettus\Repository\Contracts\RepositoryInterface;
  8. /**
  9. * Class PaperCriteria.
  10. *
  11. * @package namespace App\Repositories\Criteria\Exam;
  12. */
  13. class PaperCriteria implements CriteriaInterface
  14. {
  15. /**
  16. * Apply criteria in query repository
  17. *
  18. * @param string $model
  19. * @param RepositoryInterface $repository
  20. *
  21. * @return mixed
  22. */
  23. public function __construct(Request $request = null)
  24. {
  25. if (is_null($request)) {
  26. $this->request = \request();
  27. } else {
  28. $this->request = $request;
  29. }
  30. }
  31. /**
  32. * Apply criteria in query repository
  33. *
  34. * @param string $model
  35. * @param RepositoryInterface $repository
  36. *
  37. * @return mixed
  38. */
  39. public function apply($model, RepositoryInterface $repository)
  40. {
  41. if ($this->request->filled('title')) {
  42. $title = $this->request->get('title');
  43. $model = $model->where('title', 'like', "%{$title}%");
  44. }
  45. if ($this->request->filled('status')) {
  46. $status = $this->request->get('status');
  47. $model = $model->where('status', '=', $status);
  48. } else {
  49. $model = $model->where('status', '=', ModelStatusEnum::OK);
  50. }
  51. if ($this->request->filled('category_id')) {
  52. $category_id = $this->request->get('category_id', false);
  53. $model = $model->where('category_id', '=', $category_id);
  54. }
  55. if ($this->request->filled('course_id')) {
  56. $course_id = $this->request->get('course_id', false);
  57. $model = $model->where('course_id', '=', $course_id);
  58. }
  59. if ($this->request->filled('video_id')) {
  60. $video_id = $this->request->get('video_id', false);
  61. $model = $model->where('video_id', '=', $video_id);
  62. }
  63. if ($this->request->filled('is_published')) {
  64. $is_published = $this->request->get('is_published', false);
  65. if ($is_published) {
  66. $model = $model->where('published_at', '<', Carbon::now());
  67. }
  68. }
  69. if ($this->request->filled('is_me') && $this->request->get('is_me')) {
  70. $model = $model->where('user_id', '=', login_user_id());
  71. }
  72. $model = $model->orderByDesc('id');
  73. return $model;
  74. }
  75. }