initial project
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controllers;
|
||||
|
||||
use App\Models\BranchDetails;
|
||||
use App\Models\StaffDetails;
|
||||
use CodeIgniter\Database\Config;
|
||||
use CodeIgniter\RESTful\ResourceController;
|
||||
|
||||
class StaffDetailsController extends ResourceController
|
||||
{
|
||||
private $db;
|
||||
private $staffDetails;
|
||||
private $position;
|
||||
private $branchDetails;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->staffDetails = new StaffDetails();
|
||||
$this->branchDetails = new BranchDetails();
|
||||
$this->db = Config::connect();
|
||||
}
|
||||
|
||||
public function index()
|
||||
{
|
||||
try {
|
||||
$data = $this->staffDetails->findAll();
|
||||
if (!empty($data)) return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $data], 200);
|
||||
return $this->respond(['status' => 200, 'message' => 'No staff records found'], 200);
|
||||
} catch (\Exception $e) {
|
||||
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
|
||||
}
|
||||
}
|
||||
|
||||
public function show($id = null)
|
||||
{
|
||||
try {
|
||||
$item = $this->staffDetails->find($id);
|
||||
if ($item) return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $item], 200);
|
||||
return $this->respond(['status' => 404, 'message' => 'Staff not found'], 404);
|
||||
} catch (\Exception $e) {
|
||||
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
|
||||
}
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
$validationRules = [
|
||||
'employee_name' => 'required',
|
||||
'email' => 'required',
|
||||
'phone_no' => 'required',
|
||||
'position_id' => 'required',
|
||||
'username' => 'required',
|
||||
'password' => 'required',
|
||||
'branch_id' => 'required',
|
||||
];
|
||||
|
||||
if (!$this->validate($validationRules)) {
|
||||
$response = [
|
||||
'status' => 'error',
|
||||
'message' => 'Validation failed.',
|
||||
'data' => $this->validator->getErrors()
|
||||
];
|
||||
return $this->respond($response, 422);
|
||||
}
|
||||
|
||||
$userData = [
|
||||
'employee_name' => $this->request->getVar('employee_name'),
|
||||
'email' => $this->request->getVar('email'),
|
||||
'phone_no' => $this->request->getVar('phone_no'),
|
||||
'position_id' => $this->request->getVar('position_id'),
|
||||
'username' => $this->request->getVar('username'),
|
||||
'password' => md5($this->request->getVar('password')),
|
||||
'branch_id' => $this->request->getVar('branch_id'),
|
||||
];
|
||||
|
||||
$id = $this->staffDetails->insert($userData);
|
||||
|
||||
if ($id) {
|
||||
$result = $this->staffDetails->find($id);
|
||||
|
||||
$response = [
|
||||
'status' => 'success',
|
||||
'message' => 'Staff created successfully.',
|
||||
'data' => $result
|
||||
];
|
||||
return $this->respond($response, 201);
|
||||
}
|
||||
|
||||
$response = [
|
||||
'status' => 'error',
|
||||
'message' => 'Failed to create Staff.',
|
||||
'data' => null
|
||||
];
|
||||
return $this->respond($response, 500);
|
||||
}
|
||||
|
||||
public function update($id = null)
|
||||
{
|
||||
try {
|
||||
if (!$this->staffDetails->find($id)) return $this->respond(['status' => 404, 'message' => 'Staff not found'], 404);
|
||||
$payload = $this->request->getJSON(true);
|
||||
if ($this->staffDetails->update($id, $payload)) return $this->respond(['status' => 200, 'message' => 'Staff updated'], 200);
|
||||
return $this->respond(['status' => 400, 'message' => 'Failed to update Staff'], 400);
|
||||
} catch (\Exception $e) {
|
||||
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
|
||||
}
|
||||
}
|
||||
|
||||
public function delete($id = null)
|
||||
{
|
||||
try {
|
||||
if (!$this->staffDetails->find($id)) return $this->respond(['status' => 404, 'message' => 'Staff not found'], 404);
|
||||
if ($this->staffDetails->delete($id)) return $this->respond(['status' => 200, 'message' => 'Staff deleted'], 200);
|
||||
return $this->respond(['status' => 400, 'message' => 'Failed to delete Staff'], 400);
|
||||
} catch (\Exception $e) {
|
||||
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user