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

77 lines
2.9 KiB
PHP

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