diff --git a/index.php b/index.php index 1ae4ad6..5870efe 100644 --- a/index.php +++ b/index.php @@ -5,12 +5,14 @@ use ProductList\Http\Request; use ProductList\Http\RequestHandler; use ProductList\Http\Route; -$request = new Request($_SERVER); +$request = new Request($_SERVER, $_GET, $_POST); $handler = new RequestHandler($request); $handler->registerRoutes([ - new Route('GET', 'products', ['ProductList\View\Product', 'list']), - new Route('DELETE', 'products', ['ProductList\View\Product', 'delete']), + new Route('GET', 'test', ['ProductList\View\Product', 'test']), + new Route('GET', 'product', ['ProductList\View\Product', 'get']), + new Route('DELETE', 'product', ['ProductList\View\Product', 'delete']), + new Route('POST', 'product', ['ProductList\View\Product', 'post']), new Route('GET', 'add-product', function() { readfile('static/add-product.html'); }), new Route('GET', '', function() { readfile('static/index.html'); }), ]); diff --git a/src/Http/Request.php b/src/Http/Request.php index 342e3fe..217a988 100644 --- a/src/Http/Request.php +++ b/src/Http/Request.php @@ -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; } } diff --git a/src/Http/RequestHandler.php b/src/Http/RequestHandler.php index 87b0244..87ebe1f 100644 --- a/src/Http/RequestHandler.php +++ b/src/Http/RequestHandler.php @@ -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; } } diff --git a/src/View/Product.php b/src/View/Product.php index a2124dd..ed1b230 100644 --- a/src/View/Product.php +++ b/src/View/Product.php @@ -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); + } } } diff --git a/src/View/View.php b/src/View/View.php new file mode 100644 index 0000000..7a19ecb --- /dev/null +++ b/src/View/View.php @@ -0,0 +1,22 @@ + 0) { + http_response_code(400); + + $missing = join("', '", $missing); + + echo "Missing parameters '$missing'"; + + return false; + } + return true; + } +} diff --git a/static/add-product.html b/static/add-product.html index 5024d2e..9f5816a 100644 --- a/static/add-product.html +++ b/static/add-product.html @@ -1,16 +1,83 @@ -
- - -
+ ${product.sku}
+ ${product.name}
+ ${product.price} $
+ ${product.attribute}
+
- ${product.sku}
- ${product.name}
- ${product.price} $
- ${product.attribute}
-