86 lines
2.2 KiB
PHP
86 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Controllers\BaseController;
|
|
use App\Models\AgentModel;
|
|
|
|
class PartnerAgentController extends BaseController
|
|
{
|
|
protected $AgentModel;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->AgentModel = new AgentModel();
|
|
}
|
|
|
|
// =========================
|
|
// 1. List all records
|
|
// =========================
|
|
public function list()
|
|
{
|
|
$data = $this->AgentModel->findAll();
|
|
return $this->response->setJSON($data);
|
|
}
|
|
|
|
// =========================
|
|
// 2. Find single record by ID
|
|
// =========================
|
|
public function find($id = null)
|
|
{
|
|
$record = $this->AgentModel->find($id);
|
|
|
|
if (!$record) {
|
|
return $this->response->setJSON([
|
|
'status' => 'error',
|
|
'message' => 'Record not found'
|
|
])->setStatusCode(404);
|
|
}
|
|
|
|
return $this->response->setJSON($record);
|
|
}
|
|
|
|
// =========================
|
|
// 3. Create new record
|
|
// =========================
|
|
public function create()
|
|
{
|
|
$data = $this->request->getJSON(true); // get JSON body as array
|
|
|
|
if (!$this->AgentModel->insert($data)) {
|
|
return $this->response->setJSON([
|
|
'status' => 'error',
|
|
'message' => 'Failed to insert',
|
|
'errors' => $this->AgentModel->errors()
|
|
])->setStatusCode(400);
|
|
}
|
|
|
|
return $this->response->setJSON([
|
|
'status' => 'success',
|
|
'message' => 'Record created successfully',
|
|
'id' => $this->AgentModel->getInsertID()
|
|
]);
|
|
}
|
|
|
|
// =========================
|
|
// 4. Update record by ID
|
|
// =========================
|
|
public function update($id = null)
|
|
{
|
|
$data = $this->request->getJSON(true);
|
|
|
|
if (!$this->AgentModel->update($id, $data)) {
|
|
return $this->response->setJSON([
|
|
'status' => 'error',
|
|
'message' => 'Failed to update',
|
|
'errors' => $this->AgentModel->errors()
|
|
])->setStatusCode(400);
|
|
}
|
|
|
|
return $this->response->setJSON([
|
|
'status' => 'success',
|
|
'message' => 'Record updated successfully'
|
|
]);
|
|
}
|
|
}
|