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

101 lines
2.9 KiB
PHP

<?php
namespace App\Controllers\Backend;
use App\Controllers\BaseController;
use App\Models\DeliverySettingModel;
use CodeIgniter\HTTP\ResponseInterface;
class DeliverySetting extends BaseController
{
protected $model;
public function __construct()
{
$this->model = new DeliverySettingModel();
}
public function index()
{
$data = $this->model->findAll();
// $this->model->orderBy('status', 'active');
return $this->response->setJSON([
'status' => 'success',
'data' => $data,
]);
}
public function create()
{
$data = $this->request->getJSON(true) ?? $this->request->getPost();
if (!$this->model->insert($data)) {
return $this->response->setStatusCode(ResponseInterface::HTTP_BAD_REQUEST)
->setJSON([
'status' => 'error',
'errors' => $this->model->errors(),
]);
}
return $this->response->setStatusCode(ResponseInterface::HTTP_CREATED)
->setJSON([
'status' => 'success',
'message' => 'Delivery setting created',
'data' => $this->model->find($this->model->getInsertID()),
]);
}
public function show($id = null)
{
$row = $this->model->find($id);
if (!$row) {
return $this->response->setStatusCode(ResponseInterface::HTTP_NOT_FOUND)
->setJSON(['status' => 'error', 'message' => 'Not found']);
}
return $this->response->setJSON([
'status' => 'success',
'data' => $row,
]);
}
public function update($id = null)
{
$data = $this->request->getJSON(true) ?? $this->request->getRawInput();
if (!$this->model->find($id)) {
return $this->response->setStatusCode(ResponseInterface::HTTP_NOT_FOUND)
->setJSON(['status' => 'error', 'message' => 'Not found']);
}
if (!$this->model->update($id, $data)) {
return $this->response->setStatusCode(ResponseInterface::HTTP_BAD_REQUEST)
->setJSON([
'status' => 'error',
'errors' => $this->model->errors(),
]);
}
return $this->response->setJSON([
'status' => 'success',
'message' => 'Delivery setting updated',
'data' => $this->model->find($id),
]);
}
public function delete($id = null)
{
if (!$this->model->find($id)) {
return $this->response->setStatusCode(ResponseInterface::HTTP_NOT_FOUND)
->setJSON(['status' => 'error', 'message' => 'Not found']);
}
$this->model->delete($id);
return $this->response->setJSON([
'status' => 'success',
'message' => 'Delivery setting deleted',
]);
}
}