CategoryService.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace App\Services\Course;
  3. use App\Repositories\Criteria\Course\CategoryCriteria;
  4. use App\Repositories\Eloquent\Course\CategoryRepositoryEloquent;
  5. use App\Repositories\Presenters\Course\CategoryPresenter;
  6. use Illuminate\Http\Request;
  7. class CategoryService
  8. {
  9. /**
  10. * @var CategoryRepositoryEloquent
  11. */
  12. private $categoryRepository;
  13. /**
  14. * CategoryService constructor.
  15. *
  16. * @param CategoryRepositoryEloquent $categoryRepositoryEloquent
  17. */
  18. public function __construct(CategoryRepositoryEloquent $categoryRepositoryEloquent)
  19. {
  20. $this->categoryRepository = $categoryRepositoryEloquent;
  21. }
  22. /**
  23. * @param Request $request
  24. *
  25. * @return mixed
  26. * @throws \Prettus\Repository\Exceptions\RepositoryException
  27. */
  28. public function handleList(Request $request)
  29. {
  30. $this->categoryRepository->pushCriteria(new CategoryCriteria($request));
  31. $this->categoryRepository->setPresenter(CategoryPresenter::class);
  32. return $this->categoryRepository->searchCategoryByPage();
  33. }
  34. /**
  35. * @param $id
  36. *
  37. * @return \Illuminate\Database\Eloquent\Model
  38. */
  39. public function handleProfile($id)
  40. {
  41. $this->categoryRepository->setPresenter(CategoryPresenter::class);
  42. return $this->categoryRepository->searchCategoryBy($id);
  43. }
  44. /**
  45. * @param array $data
  46. *
  47. * @return mixed
  48. * @throws \Prettus\Validator\Exceptions\ValidatorException
  49. */
  50. public function handleStore($data)
  51. {
  52. $category = $this->categoryRepository->create($data);
  53. return $category;
  54. }
  55. /**
  56. * @param array $data
  57. *
  58. * @return mixed
  59. * @throws \Prettus\Validator\Exceptions\ValidatorException
  60. */
  61. public function handleUpdate($data)
  62. {
  63. $category = $this->categoryRepository->update($data, $data['id']);
  64. return $category;
  65. }
  66. /**
  67. * @param Request $request
  68. *
  69. * @return mixed
  70. * @throws \Prettus\Validator\Exceptions\ValidatorException
  71. */
  72. public function handleDelete($id)
  73. {
  74. return $this->categoryRepository->delete($id);
  75. }
  76. /**
  77. * 批量删除
  78. * @param $ids
  79. * @return mixed
  80. */
  81. public function handleBatchDelete($ids)
  82. {
  83. return $this->categoryRepository->whereIn('id', $ids)->delete();
  84. }
  85. /**
  86. * 选项
  87. * @param Request $request
  88. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Support\Collection|mixed
  89. * @throws \Prettus\Repository\Exceptions\RepositoryException
  90. */
  91. public function handleSelectOptions(Request $request)
  92. {
  93. $this->categoryRepository->pushCriteria(new CategoryCriteria($request));
  94. return $this->categoryRepository->all(['id', 'name', 'parent_id']);
  95. }
  96. /**
  97. * @param Request $request
  98. *
  99. * @return mixed
  100. * @throws \Prettus\Repository\Exceptions\RepositoryException
  101. */
  102. public function handleAll(Request $request, $f = ['*'])
  103. {
  104. $this->categoryRepository->pushCriteria(new CategoryCriteria($request));
  105. $this->categoryRepository->setPresenter(CategoryPresenter::class);
  106. return $this->categoryRepository->get($f);
  107. }
  108. }