initial project
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controllers;
|
||||
|
||||
use App\Models\ProductBrand;
|
||||
use CodeIgniter\Database\Config;
|
||||
use CodeIgniter\RESTful\ResourceController;
|
||||
|
||||
class ProductBrandController extends ResourceController
|
||||
{
|
||||
private $db;
|
||||
private $productBrand;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->productBrand = new ProductBrand();
|
||||
$this->db = Config::connect();
|
||||
}
|
||||
|
||||
protected $format = 'json';
|
||||
|
||||
public function index()
|
||||
{
|
||||
try {
|
||||
$data = $this->productBrand->findAll();
|
||||
if (!empty($data)) {
|
||||
return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $data], 200);
|
||||
}
|
||||
return $this->respond(['status' => 200, 'message' => 'No product brands found'], 200);
|
||||
} catch (\Exception $e) {
|
||||
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
|
||||
}
|
||||
}
|
||||
|
||||
public function show($id = null)
|
||||
{
|
||||
try {
|
||||
$item = $this->productBrand->find($id);
|
||||
if ($item) {
|
||||
return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $item], 200);
|
||||
}
|
||||
return $this->respond(['status' => 404, 'message' => 'Product Brand not found'], 404);
|
||||
} catch (\Exception $e) {
|
||||
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
|
||||
}
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
try {
|
||||
$payload = $this->request->getJSON(true);
|
||||
$insertId = $this->productBrand->insert($payload);
|
||||
if ($insertId) {
|
||||
return $this->respond(['status' => 201, 'message' => 'Product Brand created', 'result' => ['id' => $insertId]], 201);
|
||||
}
|
||||
return $this->respond(['status' => 400, 'message' => 'Failed to create Product Brand'], 400);
|
||||
} catch (\Exception $e) {
|
||||
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
|
||||
}
|
||||
}
|
||||
|
||||
public function update($id = null)
|
||||
{
|
||||
try {
|
||||
if (!$this->productBrand->find($id)) {
|
||||
return $this->respond(['status' => 404, 'message' => 'Product Brand not found'], 404);
|
||||
}
|
||||
$payload = $this->request->getJSON(true);
|
||||
$ok = $this->productBrand->update($id, $payload);
|
||||
if ($ok) {
|
||||
return $this->respond(['status' => 200, 'message' => 'Product Brand updated'], 200);
|
||||
}
|
||||
return $this->respond(['status' => 400, 'message' => 'Failed to update Product Brand'], 400);
|
||||
} catch (\Exception $e) {
|
||||
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
|
||||
}
|
||||
}
|
||||
|
||||
public function delete($id = null)
|
||||
{
|
||||
try {
|
||||
if (!$this->productBrand->find($id)) {
|
||||
return $this->respond(['status' => 404, 'message' => 'Product Brand not found'], 404);
|
||||
}
|
||||
$ok = $this->productBrand->delete($id);
|
||||
if ($ok) {
|
||||
return $this->respond(['status' => 200, 'message' => 'Product Brand deleted'], 200);
|
||||
}
|
||||
return $this->respond(['status' => 400, 'message' => 'Failed to delete Product Brand'], 400);
|
||||
} catch (\Exception $e) {
|
||||
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user