db = \Config\Database::connect(); $this->model = new \App\Models\Test\TestDefSiteModel; $this->modelCal = new \App\Models\Test\TestDefCalModel; $this->modelTech = new \App\Models\Test\TestDefTechModel; $this->modelGrp = new \App\Models\Test\TestDefGrpModel; $this->modelValueSet = new \App\Models\ValueSet\ValueSetModel; // Basic validation for the main part $this->rules = [ 'TestSiteCode' => 'required', 'TestSiteName' => 'required', 'TestType' => 'required' ]; } public function index() { $rows = $this->model->getTests(); if (empty($rows)) { return $this->respond([ 'status' => 'success', 'message' => "no Data.", 'data' => [] ], 200); } return $this->respond([ 'status' => 'success', 'message'=> "Data fetched successfully", 'data' => $rows ], 200); } public function show($id = null) { if (!$id) return $this->failValidationErrors('ID is required'); $rows = $this->model->getTest($id); if (empty($rows)) { return $this->respond([ 'status' => 'success', 'message' => "no Data.", 'data' => [] ], 200); } return $this->respond([ 'status' => 'success', 'message'=> "Data fetched successfully", 'data' => $rows ], 200); } public function create() { $input = $this->request->getJSON(true); if (!$this->validateData($input, $this->rules)) { return $this->failValidationErrors($this->validator->getErrors()); } $this->db->transStart(); try { // 1. Insert into Main Table (testdefsite) $id = $this->model->insert($input); if (!$id) { throw new \Exception("Failed to insert main test definition"); } // 2. Handle Details based on TestType $this->handleDetails($id, $input, 'insert'); $this->db->transComplete(); if ($this->db->transStatus() === false) { return $this->failServerError('Transaction failed'); } return $this->respondCreated([ 'status' => 'success', 'message' => "data created successfully", 'data'=> $id ]); } catch (\Exception $e) { $this->db->transRollback(); return $this->failServerError('Something went wrong: ' . $e->getMessage()); } } public function update($id = null) { $input = $this->request->getJSON(true); // Determine ID if (!$id && isset($input["TestSiteID"])) { $id = $input["TestSiteID"]; } if (!$id) { return $this->failValidationErrors('TestSiteID is required.'); } // Optional validation // if (!$this->validateData($input, $this->rules)) { ... } $this->db->transStart(); try { // 1. Update Main Table $this->model->update($id, $input); // 2. Handle Details $this->handleDetails($id, $input, 'update'); $this->db->transComplete(); if ($this->db->transStatus() === false) { return $this->failServerError('Transaction failed'); } return $this->respondCreated([ 'status' => 'success', 'message' => "data updated successfully", 'data'=> $id ]); } catch (\Exception $e) { $this->db->transRollback(); return $this->failServerError('Something went wrong: ' . $e->getMessage()); } } /** * Helper to handle inserting/updating sub-tables based on TestType */ private function handleDetails($testSiteID, $input, $action) { $testTypeID = $input['TestType'] ?? null; // If update and TestType not in payload, fetch from DB if (!$testTypeID && $action === 'update') { $existing = $this->model->find($testSiteID); $testTypeID = $existing['TestType'] ?? null; } if (!$testTypeID) return; // Should not happen if required // Get Type Code (TEST, PARAM, CALC, GROUP, TITLE) $vs = $this->modelValueSet->find($testTypeID); $typeCode = $vs['VValue'] ?? ''; // Get details data if present (for 'details' key in unified JSON) // We accept both flat (top-level) and nested 'details' for flexibility, prefer 'details' $details = $input['details'] ?? $input; $details['TestSiteID'] = $testSiteID; // Ensure foreign key is set $details['SiteID'] = $input['SiteID'] ?? 1; switch ($typeCode) { case 'CALC': $this->saveSubTable($this->modelCal, $testSiteID, $details, $action, 'TestCalID'); break; case 'GROUP': // Groups are special: List of members // Payload expected: details: { members: [{Member: 1}, {Member: 2}] } if ($action === 'update') { $this->modelGrp->where('TestSiteID', $testSiteID)->delete(); } $members = $details['members'] ?? ($input['Members'] ?? []); if (is_array($members)) { foreach ($members as $m) { $memberID = is_array($m) ? ($m['Member'] ?? null) : $m; if ($memberID) { $this->modelGrp->insert([ 'SiteID' => $details['SiteID'], 'TestSiteID' => $testSiteID, 'Member' => $memberID ]); } } } break; case 'TEST': case 'PARAM': default: // Default to TestDefTech for 'TEST' and 'PARAM' $this->saveSubTable($this->modelTech, $testSiteID, $details, $action, 'TestTechID'); break; } } private function saveSubTable($model, $testSiteID, $data, $action, $pkName) { if ($action === 'update') { // Check existence $exists = $model->where('TestSiteID', $testSiteID)->first(); if ($exists) { $model->update($exists[$pkName], $data); } else { $model->insert($data); } } else { $model->insert($data); } } }