add contact endpoint
This commit is contained in:
parent
8f30704a2b
commit
7e86462b4f
@ -53,7 +53,13 @@ $routes->get('/api/location', 'Location::index');
|
|||||||
$routes->get('/api/location/(:num)', 'Location::show/$1');
|
$routes->get('/api/location/(:num)', 'Location::show/$1');
|
||||||
$routes->post('/api/location', 'Location::create');
|
$routes->post('/api/location', 'Location::create');
|
||||||
$routes->patch('/api/location', 'Location::update');
|
$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', 'ValueSet::index');
|
||||||
$routes->get('/api/valueset/(:num)', 'ValueSet::show/$1');
|
$routes->get('/api/valueset/(:num)', 'ValueSet::show/$1');
|
||||||
|
|||||||
@ -16,17 +16,17 @@ class Contact extends Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function index() {
|
public function index() {
|
||||||
$rows = $this->db->table('contact')
|
$sql = $this->db->table('contact');
|
||||||
->select("contact.*, contactdetail.ContactEmail, contactdetail.JobTitle, contactdetail.Department, occupation.AbbTex, occupation.FullText, occupation.Description")
|
$sql->select("*")
|
||||||
->join("contactdetail", "contact.ContactID=contactdetail.ContactID")
|
->join("contactdetail", "contact.ContactID=contactdetail.ContactID", "left")
|
||||||
->join("occupation","occupation.OccupationID=contactdetail.OccupationID")
|
->join("occupation","contactdetail.OccupationID=occupation.OccupationID", "left");
|
||||||
->get()->getRowArray();
|
$rows = $sql->get()->getRowArray();
|
||||||
|
|
||||||
if (empty($rows)) {
|
if (empty($rows)) {
|
||||||
return $this->respond([
|
return $this->respond([
|
||||||
'status' => 'success',
|
'status' => 'success',
|
||||||
'message' => "no Data.",
|
'message' => "no Data.",
|
||||||
'data' => [],
|
'data' => $rows,
|
||||||
], 200);
|
], 200);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,10 +38,10 @@ class Contact extends Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function show($ContactID = null) {
|
public function show($ContactID = null) {
|
||||||
$row=$this->db->table('contact')
|
$rows=$this->db->table('contact')
|
||||||
->select("contact.*, contactdetail.ContactEmail, contactdetail.JobTitle, contactdetail.Department, occupation.AbbTex, occupation.FullText, occupation.Description")
|
->select("*")
|
||||||
->join("contactdetail", "contact.ContactID=contactdetail.ContactID")
|
->join("contactdetail", "contact.ContactID=contactdetail.ContactID", "left")
|
||||||
->join("occupation","occupation.OccupationID=contactdetail.OccupationID")
|
->join("occupation","occupation.OccupationID=contactdetail.OccupationID", "left")
|
||||||
->where('contact.ContactID', (int) $ContactID)
|
->where('contact.ContactID', (int) $ContactID)
|
||||||
->get()->getRowArray();
|
->get()->getRowArray();
|
||||||
|
|
||||||
@ -65,40 +65,38 @@ class Contact extends Controller {
|
|||||||
$input = $this->request->getJSON(true);
|
$input = $this->request->getJSON(true);
|
||||||
|
|
||||||
// Prepare data
|
// Prepare data
|
||||||
$dataLocation = $this->prepareLocationData($input);
|
$dataContact = $this->prepareContactData($input);
|
||||||
$dataLocationAddress = $this->prepareLocationAddressData($input);
|
$dataContactDetail = $this->prepareContactDetailData($input);
|
||||||
|
|
||||||
if (!$this->validateData($dataLocation, $this->rules)) {
|
if (!$this->validateData($dataContact, $this->rulesContact)) {
|
||||||
return $this->failValidationErrors($this->validator->getErrors());
|
return $this->failValidationErrors($this->validator->getErrors());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start transaction
|
// Start transaction
|
||||||
$this->db->transStart();
|
$this->db->transStart();
|
||||||
|
$this->db->table('contact')->insert($dataContact);
|
||||||
|
$newContactID = $this->db->insertID();
|
||||||
|
|
||||||
// Insert location
|
|
||||||
$this->db->table('location')->insert($dataLocation);
|
if (!empty($dataContactDetail)) {
|
||||||
$newLocationID = $this->db->insertID();
|
$dataContactDetail['ContactID'] = $newContactID;
|
||||||
|
$this->db->table('contactdetail')->insert($dataContactDetail);
|
||||||
// Insert address if available
|
|
||||||
if (!empty($dataLocationAddress)) {
|
|
||||||
$dataLocationAddress['LocationID'] = $newLocationID;
|
|
||||||
$this->db->table('locationaddress')->insert($dataLocationAddress);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Complete transaction
|
|
||||||
$this->db->transComplete();
|
|
||||||
|
|
||||||
if ($this->db->transStatus() === false) {
|
if ($this->db->transStatus() === false) {
|
||||||
$dbError = $this->db->error();
|
$dbError = $this->db->error();
|
||||||
return $this->failServerError(
|
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([
|
return $this->respondCreated([
|
||||||
'status' => 'success',
|
'status' => 'success',
|
||||||
'message' => 'Location created successfully',
|
'message' => 'Contact created successfully',
|
||||||
'data' => $dataLocation,
|
'data' => $dataContact,
|
||||||
], 201);
|
], 201);
|
||||||
|
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
@ -115,23 +113,22 @@ class Contact extends Controller {
|
|||||||
$input = $this->request->getJSON(true);
|
$input = $this->request->getJSON(true);
|
||||||
|
|
||||||
// Prepare data
|
// Prepare data
|
||||||
$dataLocation = $this->prepareLocationData($input);
|
$dataContact = $this->prepareContactData($input);
|
||||||
$dataLocationAddress = $this->prepareLocationAddressData($input);
|
$dataContactDetail = $this->prepareContactDetailData($input);
|
||||||
|
|
||||||
if (!$this->validateData($dataLocation, $this->rules)) {
|
if (!$this->validateData($dataContact, $this->rulesContact)) {
|
||||||
return $this->failValidationErrors( $this->validator->getErrors());
|
return $this->failValidationErrors( $this->validator->getErrors());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start transaction
|
// Start transaction
|
||||||
$this->db->transStart();
|
$this->db->transStart();
|
||||||
|
|
||||||
// Insert location
|
$this->db->table('contact')->where('ContactID', $dataContact["ContactID"])->update($dataContact);
|
||||||
$this->db->table('location')->where('LocationID', $dataLocation["LocationID"])->update($dataLocation);
|
|
||||||
|
|
||||||
// Insert address if available
|
// Insert address if available
|
||||||
if (!empty($dataLocationAddress)) {
|
if (!empty($dataContactDetail)) {
|
||||||
$dataLocationAddress['LocationID'] = $input["LocationID"];
|
$dataContactDetail['ContactID'] = $input["ContactID"];
|
||||||
$this->db->table('locationaddress')->upsert($dataLocationAddress);
|
$this->db->table('contactdetail')->upsert($dataContactDetail);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Complete transaction
|
// Complete transaction
|
||||||
@ -140,14 +137,14 @@ class Contact extends Controller {
|
|||||||
if ($this->db->transStatus() === false) {
|
if ($this->db->transStatus() === false) {
|
||||||
$dbError = $this->db->error();
|
$dbError = $this->db->error();
|
||||||
return $this->failServerError(
|
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([
|
return $this->respondCreated([
|
||||||
'status' => 'success',
|
'status' => 'success',
|
||||||
'message' => 'Location updated successfully',
|
'message' => 'Contact updated successfully',
|
||||||
'data' => $dataLocation,
|
'data' => $dataContact,
|
||||||
], 201);
|
], 201);
|
||||||
|
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
@ -162,22 +159,21 @@ class Contact extends Controller {
|
|||||||
public function delete() {
|
public function delete() {
|
||||||
try {
|
try {
|
||||||
$input = $this->request->getJSON(true);
|
$input = $this->request->getJSON(true);
|
||||||
$LocationID = $input["LocationID"];
|
$ContactID = $input["ContactID"];
|
||||||
if (!$LocationID) {
|
if (!$ContactID) {
|
||||||
return $this->failValidationError('LocationID is required.');
|
return $this->failValidationError('ContactID is required.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$contact = $this->db->table('contact')->where('ContactID', $ContactID)->get()->getRow();
|
||||||
$location = $this->db->table('location')->where('LocationID', $LocationID)->get()->getRow();
|
if (!$contact) {
|
||||||
if (!$location) {
|
return $this->failNotFound("data with {$ContactID} not found.");
|
||||||
return $this->failNotFound("LocationID with {$LocationID} 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([
|
return $this->respondDeleted([
|
||||||
'status' => 'success',
|
'status' => 'success',
|
||||||
'message' => "Location with {$LocationID} deleted successfully."
|
'message' => "Contact with {$ContactID} deleted successfully."
|
||||||
]);
|
]);
|
||||||
|
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
@ -189,35 +185,36 @@ class Contact extends Controller {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function prepareLocationData(array $input): array {
|
private function prepareContactData(array $input): array {
|
||||||
$LinkTo = null;
|
|
||||||
if (!empty($input['LinkTo'])) {
|
|
||||||
$ids = array_column($input['LinkTo'], 'InternalPID');
|
|
||||||
$LinkTo = implode(',', $ids);
|
|
||||||
}
|
|
||||||
|
|
||||||
$data = [
|
$data = [
|
||||||
"LocCode" => $input['LocCode'] ?? null,
|
"NameFirst" => $input['NameFirst'] ?? null,
|
||||||
"Parent" => $input['Parent'] ?? null,
|
"NameLast" => $input['NameLast'] ?? null,
|
||||||
"LocFull" => $input['LocFull'] ?? null,
|
"Title" => $input['Title'] ?? null,
|
||||||
"Description" => $input['Description'] ?? 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;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function prepareLocationAddressData(array $input): array {
|
private function prepareContactDetailData(array $input): array {
|
||||||
$data = [
|
$data = [
|
||||||
"LocationID" => $input['LocationID'] ?? null,
|
"Code" => $input['Code'] ?? null,
|
||||||
"Street1" => $input['Street1'] ?? null,
|
"ContactEmail" => $input['ContactEmail'] ?? null,
|
||||||
"Street2" => $input['Street2'] ?? null,
|
"OccupationID" => $input['OccupationID'] ?? null,
|
||||||
"City" => $input['City'] ?? null,
|
"JobTitle" => $input['JobTitle'] ?? null,
|
||||||
"Province" => $input['Province'] ?? null,
|
"Department" => $input['Department'] ?? null,
|
||||||
"PostCode" => $input['PostCode'] ?? null,
|
"ContactStartDate" => $input['ContactStartDate'] ?? null,
|
||||||
"GeoLocationSystem" => $input['GeoLocationSystem'] ?? null,
|
"ContactEndDate" => $input['ContactEndDate'] ?? null,
|
||||||
"GeoLocationData" => $input['GeoLocationData'] ?? null,
|
|
||||||
];
|
];
|
||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
|
|||||||
@ -8,7 +8,7 @@ class CreateContactTable extends Migration {
|
|||||||
public function up() {
|
public function up() {
|
||||||
// Contact
|
// Contact
|
||||||
$this->forge->addField([
|
$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 ],
|
'NameFirst' => [ 'type' => 'VARCHAR', 'constraint' => 100, 'null' => true ],
|
||||||
'NameLast' => [ 'type' => 'VARCHAR', 'constraint' => 100, 'null' => true ],
|
'NameLast' => [ 'type' => 'VARCHAR', 'constraint' => 100, 'null' => true ],
|
||||||
'Title' => [ 'type' => 'VARCHAR', 'constraint' => 50, '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 ],
|
'ContactDetID' => [ 'type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true ],
|
||||||
'ContactID' => [ 'type' => 'INT', 'constraint' => 11 ],
|
'ContactID' => [ 'type' => 'INT', 'constraint' => 11 ],
|
||||||
'SiteID' => [ 'type' => 'INT', 'constraint' => 11, 'null' => true ],
|
'SiteID' => [ 'type' => 'INT', 'constraint' => 11, 'null' => true ],
|
||||||
|
'Code' => [ 'type' => 'varchar', 'constraint' => 11, 'null' => false ],
|
||||||
'ContactEmail' => [ 'type' => 'VARCHAR', 'constraint' => 150, 'null' => true ],
|
'ContactEmail' => [ 'type' => 'VARCHAR', 'constraint' => 150, 'null' => true ],
|
||||||
'OccupationID' => [ 'type' => 'VARCHAR', 'constraint' => 50, 'null' => true ],
|
'OccupationID' => [ 'type' => 'VARCHAR', 'constraint' => 50, 'null' => true ],
|
||||||
'JobTitle' => [ 'type' => 'VARCHAR', 'constraint' => 100, 'null' => true ],
|
'JobTitle' => [ 'type' => 'VARCHAR', 'constraint' => 100, 'null' => true ],
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user