CategoryController.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace App\Http\Controllers\Api\Course;
  3. use App\Http\Controllers\Controller;
  4. use App\Repositories\Enums\ModelStatusEnum;
  5. use App\Repositories\Enums\ResponseCodeEnum;
  6. use Illuminate\Http\Request;
  7. use Jiannei\Response\Laravel\Support\Facades\Response;
  8. use Prettus\Validator\Contracts\ValidatorInterface;
  9. use App\Contracts\Repositories\Course\CategoryRepository;
  10. use App\Repositories\Validators\Course\CategoryValidator;
  11. /**
  12. * Class CourseCategoriesController.
  13. *
  14. * @package namespace App\Http\Controllers;
  15. */
  16. class CategoryController extends Controller
  17. {
  18. /**
  19. * @var CategoryRepository
  20. */
  21. protected $repository;
  22. /**
  23. * @var CategoryValidator
  24. */
  25. protected $validator;
  26. /**
  27. * CourseCategoriesController constructor.
  28. *
  29. * @param CategoryRepository $repository
  30. * @param CategoryValidator $validator
  31. */
  32. public function __construct(CategoryRepository $repository, CategoryValidator $validator)
  33. {
  34. $this->repository = $repository;
  35. $this->validator = $validator;
  36. }
  37. public function tree()
  38. {
  39. $departments = $this->repository->orderByDesc('sort')->get()->append('parent_name')->toArray();
  40. return Response::success(toTree($departments));
  41. }
  42. public function index(Request $request)
  43. {
  44. if ($id = $request->get('id', false)) {
  45. $category = $this->repository->skipPresenter()->where('status', ModelStatusEnum::OK)->where('parent_id', $id)->get(['id', 'name']);
  46. } else {
  47. $category = $this->repository->skipPresenter()->where('status', ModelStatusEnum::OK)->where('parent_id', 0)->get(['id', 'name']);
  48. }
  49. return Response::success($category);
  50. }
  51. }