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

79 lines
2.9 KiB
PHP

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