diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 388e9c9..26bc1ed 100644 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -53,7 +53,13 @@ $routes->get('/api/location', 'Location::index'); $routes->get('/api/location/(:num)', 'Location::show/$1'); $routes->post('/api/location', 'Location::create'); $routes->patch('/api/location', 'Location::update'); -$routes->delete('/api/location', 'Patient::delete'); +$routes->delete('/api/location', 'Location::delete'); + +$routes->get('/api/contact', 'Contact::index'); +$routes->get('/api/contact/(:num)', 'Contact::show/$1'); +$routes->post('/api/contact', 'Contact::create'); +$routes->patch('/api/contact', 'Contact::update'); +$routes->delete('/api/contact', 'Contact::delete'); $routes->get('/api/valueset', 'ValueSet::index'); $routes->get('/api/valueset/(:num)', 'ValueSet::show/$1'); diff --git a/app/Controllers/Contact.php b/app/Controllers/Contact.php index bdb9413..e940851 100644 --- a/app/Controllers/Contact.php +++ b/app/Controllers/Contact.php @@ -16,17 +16,17 @@ class Contact extends Controller { } public function index() { - $rows = $this->db->table('contact') - ->select("contact.*, contactdetail.ContactEmail, contactdetail.JobTitle, contactdetail.Department, occupation.AbbTex, occupation.FullText, occupation.Description") - ->join("contactdetail", "contact.ContactID=contactdetail.ContactID") - ->join("occupation","occupation.OccupationID=contactdetail.OccupationID") - ->get()->getRowArray(); + $sql = $this->db->table('contact'); + $sql->select("*") + ->join("contactdetail", "contact.ContactID=contactdetail.ContactID", "left") + ->join("occupation","contactdetail.OccupationID=occupation.OccupationID", "left"); + $rows = $sql->get()->getRowArray(); if (empty($rows)) { return $this->respond([ 'status' => 'success', 'message' => "no Data.", - 'data' => [], + 'data' => $rows, ], 200); } @@ -38,10 +38,10 @@ class Contact extends Controller { } public function show($ContactID = null) { - $row=$this->db->table('contact') - ->select("contact.*, contactdetail.ContactEmail, contactdetail.JobTitle, contactdetail.Department, occupation.AbbTex, occupation.FullText, occupation.Description") - ->join("contactdetail", "contact.ContactID=contactdetail.ContactID") - ->join("occupation","occupation.OccupationID=contactdetail.OccupationID") + $rows=$this->db->table('contact') + ->select("*") + ->join("contactdetail", "contact.ContactID=contactdetail.ContactID", "left") + ->join("occupation","occupation.OccupationID=contactdetail.OccupationID", "left") ->where('contact.ContactID', (int) $ContactID) ->get()->getRowArray(); @@ -65,40 +65,38 @@ class Contact extends Controller { $input = $this->request->getJSON(true); // Prepare data - $dataLocation = $this->prepareLocationData($input); - $dataLocationAddress = $this->prepareLocationAddressData($input); + $dataContact = $this->prepareContactData($input); + $dataContactDetail = $this->prepareContactDetailData($input); - if (!$this->validateData($dataLocation, $this->rules)) { + if (!$this->validateData($dataContact, $this->rulesContact)) { return $this->failValidationErrors($this->validator->getErrors()); } // Start transaction $this->db->transStart(); + $this->db->table('contact')->insert($dataContact); + $newContactID = $this->db->insertID(); - // Insert location - $this->db->table('location')->insert($dataLocation); - $newLocationID = $this->db->insertID(); - - // Insert address if available - if (!empty($dataLocationAddress)) { - $dataLocationAddress['LocationID'] = $newLocationID; - $this->db->table('locationaddress')->insert($dataLocationAddress); + + if (!empty($dataContactDetail)) { + $dataContactDetail['ContactID'] = $newContactID; + $this->db->table('contactdetail')->insert($dataContactDetail); } - // Complete transaction - $this->db->transComplete(); - if ($this->db->transStatus() === false) { $dbError = $this->db->error(); return $this->failServerError( - 'Failed to create location data (transaction rolled back): ' . ($dbError['message'] ?? 'Unknown database error') + 'Failed to create data (transaction rolled back): ' . ( $dbError['message'] ?? 'Unknown database error') ); } + + // Complete transaction + $this->db->transComplete(); return $this->respondCreated([ 'status' => 'success', - 'message' => 'Location created successfully', - 'data' => $dataLocation, + 'message' => 'Contact created successfully', + 'data' => $dataContact, ], 201); } catch (\Throwable $e) { @@ -115,23 +113,22 @@ class Contact extends Controller { $input = $this->request->getJSON(true); // Prepare data - $dataLocation = $this->prepareLocationData($input); - $dataLocationAddress = $this->prepareLocationAddressData($input); + $dataContact = $this->prepareContactData($input); + $dataContactDetail = $this->prepareContactDetailData($input); - if (!$this->validateData($dataLocation, $this->rules)) { + if (!$this->validateData($dataContact, $this->rulesContact)) { return $this->failValidationErrors( $this->validator->getErrors()); } // Start transaction $this->db->transStart(); - // Insert location - $this->db->table('location')->where('LocationID', $dataLocation["LocationID"])->update($dataLocation); + $this->db->table('contact')->where('ContactID', $dataContact["ContactID"])->update($dataContact); // Insert address if available - if (!empty($dataLocationAddress)) { - $dataLocationAddress['LocationID'] = $input["LocationID"]; - $this->db->table('locationaddress')->upsert($dataLocationAddress); + if (!empty($dataContactDetail)) { + $dataContactDetail['ContactID'] = $input["ContactID"]; + $this->db->table('contactdetail')->upsert($dataContactDetail); } // Complete transaction @@ -140,14 +137,14 @@ class Contact extends Controller { if ($this->db->transStatus() === false) { $dbError = $this->db->error(); return $this->failServerError( - 'Failed to update location data (transaction rolled back): ' . ($dbError['message'] ?? 'Unknown database error') + 'Failed to update contact data (transaction rolled back): ' . ($dbError['message'] ?? 'Unknown database error') ); } return $this->respondCreated([ 'status' => 'success', - 'message' => 'Location updated successfully', - 'data' => $dataLocation, + 'message' => 'Contact updated successfully', + 'data' => $dataContact, ], 201); } catch (\Throwable $e) { @@ -162,22 +159,21 @@ class Contact extends Controller { public function delete() { try { $input = $this->request->getJSON(true); - $LocationID = $input["LocationID"]; - if (!$LocationID) { - return $this->failValidationError('LocationID is required.'); + $ContactID = $input["ContactID"]; + if (!$ContactID) { + return $this->failValidationError('ContactID is required.'); } - - $location = $this->db->table('location')->where('LocationID', $LocationID)->get()->getRow(); - if (!$location) { - return $this->failNotFound("LocationID with {$LocationID} not found."); + $contact = $this->db->table('contact')->where('ContactID', $ContactID)->get()->getRow(); + if (!$contact) { + return $this->failNotFound("data with {$ContactID} not found."); } - $this->db->table('location')->where('LocationID', $LocationID)->update(['DelDate' => NOW()]); + $this->db->table('contact')->where('ContactID', $ContactID)->update(['EndDate' => NOW()]); return $this->respondDeleted([ 'status' => 'success', - 'message' => "Location with {$LocationID} deleted successfully." + 'message' => "Contact with {$ContactID} deleted successfully." ]); } catch (\Throwable $e) { @@ -189,35 +185,36 @@ class Contact extends Controller { } } - private function prepareLocationData(array $input): array { - $LinkTo = null; - if (!empty($input['LinkTo'])) { - $ids = array_column($input['LinkTo'], 'InternalPID'); - $LinkTo = implode(',', $ids); - } - + private function prepareContactData(array $input): array { $data = [ - "LocCode" => $input['LocCode'] ?? null, - "Parent" => $input['Parent'] ?? null, - "LocFull" => $input['LocFull'] ?? null, - "Description" => $input['Description'] ?? null, + "NameFirst" => $input['NameFirst'] ?? null, + "NameLast" => $input['NameLast'] ?? null, + "Title" => $input['Title'] ?? null, + "Initial" => $input['Initial'] ?? null, + "Birthdate" => $input['Birthdate'] ?? null, + "EmailAddress1" => $input['EmailAddress1'] ?? null, + "EmailAddress2" => $input['EmailAddress2'] ?? null, + "Phone" => $input["Phone"] ?? null, + "MobilePhone1" => $input["MobilePhone1"] ?? null, + "MobilePhone2" => $input["MobilePhone2"] ?? null, + "Specialty" => $input["Specialty"] ?? null, + "SubSpecialty" => $input["SubSpecialty"] ?? null, ]; - if(!empty($input["LocationID"])) { $data["LocationID"] = $input["LocationID"]; } + if(!empty($input["ContactID"])) { $data["ContactID"] = $input["ContactID"]; } return $data; } - private function prepareLocationAddressData(array $input): array { + private function prepareContactDetailData(array $input): array { $data = [ - "LocationID" => $input['LocationID'] ?? null, - "Street1" => $input['Street1'] ?? null, - "Street2" => $input['Street2'] ?? null, - "City" => $input['City'] ?? null, - "Province" => $input['Province'] ?? null, - "PostCode" => $input['PostCode'] ?? null, - "GeoLocationSystem" => $input['GeoLocationSystem'] ?? null, - "GeoLocationData" => $input['GeoLocationData'] ?? null, + "Code" => $input['Code'] ?? null, + "ContactEmail" => $input['ContactEmail'] ?? null, + "OccupationID" => $input['OccupationID'] ?? null, + "JobTitle" => $input['JobTitle'] ?? null, + "Department" => $input['Department'] ?? null, + "ContactStartDate" => $input['ContactStartDate'] ?? null, + "ContactEndDate" => $input['ContactEndDate'] ?? null, ]; return $data; diff --git a/app/Database/Migrations/2025-09-12-011643_Contact.php b/app/Database/Migrations/2025-09-12-011643_Contact.php index d85d5a6..3b6eb45 100644 --- a/app/Database/Migrations/2025-09-12-011643_Contact.php +++ b/app/Database/Migrations/2025-09-12-011643_Contact.php @@ -8,7 +8,7 @@ class CreateContactTable extends Migration { public function up() { // Contact $this->forge->addField([ - 'ContactID' => [ 'type' => 'INT', 'constraint' => 11 ], + 'ContactID' => [ 'type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true ], 'NameFirst' => [ 'type' => 'VARCHAR', 'constraint' => 100, 'null' => true ], 'NameLast' => [ 'type' => 'VARCHAR', 'constraint' => 100, 'null' => true ], 'Title' => [ 'type' => 'VARCHAR', 'constraint' => 50, 'null' => true ], @@ -32,6 +32,7 @@ class CreateContactTable extends Migration { 'ContactDetID' => [ 'type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true ], 'ContactID' => [ 'type' => 'INT', 'constraint' => 11 ], 'SiteID' => [ 'type' => 'INT', 'constraint' => 11, 'null' => true ], + 'Code' => [ 'type' => 'varchar', 'constraint' => 11, 'null' => false ], 'ContactEmail' => [ 'type' => 'VARCHAR', 'constraint' => 150, 'null' => true ], 'OccupationID' => [ 'type' => 'VARCHAR', 'constraint' => 50, 'null' => true ], 'JobTitle' => [ 'type' => 'VARCHAR', 'constraint' => 100, 'null' => true ],