Add deleting mechanism

This commit is contained in:
Augusto Gunsch 2022-07-28 09:19:11 +02:00
parent 40b7d51b39
commit 7d2ae0b2d2
No known key found for this signature in database
GPG Key ID: F7EEFE29825C72DC
11 changed files with 159 additions and 10 deletions

View File

@ -9,7 +9,8 @@ $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('DELETE', 'products', ['ProductList\View\Product', 'delete']),
new Route('GET', 'add-product', function() { readfile('static/add-product.html'); }), new Route('GET', 'add-product', function() { readfile('static/add-product.html'); }),
new Route('GET', '', function() { readfile('static/index.html'); }), new Route('GET', '', function() { readfile('static/index.html'); }),
]); ]);

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

@ -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();
} }