feat: initial foundation for collaborative canteen ordering system in vanilla PHP
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controllers;
|
||||
|
||||
use App\Core\Controller;
|
||||
use App\Core\Http\Request;
|
||||
use App\Core\Http\Response;
|
||||
use App\Models\Product;
|
||||
|
||||
class ProductController extends Controller
|
||||
{
|
||||
public function index(Request $request, Response $response): string
|
||||
{
|
||||
$products = Product::all();
|
||||
return $this->render('products/index', [
|
||||
'title' => 'Product Inventory',
|
||||
'products' => $products,
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(Request $request, Response $response): Response
|
||||
{
|
||||
$sku = trim((string)$request->post('sku'));
|
||||
$name = trim((string)$request->post('name'));
|
||||
$price = (float)$request->post('price', 0);
|
||||
$stock = (int)$request->post('stock', 0);
|
||||
$category = trim((string)$request->post('category', 'General'));
|
||||
$description = trim((string)$request->post('description', ''));
|
||||
|
||||
if (empty($sku) || empty($name) || $price <= 0) {
|
||||
return $this->redirect('/products', null, 'Please provide valid SKU, name, and positive price.');
|
||||
}
|
||||
|
||||
try {
|
||||
Product::create([
|
||||
'sku' => $sku,
|
||||
'name' => $name,
|
||||
'price' => $price,
|
||||
'stock' => $stock,
|
||||
'category' => $category,
|
||||
'description' => $description,
|
||||
]);
|
||||
return $this->redirect('/products', 'Product added successfully!');
|
||||
} catch (\Throwable $e) {
|
||||
return $this->redirect('/products', null, 'Error adding product: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user