requireAuth(); if ($auth instanceof ResponseInterface) { return $auth; } $params = $this->getPaginationParams(); $builder = $this->getDB()->table('branches'); // 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('name', $searchKeyword); } } // is_active 필터링 (파라미터가 있을 때만) if ($isActive !== null && $isActive !== '' && $isActive !== false) { $builder->where('is_active', strval($isActive)); } $builder->orderBy('id', 'DESC'); $result = $this->paginatedResponse($builder, $params); return $this->respondSuccess($result); } /** * Get single branch */ public function show($id = null) { $auth = $this->requireAuth(); if ($auth instanceof ResponseInterface) { return $auth; } $builder = $this->getDB()->table('branches'); $branch = $builder->where('id', $id)->get()->getRow(); if (!$branch) { return $this->respondError('지점을 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND); } return $this->respondSuccess($branch); } /** * Create branch */ 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->phone)) { // return $this->respondError('대표번호를 입력하세요.', ResponseInterface::HTTP_BAD_REQUEST); // } // if (empty($json->address)) { // return $this->respondError('주소를 입력하세요.', ResponseInterface::HTTP_BAD_REQUEST); // } // 데이터 준비 $data = [ 'name' => $json->name ?? '', // 'main_phone' => $json->phone ?? '', // DB 컬럼명은 main_phone // 'address' => $json->address ?? '', // 'detail_address' => $json->detail_address ?? '', // 'latitude' => $json->latitude ?? null, // 'longitude' => $json->longitude ?? null, // 'business_hours' => $json->business_hours ?? '', 'created_at' => date('Y-m-d H:i:s') ]; $builder = $this->getDB()->table('branches'); $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', 'Branch create error: ' . $e->getMessage()); return $this->respondError('서버 오류가 발생했습니다: ' . $e->getMessage(), ResponseInterface::HTTP_INTERNAL_SERVER_ERROR); } } /** * Update branch */ public function update($id = null) { $auth = $this->requireAuth(); if ($auth instanceof ResponseInterface) { return $auth; } $json = $this->request->getJSON(); // 데이터 준비 $data = [ 'name' => $json->name ?? '', // 'main_phone' => $json->phone ?? '', // DB 컬럼명은 main_phone // 'address' => $json->address ?? '', // 'detail_address' => $json->detail_address ?? '', // 'latitude' => $json->latitude ?? null, // 'longitude' => $json->longitude ?? null, // 'business_hours' => $json->business_hours ?? '', 'updated_at' => date('Y-m-d H:i:s') ]; $builder = $this->getDB()->table('branches'); $builder->where('id', $id)->update($data); return $this->respondSuccess(null, '지점이 수정되었습니다.'); } /** * Delete branch */ public function delete($id = null) { $auth = $this->requireAuth(); if ($auth instanceof ResponseInterface) { return $auth; } $builder = $this->getDB()->table('branches'); $builder->where('id', $id)->delete(); return $this->respondSuccess(null, '지점이 삭제되었습니다.'); } /** * Toggle branch active status */ public function toggleActive($id = null) { $auth = $this->requireAuth(); if ($auth instanceof ResponseInterface) { return $auth; } $builder = $this->getDB()->table('branches'); $branch = $builder->where('id', $id)->get()->getRow(); if (!$branch) { return $this->respondError('지점을 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND); } // 현재 상태의 반대로 변경 $newStatus = $branch->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 branch list (No authentication required) * 공개 API - 인증 없이 활성 지점 목록 조회 */ public function publicList() { try { $builder = $this->getDB()->table('branches'); // 활성화된 지점만 조회 $builder->where('is_active', 1); $builder->orderBy('id', 'DESC'); $branches = $builder->get()->getResult(); return $this->respondSuccess($branches); } catch (\Exception $e) { log_message('error', 'Branch public list error: ' . $e->getMessage()); return $this->respondError('서버 오류가 발생했습니다.', ResponseInterface::HTTP_INTERNAL_SERVER_ERROR); } } /** * Get public single branch (No authentication required) * 공개 API - 인증 없이 단일 지점 정보 조회 */ public function publicShow($id = null) { try { $builder = $this->getDB()->table('branches'); $builder->where('id', $id); $builder->where('is_active', 1); $branch = $builder->get()->getRow(); if (!$branch) { return $this->respondError('지점을 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND); } return $this->respondSuccess($branch); } catch (\Exception $e) { log_message('error', 'Branch public show error: ' . $e->getMessage()); return $this->respondError('서버 오류가 발생했습니다.', ResponseInterface::HTTP_INTERNAL_SERVER_ERROR); } } }