Skip to content

Commit

Permalink
implement GET `/products/stats/sales-per-category endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
creme332 committed Oct 8, 2024
1 parent 22cc20d commit 023f6d5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
19 changes: 10 additions & 9 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
24 changes: 24 additions & 0 deletions src/controllers/api/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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',
Expand Down Expand Up @@ -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));
}
}

0 comments on commit 023f6d5

Please sign in to comment.