NewsService.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace App\Services\Info;
  3. use App\Contracts\Repositories\Info\NewsRepository;
  4. use App\Repositories\Criteria\Info\NewsCriteria;
  5. use App\Repositories\Eloquent\Info\NewsRepositoryEloquent;
  6. use App\Repositories\Presenters\Info\NewsPresenter;
  7. use Illuminate\Http\Request;
  8. class NewsService
  9. {
  10. /**
  11. * @var NewsRepositoryEloquent
  12. */
  13. private $newsRepository;
  14. /**
  15. * NewsService constructor.
  16. *
  17. * @param NewsRepositoryEloquent $newsRepositoryEloquent
  18. */
  19. public function __construct(NewsRepositoryEloquent $newsRepositoryEloquent)
  20. {
  21. $this->newsRepository = $newsRepositoryEloquent;
  22. }
  23. /**
  24. * @param Request $request
  25. *
  26. * @return mixed
  27. * @throws \Prettus\Repository\Exceptions\RepositoryException
  28. */
  29. public function handleList(Request $request)
  30. {
  31. $this->newsRepository->pushCriteria(new NewsCriteria($request));
  32. $this->newsRepository->setPresenter(NewsPresenter::class);
  33. return $this->newsRepository->searchNewssByPage();
  34. }
  35. /**
  36. * @param $id
  37. *
  38. * @return \Illuminate\Database\Eloquent\Model
  39. */
  40. public function handleProfile($id)
  41. {
  42. $this->newsRepository->setPresenter(NewsPresenter::class);
  43. return $this->newsRepository->searchNewsBy($id);
  44. }
  45. /**
  46. * @param Request $request
  47. *
  48. * @return mixed
  49. * @throws \Prettus\Validator\Exceptions\ValidatorException
  50. */
  51. public function handleStore(Request $request)
  52. {
  53. $data = $request->all();
  54. $data['admin_id'] = login_admin_id();
  55. $news = $this->newsRepository->create($data);
  56. return $news;
  57. }
  58. /**
  59. * @param Request $request
  60. *
  61. * @return mixed
  62. * @throws \Prettus\Validator\Exceptions\ValidatorException
  63. */
  64. public function handleUpdate(Request $request)
  65. {
  66. $news = $this->newsRepository->update($request->all(), $request->get('id'));
  67. return $news;
  68. }
  69. /**
  70. * @param Request $request
  71. *
  72. * @return mixed
  73. * @throws \Prettus\Validator\Exceptions\ValidatorException
  74. */
  75. public function handleDelete($id)
  76. {
  77. return $this->newsRepository->delete($id);
  78. }
  79. }