From 023f6d5c8cd47b1265fb1ea39df023ee8866d535 Mon Sep 17 00:00:00 2001 From: creme332 <65414576+creme332@users.noreply.github.com> Date: Tue, 8 Oct 2024 16:48:06 +0400 Subject: [PATCH] implement GET `/products/stats/sales-per-category endpoint --- docs/API.md | 19 ++++++++++--------- src/controllers/api/Products.php | 24 ++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/docs/API.md b/docs/API.md index f7b294a..8153456 100644 --- a/docs/API.md +++ b/docs/API.md @@ -47,15 +47,16 @@ A user can be a client or an administrator. ### Product -| Endpoint | Description | Protected | Query string parameters | -|------------------------------------|--------------------------------------------------------|-----------|-------------------------| -| `GET /api/v1/products` | Get the list of all products available in the store. | No | `sort`, `group-by` | -| `GET /api/v1/products/[id]` | Get the details of a specific product by its ID. | No | -| `GET /api/v1/products/[id]/stores` | Get the details of stores where a product is sold. | No | -| `GET /api/v1/products/categories` | Get the list of product categories. | No | -| `POST /api/v1/products` | Create a new product entry in the database. | No | -| `DELETE /api/v1/products/[id]` | Delete a product with the specified ID. | No | -| `PUT /api/v1/products/[id]` | Update the details of a product with the specified ID. | No | +| Endpoint | Description | Protected | Query string parameters | +|-------------------------------------------------|--------------------------------------------------------|-----------|-------------------------| +| `GET /api/v1/products` | Get the list of all products available in the store. | No | `sort`, `group-by` | +| `GET /api/v1/products/[id]` | Get the details of a specific product by its ID. | No | +| `GET /api/v1/products/[id]/stores` | Get the details of stores where a product is sold. | No | +| `GET /api/v1/products/categories` | Get the list of product categories. | No | +| `POST /api/v1/products` | Create a new product entry in the database. | No | +| `DELETE /api/v1/products/[id]` | Delete a product with the specified ID. | No | +| `PUT /api/v1/products/[id]` | Update the details of a product with the specified ID. | No | +| `GET /api/v1/products/sales/sales-per-category` | Get the number of units sold per category. | No | | ### Order diff --git a/src/controllers/api/Products.php b/src/controllers/api/Products.php index 699493b..3b1190d 100644 --- a/src/controllers/api/Products.php +++ b/src/controllers/api/Products.php @@ -5,6 +5,7 @@ namespace Steamy\Controller\API; use Opis\JsonSchema\{Errors\ErrorFormatter}; +use PDO; use Steamy\Core\Utility; use Steamy\Model\Product; use Steamy\Core\Model; @@ -22,6 +23,8 @@ class Products '/products/{id}' => 'getProductById', '/products/{id}/reviews' => 'getAllReviewsForProduct', '/products/{id}/stores' => 'getAllStoresForProduct', + + '/products/stats/sales-per-category' => 'getSalesPerCategory', ], 'POST' => [ '/products' => 'createProduct', @@ -256,4 +259,25 @@ public function getAllStoresForProduct(): void // Return JSON response echo json_encode($result); } + + /** + * Get units sold per product category + * @return void + */ + public function getSalesPerCategory(): void + { + $query = <<< EOL + SELECT category, COUNT(*) as unitsSold + FROM product + INNER JOIN order_product + ON product.product_id = order_product.product_id + GROUP BY category + EOL; + + $con = self::connect(); + $stm = $con->prepare($query); + $stm->execute(); + + echo json_encode($stm->fetchAll(PDO::FETCH_ASSOC)); + } }