CommentController.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace App\Http\Controllers\Admin\Board;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\Board;
  6. use App\Models\Comment;
  7. use App\Models\DTO\SearchData;
  8. use Exception;
  9. class CommentController extends Controller
  10. {
  11. private Board $boardModel;
  12. private Comment $commentModel;
  13. public function __construct(Board $board, Comment $comment)
  14. {
  15. $this->boardModel = $board;
  16. $this->commentModel = $comment;
  17. }
  18. /**
  19. * 게시판 관리 > 댓글 관리
  20. * @method GET
  21. * @see /admin/board/comment
  22. */
  23. public function index(Request $request)
  24. {
  25. $params = SearchData::fromRequest($request);
  26. $params->boardID = $request->get('board_id');
  27. $commentData = $this->commentModel->data($params);
  28. if ($commentData->rows > 0) {
  29. $num = listNum($commentData->total, $params->page, $params->perPage);
  30. foreach ($commentData->list as $i => $row) {
  31. $row->num = $num--;
  32. $row->postURL = route('board.post.view', [$row->code, $row->post_id]);
  33. $row->page = $row->findPageNumber($row->board_id, $row->post_id, $row->id);
  34. $row->device = (MAP_DEVICE_ICON_TYPE[$row->device_type] ?? '');
  35. $commentData->list[$i] = $row;
  36. }
  37. }
  38. return view('admin.board.comment.index', [
  39. 'commentData' => $commentData,
  40. 'boardData' => $this->boardModel->all(),
  41. 'params' => $params
  42. ]);
  43. }
  44. /**
  45. * 게시판 관리 > 댓글 삭제
  46. * @method DELETE
  47. * @see /admin/board/comment/destroy
  48. */
  49. public function destroy(Request $request)
  50. {
  51. try {
  52. $chk = $request->post('chk');
  53. if ($chk) {
  54. $user = $request->user();
  55. foreach ($chk as $commentID) {
  56. $comment = $this->commentModel->findOrNew($commentID);
  57. if(!$comment->exists) {
  58. throw new Exception($commentID . '번 댓글은 존재하지 않습니다.');
  59. }
  60. if (!$this->commentModel->remove($comment, $user)) {
  61. throw new Exception($commentID . "번 댓글을 삭제할 수 없습니다.");
  62. }
  63. }
  64. }
  65. $message = '댓글이 삭제되었습니다.';
  66. return redirect()->route('admin.board.comment.index')->with('message', $message);
  67. } catch (Exception $e) {
  68. return back()->withErrors($e->getMessage())->withInput();
  69. }
  70. }
  71. }