Compare commits

..

2 Commits

Author SHA1 Message Date
Augusto Gunsch 7d2ae0b2d2
Add deleting mechanism 2022-07-28 09:19:11 +02:00
Augusto Gunsch 40b7d51b39
Reorganize files 2022-07-28 07:23:22 +02:00
15 changed files with 164 additions and 15 deletions

View File

@ -9,9 +9,10 @@ $request = new Request($_SERVER);
$handler = new RequestHandler($request); $handler = new RequestHandler($request);
$handler->registerRoutes([ $handler->registerRoutes([
new Route('GET', 'products', ['ProductList\View\Product', 'listAll']), new Route('GET', 'products', ['ProductList\View\Product', 'list']),
new Route('GET', 'add-product', function() { readfile('add-product.html'); }), new Route('DELETE', 'products', ['ProductList\View\Product', 'delete']),
new Route('GET', '', function() { readfile('index.html'); }), new Route('GET', 'add-product', function() { readfile('static/add-product.html'); }),
new Route('GET', '', function() { readfile('static/index.html'); }),
]); ]);
$handler->handle(); $handler->handle();

View File

@ -0,0 +1,4 @@
<?php
namespace ProductList\Exception;
class NotFoundException extends \Exception {}

View File

@ -5,6 +5,7 @@ class Request
{ {
private $method; private $method;
private $uri; private $uri;
private $queryString;
public function getMethod() public function getMethod()
{ {
@ -16,9 +17,19 @@ class Request
return $this->uri; return $this->uri;
} }
public function getQueryString()
{
return $this->queryString;
}
public function __construct(array $params) public function __construct(array $params)
{ {
$this->uri = basename($params['REQUEST_URI']); $uri_base = trim($params['REQUEST_URI'], '?'.$params['QUERY_STRING']);
$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);
} }
} }

View File

@ -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 = $uri; $this->uri = explode('/', $uri);
$this->view = $view; $this->view = $view;
} }

View File

@ -66,7 +66,24 @@ 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);
}
} }

View File

@ -69,4 +69,21 @@ 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);
}
} }

View File

@ -92,7 +92,24 @@ 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);
}
} }

View File

@ -5,6 +5,7 @@ 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

View File

@ -1,6 +1,8 @@
<?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;
@ -44,6 +46,11 @@ 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;
@ -56,6 +63,43 @@ 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) {
@ -65,7 +109,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");
} }
} }
@ -77,7 +121,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
@ -100,7 +144,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");
} }
} }

View File

@ -3,11 +3,48 @@ 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 listAll(Request $request) public static function list(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)
{
}
} }

View File

@ -1,7 +1,7 @@
<html> <html>
<head> <head>
<meta charset="utf-8"/> <meta charset="utf-8"/>
<link rel="stylesheet" href="index.css"/> <link rel="stylesheet" href="static/index.css"/>
<title>Product Add</title> <title>Product Add</title>
</head> </head>
<body> <body>

View File

@ -2,7 +2,7 @@
<html> <html>
<head> <head>
<meta charset="utf-8"/> <meta charset="utf-8"/>
<link rel="stylesheet" href="index.css"/> <link rel="stylesheet" href="static/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="index.js"></script> <script src="static/index.js"></script>
</html> </html>

View File

@ -37,7 +37,7 @@ const deleteSelected = () => {
loadItems(); loadItems();
} }
xhttp.open('DELETE', 'products', true); xhttp.open('DELETE', `products?id=${values.join(',')}`, true);
xhttp.send(); xhttp.send();
} }