Make 'Product Add' page
This commit is contained in:
@@ -5,7 +5,8 @@ class Request
|
||||
{
|
||||
private $method;
|
||||
private $uri;
|
||||
private $queryString;
|
||||
private $queryParams;
|
||||
private $formParams;
|
||||
|
||||
public function getMethod()
|
||||
{
|
||||
@@ -17,19 +18,25 @@ class Request
|
||||
return $this->uri;
|
||||
}
|
||||
|
||||
public function getQueryString()
|
||||
public function getQueryParams()
|
||||
{
|
||||
return $this->queryString;
|
||||
return $this->queryParams;
|
||||
}
|
||||
|
||||
public function __construct(array $params)
|
||||
public function getFormParams()
|
||||
{
|
||||
$uri_base = trim($params['REQUEST_URI'], '?'.$params['QUERY_STRING']);
|
||||
return $this->formParams;
|
||||
}
|
||||
|
||||
public function __construct(array $params, array $queryParams, array $formParams)
|
||||
{
|
||||
$uri_base = strtok($params['REQUEST_URI'], '?');
|
||||
$uri_base = trim(urldecode($uri_base), '/');
|
||||
$this->uri = explode('/', $uri_base);
|
||||
|
||||
$this->method = $params['REQUEST_METHOD'];
|
||||
|
||||
parse_str($params['QUERY_STRING'], $this->queryString);
|
||||
$this->queryParams = $queryParams;
|
||||
$this->formParams = $formParams;
|
||||
}
|
||||
}
|
||||
|
@@ -20,7 +20,12 @@ class RequestHandler
|
||||
{
|
||||
foreach ($this->routes as $route) {
|
||||
if ($route->matches($this->request)) {
|
||||
$route->execute($this->request);
|
||||
try {
|
||||
$route->execute($this->request);
|
||||
} catch (\Exception $e) {
|
||||
http_response_code(500);
|
||||
echo $e->getMessage();
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@@ -3,21 +3,25 @@ namespace ProductList\View;
|
||||
|
||||
use ProductList\Http\Request;
|
||||
use ProductList\Model\Product as ProductModel;
|
||||
use ProductList\Model\DVD;
|
||||
use ProductList\Model\Furniture;
|
||||
use ProductList\Model\Book;
|
||||
use ProductList\Exception\NotFoundException;
|
||||
|
||||
class Product
|
||||
class Product extends View
|
||||
{
|
||||
public static function list(Request $request)
|
||||
public static function get(Request $request)
|
||||
{
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode(ProductModel::selectAll());
|
||||
}
|
||||
|
||||
public static function delete(Request $request)
|
||||
{
|
||||
$queryString = $request->getQueryString();
|
||||
$queryParams = $request->getQueryParams();
|
||||
|
||||
if (array_key_exists('id', $queryString)) {
|
||||
$ids = explode(',', $queryString['id']);
|
||||
if (array_key_exists('id', $queryParams)) {
|
||||
$ids = explode(',', $queryParams['id']);
|
||||
$ids = array_map('intval', $ids);
|
||||
|
||||
foreach($ids as $id) {
|
||||
@@ -44,7 +48,70 @@ class Product
|
||||
}
|
||||
}
|
||||
|
||||
public static function add(Request $request)
|
||||
public static function post(Request $request)
|
||||
{
|
||||
$params= $request->getFormParams();
|
||||
$expected = [
|
||||
'sku',
|
||||
'name',
|
||||
'price',
|
||||
'productType',
|
||||
'weight',
|
||||
'size',
|
||||
'height',
|
||||
'width',
|
||||
'length'
|
||||
];
|
||||
if (self::expectArgs($expected, $params)) {
|
||||
$product = null;
|
||||
$type = $params['productType'];
|
||||
|
||||
switch($type) {
|
||||
case 'dvd':
|
||||
$product = new DVD(
|
||||
$params['sku'],
|
||||
$params['name'],
|
||||
$params['price'],
|
||||
$params['size'],
|
||||
);
|
||||
break;
|
||||
case 'furniture':
|
||||
$product = new Furniture(
|
||||
$params['sku'],
|
||||
$params['name'],
|
||||
$params['price'],
|
||||
$params['height'],
|
||||
$params['width'],
|
||||
$params['length'],
|
||||
);
|
||||
break;
|
||||
case 'book':
|
||||
$product = new Book(
|
||||
$params['sku'],
|
||||
$params['name'],
|
||||
$params['price'],
|
||||
$params['weight'],
|
||||
);
|
||||
break;
|
||||
default:
|
||||
http_response_code(400);
|
||||
echo "Invalid 'productType' value '$type'";
|
||||
return;
|
||||
}
|
||||
|
||||
//try {
|
||||
$product->insert();
|
||||
//} catch (\Exception $e) {
|
||||
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
public static function test(Request $request)
|
||||
{
|
||||
$params= $request->getQueryParams();
|
||||
if (self::expectArgs(['testarg'], $params)) {
|
||||
echo var_dump($params);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
22
src/View/View.php
Normal file
22
src/View/View.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
namespace ProductList\View;
|
||||
|
||||
abstract class View
|
||||
{
|
||||
protected static function expectArgs(array $args, array $provided) {
|
||||
$keys = array_keys($provided);
|
||||
|
||||
$missing = array_diff($args, $keys);
|
||||
|
||||
if (count($missing) > 0) {
|
||||
http_response_code(400);
|
||||
|
||||
$missing = join("', '", $missing);
|
||||
|
||||
echo "Missing parameters '$missing'";
|
||||
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user