CommentUpdate.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 CommentUpdate
  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. // 게시판 코드 확인
  20. if (!$code || !$postID || !$commentID) {
  21. throw new Exception("잘못된 접근입니다.", 404);
  22. }
  23. $boardService = new BoardService();
  24. // 게시판 정보 조회
  25. $board = $boardService->find($code);
  26. // 게시판 존재 유무
  27. if (!$board->exists) {
  28. throw new Exception('존재하지 않는 게시판입니다.');
  29. }
  30. // 게시판 사용 여부
  31. if ($board->is_display == 0) {
  32. throw new Exception('더 이상 사용하지 않는 게시판입니다.');
  33. }
  34. $postService = new PostService();
  35. // 게시글 조회
  36. $post = $postService->postModel->isExists($postID);
  37. if(!$post) {
  38. throw new Exception('존재하지 않는 게시글입니다.');
  39. }
  40. // 댓글 조회
  41. $commentService = new CommentService();
  42. $comment = $commentService->find($commentID);
  43. if(!$comment->exists) {
  44. throw new Exception('존재하지 않는 댓글입니다.');
  45. }
  46. $user = $request->user();
  47. // 비회원 댓글은 비밀번호 확인
  48. if(!$user && !$comment->user_id) {
  49. $passwd = $request->input('password');
  50. if(!$passwd || $passwd != $comment->password) {
  51. throw new Exception('권한이 없습니다.');
  52. }
  53. }
  54. $boardMeta = $boardService->meta($board->id);
  55. // 수정 권한 확인
  56. $response = Gate::inspect('update', [$comment, $boardMeta]);
  57. if($response->denied()) {
  58. throw new Exception($response->message(), $response->code());
  59. }
  60. $request->merge([
  61. 'board' => $board,
  62. 'boardMeta' => $boardMeta,
  63. 'comment' => $comment
  64. ]);
  65. }catch(Exception $e) {
  66. return ResponseData::fromException($e);
  67. }
  68. return $next($request);
  69. }
  70. }