CommentController.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace App\Http\Controllers\Api\Course;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Http\Request;
  5. use Jiannei\Response\Laravel\Support\Facades\Response;
  6. use Prettus\Validator\Contracts\ValidatorInterface;
  7. use App\Contracts\Repositories\Course\CommentRepository;
  8. use App\Repositories\Validators\Course\CommentValidator;
  9. /**
  10. * Class CourseCommentsController.
  11. *
  12. * @package namespace App\Http\Controllers;
  13. */
  14. class CommentController extends Controller
  15. {
  16. /**
  17. * @var CommentRepository
  18. */
  19. protected $repository;
  20. /**
  21. * @var CommentValidator
  22. */
  23. protected $validator;
  24. /**
  25. * CourseCommentsController constructor.
  26. *
  27. * @param CommentRepository $repository
  28. * @param CommentValidator $validator
  29. */
  30. public function __construct(CommentRepository $repository, CommentValidator $validator)
  31. {
  32. $this->repository = $repository;
  33. $this->validator = $validator;
  34. }
  35. public function store(Request $request)
  36. {
  37. $this->validate($request, $this->validator->getRules(ValidatorInterface::RULE_CREATE));
  38. try {
  39. $data = $request->only(array_keys($this->validator->getRules(ValidatorInterface::RULE_CREATE)));
  40. $data['user_id'] = login_user_id();
  41. $comment = $this->repository->create($data);
  42. return Response::success($comment);
  43. } catch (\Exception $e) {
  44. return $this->errorStore($e);
  45. }
  46. }
  47. public function videoComments($video_id)
  48. {
  49. $comments = $this->repository->where('course_video_id', $video_id)->paginate(request('per_page', self::PAGE_NUM));
  50. return Response::success($this->repository->parserResult($comments));
  51. }
  52. public function courseComments($course_id)
  53. {
  54. $comments = $this->repository->where('course_id', $course_id)->paginate(request('per_page', self::PAGE_NUM));
  55. return Response::success($this->repository->parserResult($comments));
  56. }
  57. }