CommentService.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace App\Services\Course;
  3. use App\Contracts\Repositories\Course\CommentRepository;
  4. use App\Repositories\Criteria\Course\CommentCriteria;
  5. use App\Repositories\Eloquent\Course\CommentRepositoryEloquent;
  6. use App\Repositories\Enums\ResponseCodeEnum;
  7. use App\Repositories\Presenters\Course\CommentPresenter;
  8. use Illuminate\Http\Request;
  9. class CommentService
  10. {
  11. /**
  12. * @var CommentRepositoryEloquent
  13. */
  14. private $commentRepository;
  15. /**
  16. * CommentService constructor.
  17. *
  18. * @param CommentRepositoryEloquent $commentRepositoryEloquent
  19. */
  20. public function __construct(CommentRepositoryEloquent $commentRepositoryEloquent)
  21. {
  22. $this->commentRepository = $commentRepositoryEloquent;
  23. }
  24. /**
  25. * @param Request $request
  26. *
  27. * @return mixed
  28. * @throws \Prettus\Repository\Exceptions\RepositoryException
  29. */
  30. public function handleList(Request $request)
  31. {
  32. $this->commentRepository->pushCriteria(new CommentCriteria($request));
  33. $this->commentRepository->setPresenter(CommentPresenter::class);
  34. return $this->commentRepository->searchCommentsByPage();
  35. }
  36. /**
  37. * @param $id
  38. *
  39. * @return \Illuminate\Database\Eloquent\Model
  40. */
  41. public function handleProfile($id)
  42. {
  43. $this->commentRepository->setPresenter(CommentPresenter::class);
  44. return $this->commentRepository->searchCommentBy($id);
  45. }
  46. /**
  47. * @param array $data
  48. *
  49. * @return mixed
  50. * @throws \Prettus\Validator\Exceptions\ValidatorException
  51. */
  52. public function handleStore($data)
  53. {
  54. $comment = $this->commentRepository->create($data);
  55. return $comment;
  56. }
  57. /**
  58. * @param array $data
  59. *
  60. * @return mixed
  61. * @throws \Prettus\Validator\Exceptions\ValidatorException
  62. */
  63. public function handleUpdate($data)
  64. {
  65. $comment = $this->commentRepository->update($data, $data['id']);
  66. return $comment;
  67. }
  68. /**
  69. * @param Request $request
  70. *
  71. * @return mixed
  72. * @throws \Prettus\Validator\Exceptions\ValidatorException
  73. */
  74. public function handleDelete($id)
  75. {
  76. return $this->commentRepository->delete($id);
  77. }
  78. /**
  79. * @param Request $request
  80. *
  81. * @return mixed
  82. * @throws \Prettus\Repository\Exceptions\RepositoryException
  83. */
  84. public function handleAll(Request $request)
  85. {
  86. $this->commentRepository->pushCriteria(new CommentCriteria($request));
  87. $this->commentRepository->setPresenter(CommentPresenter::class);
  88. return $this->commentRepository->get();
  89. }
  90. /**
  91. * 批量删除
  92. * @param $ids
  93. * @return mixed
  94. */
  95. public function handleBatchDelete($ids)
  96. {
  97. return $this->commentRepository->whereIn('id', $ids)->delete();
  98. }
  99. public function handleUserDelete($id)
  100. {
  101. $node = $this->commentRepository->find($id);
  102. if ($node['user_id'] != login_user_id()) abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '非法操作');
  103. return $this->commentRepository->delete($id);
  104. }
  105. }