| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- <?php
- namespace App\Http\Middleware\Board;
- use Closure;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Gate;
- use App\Services\BoardService;
- use App\Services\PostService;
- use App\Services\CommentService;
- use App\Models\DTO\ResponseData;
- use Exception;
- class CommentUpdate
- {
- public function handle(Request $request, Closure $next)
- {
- try{
- $code = $request->route('code');
- $postID = $request->route('postID');
- $commentID = $request->input('cid');
- // 게시판 코드 확인
- if (!$code || !$postID || !$commentID) {
- throw new Exception("잘못된 접근입니다.", 404);
- }
- $boardService = new BoardService();
- // 게시판 정보 조회
- $board = $boardService->find($code);
- // 게시판 존재 유무
- if (!$board->exists) {
- throw new Exception('존재하지 않는 게시판입니다.');
- }
- // 게시판 사용 여부
- if ($board->is_display == 0) {
- throw new Exception('더 이상 사용하지 않는 게시판입니다.');
- }
- $postService = new PostService();
- // 게시글 조회
- $post = $postService->postModel->isExists($postID);
- if(!$post) {
- throw new Exception('존재하지 않는 게시글입니다.');
- }
- // 댓글 조회
- $commentService = new CommentService();
- $comment = $commentService->find($commentID);
- if(!$comment->exists) {
- throw new Exception('존재하지 않는 댓글입니다.');
- }
- $user = $request->user();
- // 비회원 댓글은 비밀번호 확인
- if(!$user && !$comment->user_id) {
- $passwd = $request->input('password');
- if(!$passwd || $passwd != $comment->password) {
- throw new Exception('권한이 없습니다.');
- }
- }
- $boardMeta = $boardService->meta($board->id);
- // 수정 권한 확인
- $response = Gate::inspect('update', [$comment, $boardMeta]);
- if($response->denied()) {
- throw new Exception($response->message(), $response->code());
- }
- $request->merge([
- 'board' => $board,
- 'boardMeta' => $boardMeta,
- 'comment' => $comment
- ]);
- }catch(Exception $e) {
- return ResponseData::fromException($e);
- }
- return $next($request);
- }
- }
|