clqms-be/app/Controllers/Contact.php
2025-09-26 16:41:55 +07:00

134 lines
4.2 KiB
PHP

<?php
namespace App\Controllers;
use CodeIgniter\API\ResponseTrait;
use CodeIgniter\Controller;
use App\Models\ContactModel;
use App\Models\ContactDetailModel;
class Contact extends Controller {
use ResponseTrait;
protected $db;
protected $contactRule;
public function __construct() {
$this->db = \Config\Database::connect();
$this->contactRule = [ 'NameFirst' => 'required' ];
}
public function index() {
$ContactName = $this->request->getVar('ContactName');
$Specialty = $this->request->getVar('Specialty');
$model = new ContactModel();
$rows = $model->getContacts($ContactName, $Specialty);
//$rows = $model->getContacts();
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($ContactID = null) {
$model = new ContactModel();
$rows = $model->getContactWithDetail($ContactID);
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 delete() {
try {
$input = $this->request->getJSON(true);
$ContactID = $input["ContactID"];
if (!$ContactID) {
return $this->failValidationErrors('ContactID is required.');
}
$contact = $this->db->table('contact')->where('ContactID', $ContactID)->get()->getRow();
if (!$contact) {
return $this->failNotFound("data with {$ContactID} not found.");
}
$this->db->table('contact')->where('ContactID', $ContactID)->update(['EndDate' => NOW()]);
return $this->respondDeleted([
'status' => 'success',
'message' => "Contact with {$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());
}
}
public function save() {
$input = $this->request->getJSON(true);
$contactModel = new ContactModel();
$detailModel = new ContactDetailModel();
$db = \Config\Database::connect();
$db->transStart();
try {
if (!empty($input['ContactID'])) {
$ContactID = $input['ContactID'];
if (!$contactModel->update($ContactID, $input)) { throw new \RuntimeException('Failed to update contact'); }
} else {
$ContactID = $contactModel->insert($input, true);
if (!$ContactID) { throw new \RuntimeException('Failed to insert contact'); }
}
if(isset($input['Details'])) {
$result = $detailModel->syncDetails($ContactID, $input['Details']);
if ($result['status'] !== 'success') {
throw new \RuntimeException('Failed to sync details: ' . $result['message']);
}
}
$db->transComplete();
if ($db->transStatus() === false) {
throw new \RuntimeException('Transaction failed');
}
return $this->respondCreated([
'status' => 'success',
'ContactID' => $ContactID,
]);
} catch (\Throwable $e) {
$db->transRollback();
log_message('error', 'saveContact error: ' . $e->getMessage());
return $this->fail([
'status' => 'error',
'message' => $e->getMessage(),
], 500);
}
}
}