123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- <?php
- namespace App\Http\Controllers\Api\Course;
- use App\Http\Controllers\Controller;
- use App\Repositories\Criteria\Course\CourseCriteria;
- use App\Repositories\Enums\CourseTypeEnum;
- use App\Repositories\Enums\ModelStatusEnum;
- use App\Repositories\Enums\ResponseCodeEnum;
- use App\Repositories\Models\Course\Chapter;
- use App\Repositories\Models\Course\Course;
- use App\Repositories\Models\Course\UserRecord;
- use App\Repositories\Models\UserVideoWatchRecord;
- use Carbon\Carbon;
- use Illuminate\Http\Request;
- use Jiannei\Response\Laravel\Support\Facades\Response;
- use Overtrue\LaravelPinyin\Facades\Pinyin;
- use Prettus\Validator\Contracts\ValidatorInterface;
- use App\Contracts\Repositories\Course\CourseRepository;
- use App\Repositories\Validators\Course\CourseValidator;
- /**
- * Class CoursesController.
- *
- * @package namespace App\Http\Controllers;
- */
- class CourseController extends Controller
- {
- /**
- * @var CourseRepository
- */
- protected $repository;
- /**
- * @var CourseValidator
- */
- protected $validator;
- /**
- * CoursesController constructor.
- *
- * @param CourseRepository $repository
- * @param CourseValidator $validator
- */
- public function __construct(CourseRepository $repository, CourseValidator $validator)
- {
- $this->repository = $repository;
- $this->validator = $validator;
- }
- public function index(Request $request)
- {
- $this->repository->pushCriteria(CourseCriteria::class);
- $courses = $this->repository->where('status', ModelStatusEnum::OK)->where('published_at', '<', Carbon::now())->paginate($request->get('per_page', self::PAGE_NUM));
- $courses = $this->repository->parserResult($courses);
- return Response::success($courses);
- }
- /**
- * 详情
- * Author: Mead
- */
- public function show($id)
- {
- // $this->validate($request, [
- // 'id' => 'required'
- // ]);
- // $id = $request->get('id');
- $course = $this->repository->where('id', $id)->first();
- $directory = [];
- $data = Chapter::query()->where('course_id', $course->id)->where('status', ModelStatusEnum::OK)->orderByDesc('sort')->get();
- foreach ($data as $d) {
- $directory[] = [
- 'id' => $d['id'],
- 'title' => $d['title'],
- 'children' => $d->videos()->where('status', ModelStatusEnum::OK)->where('published_at', '<', Carbon::now())->orderByDesc('sort')->get(['id', 'title', 'duration', 'short_description'])->append(['duration_text', "progress"])
- ];
- }
- $comments = $course->comments()->with(['user'])->limit(self::PAGE_NUM)->orderByDesc('id')->get();
- $attaches = $course->attaches()->with(['path_resource'])->where('status', ModelStatusEnum::OK)->get(['name', 'id', 'path', 'download_times']);
- $subscribe = $course->subscribe()->where('user_id', login_user_id())->first();
- $course = $this->repository->parserResult($course);
- // $watch_record_video_id = UserVideoWatchRecord::query()->where('user_id', login_user_id())->where('course_id', $id)->orderByDesc('watched_at')->value('video_id');
- return Response::success(compact('course', 'directory', 'comments', 'attaches', 'subscribe'));
- }
- /**
- * 订阅课程
- * @param Request $request
- * Author: Mead
- */
- public function subscribe(Request $request)
- {
- $this->validate($request, ['course_id' => 'required|integer']);
- $course_id = $request->get('course_id');
- $user_id = login_user_id();
- if (UserRecord::query()->where('course_id', $course_id)->where('user_id', $user_id)->exists()) {
- return Response::success(null);
- }
- UserRecord::query()->create(['course_id' => $course_id, 'user_id' => $user_id]);
- Course::query()->where('id', $course_id)->increment('user_count');
- return Response::success(null);
- }
- public function lookCourses(Request $request)
- {
- $user_id = login_user_id();
- $course_ids = UserVideoWatchRecord::query()->where('user_id', $user_id)->distinct('course_id')->orderByDesc('id')->pluck('course_id')->toArray();
- if (count($course_ids) == 0) {
- return Response::success([]);
- }
- $courses = $this->repository->where('status', ModelStatusEnum::OK)->whereIn('id', $course_ids)->paginate($request->get('per_page', self::PAGE_NUM));
- $courses = $this->repository->parserResult($courses);
- return Response::success($courses);
- }
- }
|