clqms-be/app/Controllers/Contact.php

131 lines
4.0 KiB
PHP
Raw Normal View History

2025-09-17 15:50:55 +07:00
<?php
namespace App\Controllers;
use CodeIgniter\API\ResponseTrait;
use CodeIgniter\Controller;
2025-09-24 16:15:55 +07:00
use App\Models\ContactModel;
2025-09-25 13:30:15 +07:00
use App\Models\ContactDetailModel;
2025-09-17 15:50:55 +07:00
class Contact extends Controller {
use ResponseTrait;
2025-09-25 13:30:15 +07:00
protected $db;
2025-09-24 16:15:55 +07:00
protected $contactRule;
2025-09-17 15:50:55 +07:00
public function __construct() {
2025-09-25 13:30:15 +07:00
$this->db = \Config\Database::connect();
2025-09-24 16:15:55 +07:00
$this->contactRule = [ 'NameFirst' => 'required' ];
2025-09-17 15:50:55 +07:00
}
public function index() {
2025-09-24 16:15:55 +07:00
$model = new ContactModel();
2025-09-25 16:43:58 +07:00
$rows = $model->getContacts();
2025-09-17 15:50:55 +07:00
if (empty($rows)) {
return $this->respond([
'status' => 'success',
'message' => "no Data.",
2025-09-18 15:52:37 +07:00
'data' => $rows,
2025-09-17 15:50:55 +07:00
], 200);
}
return $this->respond([
'status' => 'success',
'message'=> "fetch success",
'data' => $rows,
], 200);
}
public function show($ContactID = null) {
2025-09-24 16:15:55 +07:00
$model = new ContactModel();
$rows = $model->getContactWithDetail($ContactID);
2025-09-17 15:50:55 +07:00
if (empty($rows)) {
return $this->respond([
'status' => 'success',
'message' => "Data not found.",
'data' => [],
], 200);
}
return $this->respond([
2025-09-24 16:15:55 +07:00
'status' => 'success',
2025-09-17 15:50:55 +07:00
'message'=> "Data fetched successfully",
2025-09-24 16:15:55 +07:00
'data' => $rows,
2025-09-17 15:50:55 +07:00
], 200);
}
public function delete() {
try {
$input = $this->request->getJSON(true);
2025-09-18 15:52:37 +07:00
$ContactID = $input["ContactID"];
if (!$ContactID) {
2025-09-25 13:30:15 +07:00
return $this->failValidationErrors('ContactID is required.');
2025-09-17 15:50:55 +07:00
}
2025-09-25 13:42:37 +07:00
2025-09-18 15:52:37 +07:00
$contact = $this->db->table('contact')->where('ContactID', $ContactID)->get()->getRow();
if (!$contact) {
return $this->failNotFound("data with {$ContactID} not found.");
2025-09-17 15:50:55 +07:00
}
2025-09-18 15:52:37 +07:00
$this->db->table('contact')->where('ContactID', $ContactID)->update(['EndDate' => NOW()]);
2025-09-17 15:50:55 +07:00
return $this->respondDeleted([
'status' => 'success',
2025-09-18 15:52:37 +07:00
'message' => "Contact with {$ContactID} deleted successfully."
2025-09-17 15:50:55 +07:00
]);
} 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());
}
}
2025-09-25 13:30:15 +07:00
public function save() {
$input = $this->request->getJSON(true);
$contactModel = new ContactModel();
$detailModel = new ContactDetailModel();
$db = \Config\Database::connect();
$db->transStart();
2025-09-17 15:50:55 +07:00
2025-09-25 13:30:15 +07:00
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'); }
}
2025-09-26 15:03:11 +07:00
if(isset($input['Details'])) {
$result = $detailModel->syncDetails($ContactID, $input['Details']);
if ($result['status'] !== 'success') {
throw new \RuntimeException('Failed to sync details: ' . $result['message']);
}
2025-09-25 13:30:15 +07:00
}
$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);
2025-09-24 16:15:55 +07:00
}
2025-09-17 15:50:55 +07:00
}
}