PostController.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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\BoardCategory;
  7. use App\Models\Post;
  8. use App\Models\DTO\SearchData;
  9. use Exception;
  10. class PostController extends Controller
  11. {
  12. private Board $boardModel;
  13. private BoardCategory $boardCategoryModel;
  14. private Post $postModel;
  15. public function __construct(Board $board, BoardCategory $boardCategory, Post $post)
  16. {
  17. $this->boardModel = $board;
  18. $this->boardCategoryModel = $boardCategory;
  19. $this->postModel = $post;
  20. }
  21. /**
  22. * 게시판 > 게시글 관리
  23. * @method GET
  24. * @see /admin/board/post
  25. */
  26. public function index(Request $request)
  27. {
  28. $params = SearchData::fromRequest($request);
  29. $params->boardID = $request->get('board_id');
  30. $postData = $this->postModel->data($params);
  31. if ($postData->rows > 0) {
  32. $num = listNum($postData->total, $params->page, $params->perPage);
  33. foreach ($postData->list as $i => $row) {
  34. $row->num = $num--;
  35. $row->postURL = route('board.post.view', [$row->code, $row->id]);
  36. $row->hits = number_format($row->hit);
  37. $row->hvFile = ($row->file_rows > 0);
  38. $row->hvImage = ($row->image_rows > 0);
  39. $row->device = MAP_DEVICE_ICON_TYPE[$row->device_type];
  40. $row->secret = ($row->is_secret > 0);
  41. $postData->list[$i] = $row;
  42. }
  43. }
  44. return view('admin.board.post.index', [
  45. 'postData' => $postData,
  46. 'params' => $params,
  47. 'boardData' => $this->boardModel->all()
  48. ]);
  49. }
  50. /**
  51. * 게시글 관리 > 게시물 삭제
  52. * @method DELETE
  53. * @see /admin/board/post/destroy
  54. */
  55. public function destroy(Request $request)
  56. {
  57. try {
  58. $chk = $request->post('chk');
  59. if ($chk) {
  60. $user = $request->user();
  61. foreach ($chk as $postID) {
  62. $post = $this->postModel->findOrNew($postID);
  63. if(!$post->exists) {
  64. throw new Exception($postID . '번 게시물은 존재하지 않습니다.');
  65. }
  66. if (!$this->postModel->remove($post, $user)) {
  67. throw new Exception($postID . "번 게시물을 삭제할 수 없습니다.");
  68. }
  69. }
  70. }
  71. $message = '게시물이 삭제되었습니다.';
  72. return redirect()->route('admin.board.post.index')->with('message', $message);
  73. } catch (Exception $e) {
  74. return back()->withErrors($e->getMessage())->withInput();
  75. }
  76. }
  77. }