diff --git a/app/Controllers/Location.php b/app/Controllers/Location.php index 966c610..fe447dd 100644 --- a/app/Controllers/Location.php +++ b/app/Controllers/Location.php @@ -3,7 +3,6 @@ namespace App\Controllers; use CodeIgniter\API\ResponseTrait; use CodeIgniter\Controller; -use CodeIgniter\Database\RawSql; class Location extends Controller { use ResponseTrait; diff --git a/app/Controllers/Occupation.php b/app/Controllers/Occupation.php index ae50f92..bd274b0 100644 --- a/app/Controllers/Occupation.php +++ b/app/Controllers/Occupation.php @@ -3,11 +3,14 @@ namespace App\Controllers; use CodeIgniter\API\ResponseTrait; use CodeIgniter\Controller; -use CodeIgniter\Database\RawSql; +use App\Models\OccupationModel; class Occupation extends Controller { use ResponseTrait; + protected $db; + protected $rulesOccupation; + public function __construct() { $this->db = \Config\Database::connect(); $this->rulesOccupation = [ @@ -17,7 +20,8 @@ class Occupation extends Controller { } public function index() { - $rows = $this->db->table('occupation')->select("*")->get()->getResultArray(); + $model = new OccupationModel(); + $rows = $model->get()->getResultArray(); if (empty($rows)) { return $this->respond([ @@ -35,7 +39,9 @@ class Occupation extends Controller { } public function show($OccupationID = null) { - $rows=$this->db->table('occupation')->select("*")->where('occupationID', (int) $OccupationID)->get()->getResultArray(); + $model = new OccupationModel(); + $rows = $model->where('occupationID', (int) $OccupationID)->get()->getResultArray(); + //$rows=$this->db->table('occupation')->select("*")->where('occupationID', (int) $OccupationID)->get()->getResultArray(); if (empty($rows)) { return $this->respond([ diff --git a/app/Database/Seeds/DummySeeder.php b/app/Database/Seeds/DummySeeder.php index 3c55d50..183f061 100644 --- a/app/Database/Seeds/DummySeeder.php +++ b/app/Database/Seeds/DummySeeder.php @@ -35,10 +35,11 @@ class DummySeeder extends Seeder { ]; $this->db->table('contactdetail')->insertBatch($data); $data = [ - ['OccupationID'=>1, 'AbbTex'=>'OC0001', 'FullText'=>'Medical Doctor', 'Description'=>'Diagnoses and treats, injuries and illnesses' ], - ['OccupationID'=>2, 'AbbTex'=>'OC0002', 'FullText'=>'Trainee Medical Technician', 'Description'=>'Performing basic laboratory task' ], - ['OccupationID'=>3, 'AbbTex'=>'OC0003', 'FullText'=>'Medical Laboratory Technician', 'Description'=>'Perform routine laboratory tests' ] + ['OccupationID'=>1, 'AbbTex'=>'OC001', 'FullText'=>'Medical Doctor', 'Description'=>'Diagnoses and treats, injuries and illnesses' ], + ['OccupationID'=>2, 'AbbTex'=>'OC002', 'FullText'=>'Trainee Medical Technician', 'Description'=>'Performing basic laboratory task' ], + ['OccupationID'=>3, 'AbbTex'=>'OC003', 'FullText'=>'Medical Laboratory Technician', 'Description'=>'Perform routine laboratory tests' ] ]; + $this->db->table('occupation')->insertBatch($data); // patient $data = [ diff --git a/app/Models/OccupationModel.php b/app/Models/OccupationModel.php index 6863259..2223a48 100644 --- a/app/Models/OccupationModel.php +++ b/app/Models/OccupationModel.php @@ -4,110 +4,8 @@ namespace App\Models; use CodeIgniter\Model; -class ContactModel extends Model { +class OccupationModel extends Model { protected $table = 'occupation'; protected $primaryKey = 'OccupationID'; protected $allowedFields = ['AbbTex', 'FullText', 'Description']; - - public function getContactsWithDetail() { - $rows = $this->select("contact.ContactID, cd.SiteID, cd.ContactCode, NameFirst, NameLast, Specialty") - ->join("contactdetail cd", "contact.ContactID=cd.ContactID", "left") - ->join("occupation o","cd.OccupationID=o.OccupationID", "left") - ->get()->getResultArray(); - - return $rows; - } - - public function getContacts($ContactName, $Specialty) { - $sql = $this->select("ContactID, NameFirst, NameLast, Title, Initial, Specialty"); - if($ContactName !='') { $sql->like('NameFirst',$ContactName,'both')->orLike('NameLast',$ContactName,'both'); } - if($Specialty != '') { $sql->like('Specialty',$Specialty); } - $rows = $sql->get()->getResultArray(); - return $rows; - } - - public function getContactWithDetail($ContactID) { - $rows = $this->where('contact.ContactID', $ContactID)->join('contactdetail cd', 'contact.ContactID=cd.ContactID','left')->get()->getResultArray(); - $contact = []; - - foreach ($rows as $row) { - if(empty($contact['NameFirst'])) { - $contact = [ - 'ContactID' => $ContactID, - 'NameFirst' => $row['NameFirst'] ?? null, - 'NameLast' => $row['NameLast'] ?? null, - 'Title' => $row['Title'] ?? null, - 'Initial' => $row['Initial'] ?? null, - 'Birthdate' => $row['Birthdate'] ?? null, - 'EmailAddress1' => $row['EmailAddress1'] ?? null, - 'EmailAddress2' => $row['EmailAddress2'] ?? null, - 'Phone' => $row['Phone'] ?? null, - 'MobilePhone1' => $row['MobilePhone1'] ?? null, - 'MobilePhone2' => $row['MobilePhone2'] ?? null, - 'Specialty' => $row['Specialty'] ?? null, - 'SubSpecialty' => $row['SubSpecialty'] ?? null, - 'Details' => [] - ]; - } - if (!empty($row['ContactDetID'])) { - $contact['Details'][] = [ - 'SiteID' => $row['SiteID'] ?? null, - 'ContactDetID' => $row['ContactDetID'], - 'ContactCode' => $row['ContactCode'] ?? null, - 'ContactEmail' => $row['ContactEmail'] ?? null, - 'OccupationID' => $row['OccupationID'] ?? null, - 'JobTitle' => $row['JobTitle'] ?? null, - 'Department' => $row['Department'] ?? null, - 'ContactStartDate' => $row['ContactStartDate'] ?? null, - 'ContactEndDate' => $row['ContactEndDate'] ?? null - ]; - } - } - - return $contact; - } - - public function saveWithDetails(array $data): array { - $db = \Config\Database::connect(); - $db->transStart(); - - try { - if (!empty($data['ContactID'])) { - $contactId = $data['ContactID']; - $this->update($contactId, $data); - } else { - $contactId = $this->insert($data, true); - } - - if (!$contactId) { - throw new \RuntimeException('Failed to save contact'); - } - - if (!empty($data['Details'])) { - $detailModel = new \App\Models\ContactDetailModel(); - $result = $detailModel->syncDetails($contactId, $data['Details']); - - if ($result['status'] !== 'success') { - throw new \RuntimeException('SyncDetails failed: ' . $result['message']); - } - } - - $db->transComplete(); - - return [ - 'status' => 'success', - 'ContactID' => $contactId, - ]; - } catch (\Throwable $e) { - $db->transRollback(); - - log_message('error', 'saveWithDetails error: ' . $e->getMessage()); - - return [ - 'status' => 'error', - 'message' => $e->getMessage(), - ]; - } - } - }