ListController.php 2.4 KB

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