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