CompanyController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace App\Http\Controllers\Movie;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Http\Client\ConnectionException;
  5. use Illuminate\Support\Facades\Http;
  6. use Illuminate\Support\Facades\Validator;
  7. use App\Http\Controllers\Controller;
  8. use App\Http\Traits\CommonTrait;
  9. use App\Http\Traits\PagingTrait;
  10. use App\Models\DTO\Movie\CompanyParams;
  11. use Exception;
  12. class CompanyController extends Controller
  13. {
  14. use CommonTrait;
  15. use PagingTrait;
  16. private string $host;
  17. public function __construct() {
  18. $this->middleware('front');
  19. $this->host = env('MOVIE_API');
  20. }
  21. /**
  22. * 영화사 목록
  23. * @method GET
  24. * @see /movie/company
  25. */
  26. public function index(Request $request, CompanyParams $params)
  27. {
  28. $rules = [
  29. 'page' => 'nullable|numeric',
  30. 'keyword' => 'nullable|string|max:100',
  31. 'ceo' => 'nullable|string|max:30'
  32. ];
  33. $attributes = [
  34. 'page' => '페이지 번호',
  35. 'keyword' => '영화사명',
  36. 'ceo' => '대표자명'
  37. ];
  38. $message = [
  39. 'page.numeric' => '페이지 번호는 숫자여야 합니다.',
  40. 'keyword.max' => '영화사명 최대 글자는 100자 입니다.',
  41. 'ceo.max' => '대표자명 최대 글자는 30자 입니다.'
  42. ];
  43. // 검색 유효성 검사
  44. Validator::make($request->all(), $rules, $message, $attributes)->validated();
  45. $page = max(1, (int)$request->get('page', 1));
  46. $params->curPage = $page;
  47. $params->companyNm = $request->get('keyword');
  48. $params->ceoNm = $request->get('ceo');
  49. // 검색 조건 (crawler 장애 시 빈 목록으로 격리)
  50. $total = 0;
  51. $list = [];
  52. try {
  53. $response = Http::timeout(5)->get($this->host . COMPANY_LIST, $params->toArray());
  54. } catch (ConnectionException) {
  55. $response = null;
  56. }
  57. if ($response && $response->ok()) {
  58. $data = json_decode($response->body());
  59. $total = (int)($data->total ?? 0);
  60. if ($total > 0) {
  61. $queryString = $request->getQueryString();
  62. foreach ($data->list as $row) {
  63. $row->viewURL = route('movie.company.show', base64_encode($row->companyCd));
  64. if ($queryString) {
  65. $row->viewURL .= ('?' . $queryString);
  66. }
  67. $list[] = $row;
  68. }
  69. }
  70. }
  71. $companies = $this->getPaginator($list, $total, $params->itemPerPage, $page);
  72. return view(layout('movie.company.index'), [
  73. 'keyword' => $request->get('keyword'),
  74. 'ceo' => $request->get('ceo'),
  75. 'total' => $total,
  76. 'companies' => $companies
  77. ]);
  78. }
  79. /**
  80. * 영화사 상세 보기
  81. * @method GET
  82. * @see /movie/company/{company_cd}
  83. */
  84. public function show(string $base64, Request $request, CompanyParams $params)
  85. {
  86. try {
  87. if(!$base64) {
  88. throw new Exception('잘못된 접근입니다. [1]');
  89. }
  90. $companyCd = base64_decode($base64);
  91. if(!$companyCd) {
  92. throw new Exception('잘못된 접근입니다. [2]');
  93. }
  94. $listURL = route('movie.company.index');
  95. if($queryString = $request->getQueryString()) {
  96. $listURL .= ('?' . $queryString);
  97. }
  98. // 검색 조건
  99. $params->companyCd = $companyCd;
  100. $response = Http::timeout(5)->get($this->host . COMPANY_INFO, $params->toArray());
  101. if(!$response->ok()) {
  102. throw new Exception('요청한 정보가 없습니다.');
  103. }
  104. $data = json_decode($response->body());
  105. $info = ($data->info ?? null);
  106. if(!$info || empty($info->companyCd)) {
  107. throw new Exception('요청한 정보가 없습니다.');
  108. }
  109. $companyNm = $info->companyNm;
  110. if(!empty($info->companyNmEn)) {
  111. $companyNm .= (' (' . $info->companyNmEn . ')');
  112. }
  113. return view(layout('movie.company.show'), [
  114. 'companyNm' => $companyNm,
  115. 'info' => $info,
  116. 'listURL' => $listURL
  117. ]);
  118. }catch(Exception $e) {
  119. abort($e->getCode() ?: 500, $e->getMessage());
  120. }
  121. }
  122. }