PostController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace App\Http\Controllers\Admin\Board\Like;
  3. use Illuminate\Http\Request;
  4. use App\Http\Traits\AgentTrait;
  5. use App\Http\Controllers\Controller;
  6. use App\Models\Board;
  7. use App\Models\Post;
  8. use App\Models\PostLike;
  9. use App\Models\DTO\SearchData;
  10. use Exception;
  11. class PostController extends Controller
  12. {
  13. use AgentTrait;
  14. private Board $boardModel;
  15. private PostLike $postLikeModel;
  16. public function __construct(Board $board, PostLike $postLike)
  17. {
  18. $this->boardModel = $board;
  19. $this->postLikeModel = $postLike;
  20. }
  21. /**
  22. * 게시판 > 추천/비추 조회
  23. * @method GET
  24. * @see /admin/board/like/post
  25. */
  26. public function index(Request $request)
  27. {
  28. $params = SearchData::fromRequest($request);
  29. $params->boardID = $request->post('board_id');
  30. $likeData = $this->postLikeModel->data($params);
  31. if ($likeData->rows > 0) {
  32. $num = listNum($likeData->total, $params->page, $params->perPage);
  33. foreach ($likeData->list as $i => $row) {
  34. $row->num = $num--;
  35. $row->postURL = route('board.post.view', [$row->code, $row->post_id]);
  36. $row->editURL = route('admin.board.like.post.edit', $row->id);
  37. $likeData->list[$i] = $row;
  38. }
  39. }
  40. return view('admin.board.like.post.index', [
  41. 'likeData' => $likeData,
  42. 'params' => $params,
  43. 'boardData' => $this->boardModel->all()
  44. ]);
  45. }
  46. /**
  47. * 게시판 > 추천/비추 삭제
  48. * @method DELETE
  49. * @see /admin/board/like/post/destroy
  50. */
  51. public function destroy(Request $request, Post $postModel)
  52. {
  53. try {
  54. $chk = $request->post('chk');
  55. if ($chk) {
  56. foreach ($chk as $postLikeID) {
  57. $postLike = $this->postLikeModel->findOrNew($postLikeID);
  58. if(!$postLike->exists) {
  59. throw new Exception($postLikeID . '번 추천/비추 기록이 존재하지 않습니다.');
  60. }
  61. if(!$postLike->delete()) {
  62. throw new Exception($postLikeID . '번 추천/비추 기록을 삭제할 수 없습니다.');
  63. }
  64. // 게시글 추천/비추천 개수 갱신
  65. if($postLike->type == 1) {
  66. $postModel->decreaseLike($postLike->post_id);
  67. }else{
  68. $postModel->decreaseDisLike($postLike->post_id);
  69. }
  70. }
  71. }
  72. $message = '게시글 추천/비추 기록이 삭제되었습니다.';
  73. return redirect()->route('admin.board.like.post.index')->with('message', $message);
  74. } catch (Exception $e) {
  75. return back()->withErrors($e->getMessage())->withInput();
  76. }
  77. }
  78. /**
  79. * 게시판 > 추천/비추천 상세 보기
  80. * @method GET
  81. * @see /admin/board/like/{pk}/edit
  82. */
  83. public function edit(int $postLikeID)
  84. {
  85. $like = $this->postLikeModel->findOrFail($postLikeID);
  86. $like->device = $this->device($like->user_agent);
  87. $like->platform = $this->platform($like->user_agent);
  88. $like->browser = $this->browser($like->user_agent);
  89. return view('admin.board.like.post.edit', [
  90. 'like' => $like
  91. ]);
  92. }
  93. }