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