db = \Config\Database::connect(); $this->model = new SpecimenStatusModel(); $this->rules = []; } public function index() { try { $rows = $this->model->findAll(); // Transform Status and Activity foreach ($rows as &$row) { if (isset($row['Status'])) { $row['status'] = $row['Status']; $row['statusLabel'] = ValueSet::getLabel('specimen_status', $row['Status']) ?? ''; unset($row['Status']); } if (isset($row['Activity'])) { $row['activity'] = $row['Activity']; $row['activityLabel'] = ValueSet::getLabel('specimen_activity', $row['Activity']) ?? ''; unset($row['Activity']); } } return $this->respond([ 'status' => 'success', 'message'=> "data fetched successfully", 'data' => $rows ], 200); } catch (\Exception $e) { return $this->failServerError('Exception : '.$e->getMessage()); } } public function show($id) { try { $row = $this->model->where('SpcStaID', $id)->first(); if (empty($row)) { return $this->respond([ 'status' => 'success', 'message'=> "data not found", 'data' => null ], 200); } // Transform Status and Activity if (isset($row['Status'])) { $row['status'] = $row['Status']; $row['statusLabel'] = ValueSet::getLabel('specimen_status', $row['Status']) ?? ''; unset($row['Status']); } if (isset($row['Activity'])) { $row['activity'] = $row['Activity']; $row['activityLabel'] = ValueSet::getLabel('specimen_activity', $row['Activity']) ?? ''; unset($row['Activity']); } return $this->respond([ 'status' => 'success', 'message'=> "data fetched successfully", 'data' => $row ], 200); } catch (\Exception $e) { return $this->failServerError('Exception : '.$e->getMessage()); } } public function create() { $input = $this->request->getJSON(true); if (!$this->validateData($input, $this->rules)) { return $this->failValidationErrors($this->validator->getErrors()); } try { $id = $this->model->insert($input); return $this->respondCreated([ 'status' => 'success', 'message' => "data $id created successfully" ]); } catch (\Exception $e) { return $this->failServerError('Something went wrong: ' . $e->getMessage()); } } public function update() { $input = $this->request->getJSON(true); if (!$this->validateData($input, $this->rules)) { return $this->failValidationErrors($this->validator->getErrors()); } try { $id = $this->model->update($input['SpcStaID'], $input); return $this->respondCreated([ 'status' => 'success', 'message' => "data $id updated successfully" ]); } catch (\Exception $e) { return $this->failServerError('Something went wrong: ' . $e->getMessage()); } } }