AMS_Backend/app/Controllers/CustomerDetailsController.php
2025-11-06 13:41:06 +08:00

80 lines
3.0 KiB
PHP

<?php
namespace App\Controllers;
use App\Models\CustomerDetails;
use App\Models\Platform;
use CodeIgniter\Database\Config;
use CodeIgniter\RESTful\ResourceController;
class CustomerDetailsController extends ResourceController
{
private $db;
private $customerDetails;
private $platform;
public function __construct()
{
$this->customerDetails = new CustomerDetails();
$this->platform = new Platform();
$this->db = Config::connect();
}
public function index()
{
try {
$data = $this->customerDetails->findAll();
if (!empty($data)) return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $data], 200);
return $this->respond(['status' => 200, 'message' => 'No customers found'], 200);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function show($id = null)
{
try {
$item = $this->customerDetails->find($id);
if ($item) return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $item], 200);
return $this->respond(['status' => 404, 'message' => 'Customer not found'], 404);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function create()
{
try {
$payload = $this->request->getJSON(true);
$id = $this->customerDetails->insert($payload);
if ($id) return $this->respond(['status' => 201, 'message' => 'Customer created', 'result' => ['id' => $id]], 201);
return $this->respond(['status' => 400, 'message' => 'Failed to create Customer'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function update($id = null)
{
try {
if (!$this->customerDetails->find($id)) return $this->respond(['status' => 404, 'message' => 'Customer not found'], 404);
$payload = $this->request->getJSON(true);
if ($this->customerDetails->update($id, $payload)) return $this->respond(['status' => 200, 'message' => 'Customer updated'], 200);
return $this->respond(['status' => 400, 'message' => 'Failed to update Customer'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function delete($id = null)
{
try {
if (!$this->customerDetails->find($id)) return $this->respond(['status' => 404, 'message' => 'Customer not found'], 404);
if ($this->customerDetails->delete($id)) return $this->respond(['status' => 200, 'message' => 'Customer deleted'], 200);
return $this->respond(['status' => 400, 'message' => 'Failed to delete Customer'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
}