CommentController.php 2.5 KB

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