CourseController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace App\Http\Controllers\Api\Course;
  3. use App\Http\Controllers\Controller;
  4. use App\Repositories\Criteria\Course\CourseCriteria;
  5. use App\Repositories\Enums\CourseTypeEnum;
  6. use App\Repositories\Enums\ModelStatusEnum;
  7. use App\Repositories\Enums\ResponseCodeEnum;
  8. use App\Repositories\Models\Course\Chapter;
  9. use App\Repositories\Models\Course\Course;
  10. use App\Repositories\Models\Course\UserRecord;
  11. use App\Repositories\Models\UserVideoWatchRecord;
  12. use Carbon\Carbon;
  13. use Illuminate\Http\Request;
  14. use Jiannei\Response\Laravel\Support\Facades\Response;
  15. use Overtrue\LaravelPinyin\Facades\Pinyin;
  16. use Prettus\Validator\Contracts\ValidatorInterface;
  17. use App\Contracts\Repositories\Course\CourseRepository;
  18. use App\Repositories\Validators\Course\CourseValidator;
  19. /**
  20. * Class CoursesController.
  21. *
  22. * @package namespace App\Http\Controllers;
  23. */
  24. class CourseController extends Controller
  25. {
  26. /**
  27. * @var CourseRepository
  28. */
  29. protected $repository;
  30. /**
  31. * @var CourseValidator
  32. */
  33. protected $validator;
  34. /**
  35. * CoursesController constructor.
  36. *
  37. * @param CourseRepository $repository
  38. * @param CourseValidator $validator
  39. */
  40. public function __construct(CourseRepository $repository, CourseValidator $validator)
  41. {
  42. $this->repository = $repository;
  43. $this->validator = $validator;
  44. }
  45. public function index(Request $request)
  46. {
  47. $this->repository->pushCriteria(CourseCriteria::class);
  48. $courses = $this->repository->where('status', ModelStatusEnum::OK)->where('published_at', '<', Carbon::now())->paginate($request->get('per_page', self::PAGE_NUM));
  49. $courses = $this->repository->parserResult($courses);
  50. return Response::success($courses);
  51. }
  52. /**
  53. * 详情
  54. * Author: Mead
  55. */
  56. public function show($id)
  57. {
  58. // $this->validate($request, [
  59. // 'id' => 'required'
  60. // ]);
  61. // $id = $request->get('id');
  62. $course = $this->repository->where('id', $id)->first();
  63. $directory = [];
  64. $data = Chapter::query()->where('course_id', $course->id)->where('status', ModelStatusEnum::OK)->orderByDesc('sort')->get();
  65. foreach ($data as $d) {
  66. $directory[] = [
  67. 'id' => $d['id'],
  68. 'title' => $d['title'],
  69. 'children' => $d->videos()->where('status', ModelStatusEnum::OK)->where('published_at', '<', Carbon::now())->orderByDesc('sort')->get(['id', 'title', 'duration', 'short_description'])->append(['duration_text', "progress"])
  70. ];
  71. }
  72. $comments = $course->comments()->with(['user'])->limit(self::PAGE_NUM)->orderByDesc('id')->get();
  73. $attaches = $course->attaches()->with(['path_resource'])->where('status', ModelStatusEnum::OK)->get(['name', 'id', 'path', 'download_times']);
  74. $subscribe = $course->subscribe()->where('user_id', login_user_id())->first();
  75. $course = $this->repository->parserResult($course);
  76. // $watch_record_video_id = UserVideoWatchRecord::query()->where('user_id', login_user_id())->where('course_id', $id)->orderByDesc('watched_at')->value('video_id');
  77. return Response::success(compact('course', 'directory', 'comments', 'attaches', 'subscribe'));
  78. }
  79. /**
  80. * 订阅课程
  81. * @param Request $request
  82. * Author: Mead
  83. */
  84. public function subscribe(Request $request)
  85. {
  86. $this->validate($request, ['course_id' => 'required|integer']);
  87. $course_id = $request->get('course_id');
  88. $user_id = login_user_id();
  89. if (UserRecord::query()->where('course_id', $course_id)->where('user_id', $user_id)->exists()) {
  90. return Response::success(null);
  91. }
  92. UserRecord::query()->create(['course_id' => $course_id, 'user_id' => $user_id]);
  93. Course::query()->where('id', $course_id)->increment('user_count');
  94. return Response::success(null);
  95. }
  96. public function lookCourses(Request $request)
  97. {
  98. $user_id = login_user_id();
  99. $course_ids = UserVideoWatchRecord::query()->where('user_id', $user_id)->distinct('course_id')->orderByDesc('id')->pluck('course_id')->toArray();
  100. if (count($course_ids) == 0) {
  101. return Response::success([]);
  102. }
  103. $courses = $this->repository->where('status', ModelStatusEnum::OK)->whereIn('id', $course_ids)->paginate($request->get('per_page', self::PAGE_NUM));
  104. $courses = $this->repository->parserResult($courses);
  105. return Response::success($courses);
  106. }
  107. }