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

77 lines
3.0 KiB
PHP

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