Compare commits
No commits in common. "7d2ae0b2d21f4110649ede0169c06d8effa6816e" and "922b29519aa7da90dd75b622ec87ab5e327c8107" have entirely different histories.
7d2ae0b2d2
...
922b29519a
|
@ -1,7 +1,7 @@
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8"/>
|
<meta charset="utf-8"/>
|
||||||
<link rel="stylesheet" href="static/index.css"/>
|
<link rel="stylesheet" href="index.css"/>
|
||||||
<title>Product Add</title>
|
<title>Product Add</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
|
@ -2,7 +2,7 @@
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8"/>
|
<meta charset="utf-8"/>
|
||||||
<link rel="stylesheet" href="static/index.css"/>
|
<link rel="stylesheet" href="index.css"/>
|
||||||
<title>Product List</title>
|
<title>Product List</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
@ -16,5 +16,5 @@
|
||||||
<div id="products">
|
<div id="products">
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
<script src="static/index.js"></script>
|
<script src="index.js"></script>
|
||||||
</html>
|
</html>
|
|
@ -37,7 +37,7 @@ const deleteSelected = () => {
|
||||||
loadItems();
|
loadItems();
|
||||||
}
|
}
|
||||||
|
|
||||||
xhttp.open('DELETE', `products?id=${values.join(',')}`, true);
|
xhttp.open('DELETE', 'products', true);
|
||||||
xhttp.send();
|
xhttp.send();
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,10 +9,9 @@ $request = new Request($_SERVER);
|
||||||
$handler = new RequestHandler($request);
|
$handler = new RequestHandler($request);
|
||||||
|
|
||||||
$handler->registerRoutes([
|
$handler->registerRoutes([
|
||||||
new Route('GET', 'products', ['ProductList\View\Product', 'list']),
|
new Route('GET', 'products', ['ProductList\View\Product', 'listAll']),
|
||||||
new Route('DELETE', 'products', ['ProductList\View\Product', 'delete']),
|
new Route('GET', 'add-product', function() { readfile('add-product.html'); }),
|
||||||
new Route('GET', 'add-product', function() { readfile('static/add-product.html'); }),
|
new Route('GET', '', function() { readfile('index.html'); }),
|
||||||
new Route('GET', '', function() { readfile('static/index.html'); }),
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$handler->handle();
|
$handler->handle();
|
||||||
|
|
|
@ -1,4 +0,0 @@
|
||||||
<?php
|
|
||||||
namespace ProductList\Exception;
|
|
||||||
|
|
||||||
class NotFoundException extends \Exception {}
|
|
|
@ -5,7 +5,6 @@ class Request
|
||||||
{
|
{
|
||||||
private $method;
|
private $method;
|
||||||
private $uri;
|
private $uri;
|
||||||
private $queryString;
|
|
||||||
|
|
||||||
public function getMethod()
|
public function getMethod()
|
||||||
{
|
{
|
||||||
|
@ -17,19 +16,9 @@ class Request
|
||||||
return $this->uri;
|
return $this->uri;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getQueryString()
|
|
||||||
{
|
|
||||||
return $this->queryString;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function __construct(array $params)
|
public function __construct(array $params)
|
||||||
{
|
{
|
||||||
$uri_base = trim($params['REQUEST_URI'], '?'.$params['QUERY_STRING']);
|
$this->uri = basename($params['REQUEST_URI']);
|
||||||
$uri_base = trim(urldecode($uri_base), '/');
|
|
||||||
$this->uri = explode('/', $uri_base);
|
|
||||||
|
|
||||||
$this->method = $params['REQUEST_METHOD'];
|
$this->method = $params['REQUEST_METHOD'];
|
||||||
|
|
||||||
parse_str($params['QUERY_STRING'], $this->queryString);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,7 @@ class Route
|
||||||
public function __construct(string $method, string $uri, array|\Closure $view)
|
public function __construct(string $method, string $uri, array|\Closure $view)
|
||||||
{
|
{
|
||||||
$this->method = $method;
|
$this->method = $method;
|
||||||
$this->uri = explode('/', $uri);
|
$this->uri = $uri;
|
||||||
$this->view = $view;
|
$this->view = $view;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -66,24 +66,7 @@ class Book extends Product
|
||||||
$this->setVariationId($conn->insert_id);
|
$this->setVariationId($conn->insert_id);
|
||||||
return $conn->insert_id;
|
return $conn->insert_id;
|
||||||
} else {
|
} else {
|
||||||
throw new \Exception("Unable to insert object");
|
throw new Exception("Unable to insert object");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function delete($conn = null)
|
|
||||||
{
|
|
||||||
if ($conn === null) {
|
|
||||||
$conn = Database::connect();
|
|
||||||
}
|
|
||||||
|
|
||||||
$variationId = $this->getVariationId();
|
|
||||||
$stmt = $conn->prepare('DELETE FROM '.BOOK.' WHERE id = ?');
|
|
||||||
$stmt->bind_param('i', $variationId);
|
|
||||||
|
|
||||||
if ($stmt->execute() === false) {
|
|
||||||
throw new \Exception("Unable to delete product with id '$id'");
|
|
||||||
}
|
|
||||||
|
|
||||||
parent::delete($conn);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -69,21 +69,4 @@ class DVD extends Product
|
||||||
throw new Exception("Unable to insert object");
|
throw new Exception("Unable to insert object");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function delete($conn = null)
|
|
||||||
{
|
|
||||||
if ($conn === null) {
|
|
||||||
$conn = Database::connect();
|
|
||||||
}
|
|
||||||
|
|
||||||
$variationId = $this->getVariationId();
|
|
||||||
$stmt = $conn->prepare('DELETE FROM '.DVD.' WHERE id = ?');
|
|
||||||
$stmt->bind_param('i', $variationId);
|
|
||||||
|
|
||||||
if ($stmt->execute() === false) {
|
|
||||||
throw new \Exception("Unable to delete product with id '$id'");
|
|
||||||
}
|
|
||||||
|
|
||||||
parent::delete($conn);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -92,24 +92,7 @@ class Furniture extends Product
|
||||||
$this->setVariationId($conn->insert_id);
|
$this->setVariationId($conn->insert_id);
|
||||||
return $conn->insert_id;
|
return $conn->insert_id;
|
||||||
} else {
|
} else {
|
||||||
throw new \Exception("Unable to insert object");
|
throw new Exception("Unable to insert object");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function delete($conn = null)
|
|
||||||
{
|
|
||||||
if ($conn === null) {
|
|
||||||
$conn = Database::connect();
|
|
||||||
}
|
|
||||||
|
|
||||||
$variationId = $this->getVariationId();
|
|
||||||
$stmt = $conn->prepare('DELETE FROM '.FURNITURE.' WHERE id = ?');
|
|
||||||
$stmt->bind_param('i', $variationId);
|
|
||||||
|
|
||||||
if ($stmt->execute() === false) {
|
|
||||||
throw new \Exception("Unable to delete product with id '$id'");
|
|
||||||
}
|
|
||||||
|
|
||||||
parent::delete($conn);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,6 @@ trait Model
|
||||||
{
|
{
|
||||||
abstract public static function fromRow($row) : self;
|
abstract public static function fromRow($row) : self;
|
||||||
abstract public function insert($conn = null) : int; // should return id
|
abstract public function insert($conn = null) : int; // should return id
|
||||||
abstract public function delete($conn = null);
|
|
||||||
abstract private static function getSelectAllQuery() : string;
|
abstract private static function getSelectAllQuery() : string;
|
||||||
|
|
||||||
public static function selectAll($conn = null) : array
|
public static function selectAll($conn = null) : array
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
namespace ProductList\Model;
|
namespace ProductList\Model;
|
||||||
|
|
||||||
use ProductList\Exception\NotFoundException;
|
|
||||||
|
|
||||||
abstract class Product implements \JsonSerializable
|
abstract class Product implements \JsonSerializable
|
||||||
{
|
{
|
||||||
use Model;
|
use Model;
|
||||||
|
@ -46,11 +44,6 @@ abstract class Product implements \JsonSerializable
|
||||||
return $this->productId;
|
return $this->productId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getVariationId()
|
|
||||||
{
|
|
||||||
return $this->variationId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setVariationId($id)
|
public function setVariationId($id)
|
||||||
{
|
{
|
||||||
$this->variationId = $id;
|
$this->variationId = $id;
|
||||||
|
@ -63,43 +56,6 @@ abstract class Product implements \JsonSerializable
|
||||||
|
|
||||||
abstract public function getFormatedAttr();
|
abstract public function getFormatedAttr();
|
||||||
|
|
||||||
public function delete($conn = null)
|
|
||||||
{
|
|
||||||
if ($conn === null) {
|
|
||||||
$conn = Database::connect();
|
|
||||||
}
|
|
||||||
|
|
||||||
$productId = $this->getProductId();
|
|
||||||
$stmt = $conn->prepare('DELETE FROM '.PRODUCT.' WHERE id = ?');
|
|
||||||
$stmt->bind_param('i', $productId);
|
|
||||||
|
|
||||||
if ($stmt->execute() === false) {
|
|
||||||
throw new \Exception("Unable to delete product with id '$id'");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function fromId($id, $conn = null) : self
|
|
||||||
{
|
|
||||||
if ($conn === null) {
|
|
||||||
$conn = Database::connect();
|
|
||||||
}
|
|
||||||
|
|
||||||
$stmt = $conn->prepare(self::getSelectAllQuery().' WHERE '.PRODUCT.'.id = ?');
|
|
||||||
$stmt->bind_param('i', $id);
|
|
||||||
|
|
||||||
if ($stmt->execute() === true) {
|
|
||||||
$row = $stmt->get_result()->fetch_assoc();
|
|
||||||
|
|
||||||
if($row === null) {
|
|
||||||
throw new NotFoundException("No product with id '$id'");
|
|
||||||
}
|
|
||||||
|
|
||||||
return self::fromRow($row);
|
|
||||||
} else {
|
|
||||||
throw new \Exception("Unable to select object");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function fromRow($row) : self
|
public static function fromRow($row) : self
|
||||||
{
|
{
|
||||||
if ($row['size'] !== null) {
|
if ($row['size'] !== null) {
|
||||||
|
@ -109,7 +65,7 @@ abstract class Product implements \JsonSerializable
|
||||||
} elseif ($row['height'] !== null) {
|
} elseif ($row['height'] !== null) {
|
||||||
return Furniture::fromRow($row);
|
return Furniture::fromRow($row);
|
||||||
} else {
|
} else {
|
||||||
throw new \Exception("Product without a type");
|
throw new Exception("Product without a type");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -121,7 +77,7 @@ abstract class Product implements \JsonSerializable
|
||||||
FROM '.PRODUCT.'
|
FROM '.PRODUCT.'
|
||||||
LEFT JOIN '.DVD.' ON '.PRODUCT.'.id = '.DVD.'.product_id
|
LEFT JOIN '.DVD.' ON '.PRODUCT.'.id = '.DVD.'.product_id
|
||||||
LEFT JOIN '.BOOK.' ON '.PRODUCT.'.id = '.BOOK.'.product_id
|
LEFT JOIN '.BOOK.' ON '.PRODUCT.'.id = '.BOOK.'.product_id
|
||||||
LEFT JOIN '.FURNITURE.' ON '.PRODUCT.'.id = '.FURNITURE.'.product_id';
|
LEFT JOIN '.FURNITURE.' ON '.PRODUCT.'.id = '.FURNITURE.'.product_id;';
|
||||||
}
|
}
|
||||||
|
|
||||||
public function insert($conn = null) : int
|
public function insert($conn = null) : int
|
||||||
|
@ -144,7 +100,7 @@ abstract class Product implements \JsonSerializable
|
||||||
$this->setProductId($conn->insert_id);
|
$this->setProductId($conn->insert_id);
|
||||||
return $conn->insert_id;
|
return $conn->insert_id;
|
||||||
} else {
|
} else {
|
||||||
throw new \Exception("Unable to insert object");
|
throw new Exception("Unable to insert object");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,48 +3,11 @@ namespace ProductList\View;
|
||||||
|
|
||||||
use ProductList\Http\Request;
|
use ProductList\Http\Request;
|
||||||
use ProductList\Model\Product as ProductModel;
|
use ProductList\Model\Product as ProductModel;
|
||||||
use ProductList\Exception\NotFoundException;
|
|
||||||
|
|
||||||
class Product
|
class Product
|
||||||
{
|
{
|
||||||
public static function list(Request $request)
|
public static function listAll(Request $request)
|
||||||
{
|
{
|
||||||
echo json_encode(ProductModel::selectAll());
|
echo json_encode(ProductModel::selectAll());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function delete(Request $request)
|
|
||||||
{
|
|
||||||
$queryString = $request->getQueryString();
|
|
||||||
|
|
||||||
if (array_key_exists('id', $queryString)) {
|
|
||||||
$ids = explode(',', $queryString['id']);
|
|
||||||
$ids = array_map('intval', $ids);
|
|
||||||
|
|
||||||
foreach($ids as $id) {
|
|
||||||
try {
|
|
||||||
$product = ProductModel::fromId($id);
|
|
||||||
|
|
||||||
try {
|
|
||||||
$product->delete();
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
http_response_code(500);
|
|
||||||
echo $e->getMessage();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (NotFoundException $e) {
|
|
||||||
http_response_code(404);
|
|
||||||
echo $e->getMessage();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
http_response_code(400);
|
|
||||||
echo 'Missing parameter "id".';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function add(Request $request)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue