This commit is contained in:
mahdahar 2025-09-19 15:22:37 +07:00
commit d28bb3cd4b
9 changed files with 267 additions and 79 deletions

View File

@ -47,7 +47,19 @@ $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/occupation', 'Occupation::index');
$routes->get('/api/occupation/(:num)', 'Occupation::show/$1');
$routes->post('/api/occupation', 'Occupation::create');
$routes->patch('/api/occupation', 'Occupation::update');
//$routes->delete('/api/occupation', 'Occupation::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');

View File

@ -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;

View File

@ -0,0 +1,169 @@
<?php
namespace App\Controllers;
use CodeIgniter\API\ResponseTrait;
use CodeIgniter\Controller;
use CodeIgniter\Database\RawSql;
class Occupation extends Controller {
use ResponseTrait;
public function __construct() {
$this->db = \Config\Database::connect();
$this->rulesOccupation = [
'AbbTex' => 'required',
'FullText' => 'required'
];
}
public function index() {
$rows = $this->db->table('occupation')->select("*")->get()->getRowArray();
if (empty($rows)) {
return $this->respond([
'status' => 'success',
'message' => "no Data.",
'data' => $rows,
], 200);
}
return $this->respond([
'status' => 'success',
'message'=> "fetch success",
'data' => $rows,
], 200);
}
public function show($OccupationID = null) {
$rows=$this->db->table('occupation')->select("*")->where('occupationID', (int) $OccupationID)->get()->getRowArray();
if (empty($rows)) {
return $this->respond([
'status' => 'success',
'message' => "Data not found.",
'data' => [],
], 200);
}
return $this->respond([
'status' => 'success',
'message'=> "Data fetched successfully",
'data' => $rows,
], 200);
}
public function create() {
try {
$input = $this->request->getJSON(true);
$dataOccupation = $this->prepareOccupationData($input);
if (!$this->validateData($dataOccupation, $this->rulesOccupation)) {
return $this->failValidationErrors($this->validator->getErrors());
}
$this->db->transStart();
$this->db->table('contact')->insert($dataOccupation);
if ($this->db->transStatus() === false) {
$dbError = $this->db->error();
return $this->failServerError(
'Failed to create data (transaction rolled back): ' . ( $dbError['message'] ?? 'Unknown database error')
);
}
// Complete transaction
$this->db->transComplete();
return $this->respondCreated([
'status' => 'success',
'message' => 'data created successfully',
'data' => $dataOccupation,
], 201);
} catch (\Throwable $e) {
// Ensure rollback if something goes wrong
if ($this->db->transStatus() !== false) {
$this->db->transRollback();
}
return $this->failServerError('Something went wrong: ' . $e->getMessage());
}
}
public function update() {
try {
$input = $this->request->getJSON(true);
$dataOccupation = $this->prepareOccupationData($input);
if (!$this->validateData($dataOccupation, $this->rulesOccupation)) {
return $this->failValidationErrors( $this->validator->getErrors());
}
$this->db->transStart();
$this->db->table('occupation')->where('OccupationID', $dataOccupation["OccupationID"])->update($dataOccupation);
if ($this->db->transStatus() === false) {
$dbError = $this->db->error();
return $this->failServerError(
'Failed to update data (transaction rolled back): ' . ($dbError['message'] ?? 'Unknown database error')
);
}
$this->db->transComplete();
return $this->respondCreated([
'status' => 'success',
'message' => 'Occupation updated successfully',
'data' => $dataContact,
], 201);
} catch (\Throwable $e) {
// Ensure rollback if something goes wrong
if ($this->db->transStatus() !== false) {
$this->db->transRollback();
}
return $this->failServerError('Something went wrong: ' . $e->getMessage());
}
}
/*
public function delete() {
try {
$input = $this->request->getJSON(true);
$OccupationID = $input["OccupationID"];
if (!$OccupationID) {
return $this->failValidationError('ContactID is required.');
}
$occupation = $this->db->table('occupation')->where('OccupationID', $OccupationID)->get()->getRow();
if (!$occupation) {
return $this->failNotFound("data with {$OccupationID} not found.");
}
$this->db->table('occupation')->where('OccupationID', $OccupationID)->update(['EndDate' => NOW()]);
return $this->respondDeleted([
'status' => 'success',
'message' => "{$ContactID} deleted successfully."
]);
} catch (\Throwable $e) {
// Ensure rollback if something goes wrong
if ($this->db->transStatus() !== false) {
$this->db->transRollback();
}
return $this->failServerError('Something went wrong: ' . $e->getMessage());
}
}
*/
private function prepareOccupationData(array $input): array {
$data = [
"AbbTex" => $input['AbbTex'] ?? null,
"FullText" => $input['FullText'] ?? null,
"Description" => $input['Description'] ?? null,
];
if(!empty($input["OccupationID"])) { $data["OccupationID"] = $input["OccupationID"]; }
return $data;
}
}

View File

@ -17,9 +17,13 @@ class ValueSet extends Controller {
} }
public function index() { public function index() {
$rows = $this->db->table('valueset') $builder = $this->db->table('valueset')->select("*");
->select("*") $param = $this->request->getVar('param');
->get()->getResultArray(); if ($param !== null) {
$builder->like('VValue', $param, 'both');
$builder->orlike('VDesc', $param, 'both');
}
$rows = $builder->get()->getResultArray();
if (empty($rows)) { if (empty($rows)) {
return $this->respond([ return $this->respond([
@ -38,8 +42,9 @@ class ValueSet extends Controller {
public function show($VID = null) { public function show($VID = null) {
$rows = $this->db->table('valueset') $rows = $this->db->table('valueset')
->select("*") ->select("valueset.*, valuesetdef.VSName")
->where('VID', (int) $VID) ->join('valuesetdef', 'valuesetdef.VSetID = valueset.VSetID', 'LEFT')
->where('valueset.VID', (int) $VID)
->get()->getResultArray(); ->get()->getResultArray();
if (empty($rows)) { if (empty($rows)) {

View File

@ -17,10 +17,14 @@ class ValueSetDef extends Controller {
} }
public function index() { public function index() {
$rows = $this->db->table('valuesetdef') $builder = $this->db->table('valuesetdef')->select("*");
->select("*") $param = $this->request->getVar('param');
->get()->getResultArray(); if ($param !== null) {
$builder->like('VSName', $param, 'both');
$builder->orlike('VSDesc', $param, 'both');
}
$rows = $builder->get()->getResultArray();
if (empty($rows)) { if (empty($rows)) {
return $this->respond([ return $this->respond([
'status' => 'success', 'status' => 'success',

View File

@ -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 ],

BIN
public/clqms01.sql.gz Normal file

Binary file not shown.

BIN
public/upload/meow.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

BIN
public/upload/panda.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB