CommentController.php 3.1 KB

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