nhance_partner_be/app/Controllers/StaffController.php
2025-09-01 18:01:41 +05:30

86 lines
2.2 KiB
PHP

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