MainController.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Support\Facades\Http;
  4. use Illuminate\Support\Facades\Cache;
  5. use Illuminate\Http\Request;
  6. use App\Http\Traits\CommonTrait;
  7. use App\Models\Popup;
  8. use App\Models\DTO\SearchData;
  9. use App\Services\BoardService;
  10. class MainController extends Controller
  11. {
  12. use CommonTrait;
  13. private BoardService $boardService;
  14. private Popup $popupModel;
  15. /**
  16. * Create a new controller instance.
  17. *
  18. * @return void
  19. */
  20. public function __construct(BoardService $boardService, Popup $popupModel)
  21. {
  22. $this->middleware('front');
  23. $this->boardService = $boardService;
  24. $this->popupModel = $popupModel;
  25. }
  26. /**
  27. * Show the application dashboard.
  28. * @method GET
  29. * @see /
  30. * @return \Illuminate\Contracts\Support\Renderable
  31. */
  32. public function index()
  33. {
  34. // 팝업
  35. $popups = $this->popupModel->list();
  36. // 최근 게시글
  37. $recent = $this->boardService->latest(null, 1, DEFAULT_LIST_PER_PAGE);
  38. // 공지사항
  39. $notice = $this->boardService->latest('notice', 1, DEFAULT_LIST_PER_PAGE);
  40. // 사진게시판
  41. $photo = $this->boardService->latest('photo', 1, DEFAULT_LIST_PER_PAGE);
  42. // 최신 뉴스
  43. $news = $this->_news();
  44. return view(layout('main'), [
  45. 'popups' => $popups,
  46. 'recent' => $recent,
  47. 'notice' => $notice,
  48. 'photo' => $photo,
  49. 'news' => $news
  50. ]);
  51. }
  52. /**
  53. * 최신글 보기
  54. * @method GET
  55. * @see /recently
  56. */
  57. public function recently(Request $request)
  58. {
  59. $params = SearchData::fromRequest($request);
  60. $listURL = route('recently');
  61. // 최근 게시글
  62. if ($params->category || $params->keyword) {
  63. $recent = $this->boardService->search($params);
  64. } else {
  65. $recent = $this->boardService->latest(null, $params->page, $params->perPage);
  66. }
  67. return view(layout('recently'), [
  68. 'recent' => $recent,
  69. 'params' => $params,
  70. 'listURL' => $listURL
  71. ]);
  72. }
  73. /**
  74. * 최신 뉴스 조회
  75. */
  76. private function _news()
  77. {
  78. // 뉴스
  79. if (!$news = Cache::get('mainNews')) {
  80. $response = Http::get('https://serpapi.com/search', [
  81. 'q' => '주요 뉴스',
  82. 'tbm' => 'nws',
  83. 'api_key'=> SERP_API_KEY
  84. ]);
  85. if($response->ok()) {
  86. $news = $response->json('news_results');
  87. Cache::put('mainNews', $news, 86400);
  88. }
  89. }
  90. return $news;
  91. }
  92. }