CommentCreate.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace App\Http\Middleware\Board;
  3. use Closure;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\Gate;
  6. use App\Services\BoardService;
  7. use App\Services\PostService;
  8. use App\Services\CommentService;
  9. use App\Models\DTO\ResponseData;
  10. use Exception;
  11. class CommentCreate
  12. {
  13. public function handle(Request $request, Closure $next)
  14. {
  15. try{
  16. $code = $request->route('code');
  17. $postID = $request->route('postID');
  18. $commentID = $request->input('cid');
  19. $mode = $request->input('mode');
  20. // 게시판 코드 확인
  21. if (!$code || !$postID) {
  22. throw new Exception('잘못된 접근입니다.', 404);
  23. }
  24. $boardService = new BoardService();
  25. // 게시판 정보 조회
  26. $board = $boardService->find($code);
  27. // 게시판 존재 유무
  28. if (!$board->exists) {
  29. throw new Exception('존재하지 않는 게시판입니다.');
  30. }
  31. // 게시판 사용 여부
  32. if ($board->is_display == 0) {
  33. throw new Exception('더 이상 사용하지 않는 게시판입니다.');
  34. }
  35. $postService = new PostService();
  36. // 게시글 조회
  37. $post = $postService->postModel->isExists($postID);
  38. if(!$post) {
  39. throw new Exception('존재하지 않는 게시글입니다.');
  40. }
  41. // 댓글 조회
  42. if($commentID && $mode == 'reply') {
  43. // 답글 작성 시 확인
  44. $commentService = new CommentService();
  45. $comment = $commentService->find($commentID);
  46. if(!$comment->exists) {
  47. throw new Exception('존재하지 않는 댓글입니다.');
  48. }
  49. }
  50. // 게시판 정보 조회
  51. $boardMeta = $boardService->meta($board->id);
  52. // 댓글 쓰기 권한 확인
  53. $response = Gate::inspect('create', ['App\Models\Comment', $boardMeta]);
  54. if($response->denied()) {
  55. throw new Exception($response->message(), $response->code());
  56. }
  57. $request->merge([
  58. 'board' => $board,
  59. 'boardMeta' => $boardMeta
  60. ]);
  61. }catch(Exception $e) {
  62. return ResponseData::fromException($e);
  63. }
  64. return $next($request);
  65. }
  66. }