requireAuth(); if ($auth instanceof ResponseInterface) { return $auth; } $params = $this->getPaginationParams(); $builder = $this->getDB()->table('showrooms s'); // Join with branches table to get branch name $builder->select('s.*, b.name as branch_name'); $builder->join('branches b', 's.branch_id = b.id', 'left'); // Search $searchType = $this->request->getGet('search_type'); $searchKeyword = $this->request->getGet('search_keyword'); $isActive = $this->request->getGet('is_active'); if ($searchType && $searchKeyword) { if ($searchType === 'name') { $builder->like('s.name', $searchKeyword); } elseif ($searchType === 'address') { $builder->like('s.address', $searchKeyword); } elseif ($searchType === 'phone') { $builder->like('s.main_phone', $searchKeyword); } } // is_active 필터링 if ($isActive !== null && $isActive !== '' && $isActive !== false) { $builder->where('s.is_active', strval($isActive)); } $builder->orderBy('s.id', 'DESC'); $result = $this->paginatedResponse($builder, $params); // Decode links JSON for each item foreach ($result['items'] as &$item) { if (!empty($item->links)) { $item->links = json_decode($item->links, true); } else { $item->links = []; } } return $this->respondSuccess($result); } /** * Get single showroom */ public function show($id = null) { $auth = $this->requireAuth(); if ($auth instanceof ResponseInterface) { return $auth; } $builder = $this->getDB()->table('showrooms s'); $builder->select('s.*, b.name as branch_name'); $builder->join('branches b', 's.branch_id = b.id', 'left'); $builder->where('s.id', $id); $showroom = $builder->get()->getRow(); if (!$showroom) { return $this->respondError('전시장을 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND); } // Decode links JSON if (!empty($showroom->links)) { $showroom->links = json_decode($showroom->links, true); } else { $showroom->links = []; } return $this->respondSuccess($showroom); } /** * Create showroom */ public function create() { $auth = $this->requireAuth(); if ($auth instanceof ResponseInterface) { return $auth; } try { $json = $this->request->getJSON(); // Validation if (empty($json->name)) { return $this->respondError('전시장명을 입력하세요.', ResponseInterface::HTTP_BAD_REQUEST); } if (empty($json->branch_id)) { return $this->respondError('소속 지점을 선택하세요.', ResponseInterface::HTTP_BAD_REQUEST); } if (empty($json->phone)) { return $this->respondError('대표번호를 입력하세요.', ResponseInterface::HTTP_BAD_REQUEST); } if (empty($json->address)) { return $this->respondError('주소를 입력하세요.', ResponseInterface::HTTP_BAD_REQUEST); } // Encode links to JSON $links = null; if (!empty($json->links) && is_array($json->links)) { $links = json_encode($json->links); } // 데이터 준비 $data = [ 'name' => $json->name ?? '', 'branch_id' => $json->branch_id ?? null, 'main_phone' => $json->phone ?? '', 'address' => $json->address ?? '', 'detail_address' => $json->detail_address ?? '', 'latitude' => $json->latitude ?? null, 'longitude' => $json->longitude ?? null, 'business_hours' => $json->business_hours ?? '', 'quote_link' => $json->quote_link ?? '', 'test_drive_link' => $json->test_drive_link ?? '', 'links' => $links, 'created_at' => date('Y-m-d H:i:s') ]; $builder = $this->getDB()->table('showrooms'); $result = $builder->insert($data); if (!$result) { return $this->respondError('전시장 등록에 실패했습니다.', ResponseInterface::HTTP_INTERNAL_SERVER_ERROR); } return $this->respondSuccess(['id' => $this->getDB()->insertID()], '전시장이 등록되었습니다.'); } catch (\Exception $e) { log_message('error', 'Showroom create error: ' . $e->getMessage()); return $this->respondError('서버 오류가 발생했습니다: ' . $e->getMessage(), ResponseInterface::HTTP_INTERNAL_SERVER_ERROR); } } /** * Update showroom */ public function update($id = null) { $auth = $this->requireAuth(); if ($auth instanceof ResponseInterface) { return $auth; } $json = $this->request->getJSON(); // Encode links to JSON $links = null; if (!empty($json->links) && is_array($json->links)) { $links = json_encode($json->links); } // 데이터 준비 $data = [ 'name' => $json->name ?? '', 'branch_id' => $json->branch_id ?? null, 'main_phone' => $json->phone ?? '', 'address' => $json->address ?? '', 'detail_address' => $json->detail_address ?? '', 'latitude' => $json->latitude ?? null, 'longitude' => $json->longitude ?? null, 'business_hours' => $json->business_hours ?? '', 'quote_link' => $json->quote_link ?? '', 'test_drive_link' => $json->test_drive_link ?? '', 'links' => $links, 'updated_at' => date('Y-m-d H:i:s') ]; $builder = $this->getDB()->table('showrooms'); $builder->where('id', $id)->update($data); return $this->respondSuccess(null, '전시장이 수정되었습니다.'); } /** * Delete showroom */ public function delete($id = null) { $auth = $this->requireAuth(); if ($auth instanceof ResponseInterface) { return $auth; } $builder = $this->getDB()->table('showrooms'); $builder->where('id', $id)->delete(); return $this->respondSuccess(null, '전시장이 삭제되었습니다.'); } /** * Toggle showroom active status */ public function toggleActive($id = null) { $auth = $this->requireAuth(); if ($auth instanceof ResponseInterface) { return $auth; } $builder = $this->getDB()->table('showrooms'); $showroom = $builder->where('id', $id)->get()->getRow(); if (!$showroom) { return $this->respondError('전시장을 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND); } // 현재 상태의 반대로 변경 $newStatus = $showroom->is_active == 1 ? 0 : 1; $builder->where('id', $id)->update([ 'is_active' => $newStatus, 'updated_at' => date('Y-m-d H:i:s') ]); $statusText = $newStatus == 1 ? '사용' : '비사용'; return $this->respondSuccess(['is_active' => $newStatus], "전시장이 {$statusText} 상태로 변경되었습니다."); } /** * Get public showroom list (No authentication required) * 공개 API - 인증 없이 활성 전시장 목록 조회 */ public function publicList() { try { $builder = $this->getDB()->table('showrooms s'); $builder->select('s.*, b.name as branch_name'); $builder->join('branches b', 's.branch_id = b.id', 'left'); // 활성화된 전시장만 조회 $builder->where('s.is_active', 1); $builder->orderBy('s.id', 'DESC'); $showrooms = $builder->get()->getResult(); // Decode links JSON for each item foreach ($showrooms as &$showroom) { if (!empty($showroom->links)) { $showroom->links = json_decode($showroom->links, true); } else { $showroom->links = []; } } return $this->respondSuccess($showrooms); } catch (\Exception $e) { log_message('error', 'Showroom public list error: ' . $e->getMessage()); return $this->respondError('서버 오류가 발생했습니다.', ResponseInterface::HTTP_INTERNAL_SERVER_ERROR); } } /** * Get public single showroom (No authentication required) * 공개 API - 인증 없이 단일 전시장 정보 조회 */ public function publicShow($id = null) { try { $builder = $this->getDB()->table('showrooms s'); $builder->select('s.*, b.name as branch_name'); $builder->join('branches b', 's.branch_id = b.id', 'left'); $builder->where('s.id', $id); $builder->where('s.is_active', 1); $showroom = $builder->get()->getRow(); if (!$showroom) { return $this->respondError('전시장을 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND); } // Decode links JSON if (!empty($showroom->links)) { $showroom->links = json_decode($showroom->links, true); } else { $showroom->links = []; } return $this->respondSuccess($showroom); } catch (\Exception $e) { log_message('error', 'Showroom public show error: ' . $e->getMessage()); return $this->respondError('서버 오류가 발생했습니다.', ResponseInterface::HTTP_INTERNAL_SERVER_ERROR); } } }