Skip to content

Commit

Permalink
implement 2 endpoints: /orders and /orders/stats/sales-over-time
Browse files Browse the repository at this point in the history
  • Loading branch information
creme332 committed Oct 8, 2024
1 parent 82c0cda commit 67ad1b1
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 7 deletions.
15 changes: 8 additions & 7 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,14 @@ A user can be a client or an administrator.

### Order

| Endpoint | Description | Protected | Query string parameters |
|------------------------------|-------------------------------------------------------|-----------|------------------------------|
| `GET /api/v1/orders/` | Get the list of all orders. | Yes | `sort`, `group-by`, `status` |
| `GET /api/v1/orders/[id]` | Get the details of a specific order by its ID. | Yes |
| `POST /api/v1/orders` | Create a new order for products. | Yes |
| `PUT /api/v1/orders/[id]` | Update the details of an order with the specified ID. | Yes |
| `DELETE /api/v1/orders/[id]` | Delete an order with the specified ID. | Yes |
| Endpoint | Description | Protected | Query string parameters |
|--------------------------------------------|-------------------------------------------------------|-----------|------------------------------|
| `GET /api/v1/orders/` | Get the list of all orders. | Yes | `sort`, `group-by`, `status` |
| `GET /api/v1/orders/[id]` | Get the details of a specific order by its ID. | Yes |
| `POST /api/v1/orders` | Create a new order for products. | Yes |
| `PUT /api/v1/orders/[id]` | Update the details of an order with the specified ID. | Yes |
| `DELETE /api/v1/orders/[id]` | Delete an order with the specified ID. | Yes |
| `GET /api/v1/orders/stats/sales-over-time` | Get total revenue for each month. | Yes | |

### Review

Expand Down
79 changes: 79 additions & 0 deletions src/controllers/api/Orders.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

namespace Steamy\Controller\API;

use PDO;
use Steamy\Core\Model;

class Orders
{
use Model;

public static array $routes = [
'GET' => [
'/orders' => 'getAllOrders',
'/orders/stats/sales-over-time' => 'getSalesOverTime',
]
];

/**
* Get all orders
* @return void
*/
public function getAllOrders(): void
{
$con = self::connect();
$stm = $con->prepare("SELECT * FROM `order`");
$stm->execute();

echo json_encode($stm->fetchAll(PDO::FETCH_ASSOC));
}

/**
* Get total revenue for each month.
*
* Example:
*
* <pre>
* [
* {
* "date": "2024-06-01",
* "totalOrders": 1,
* "totalRevenue": "3.49"
* },
* {
* "date": "2024-07-01",
* "totalOrders": 9,
* "totalRevenue": "40.91"
* }
* ]
* </pre>
*
* @return void
*/
public function getSalesOverTime(): void
{
$query = <<< EOL
SELECT
DATE_FORMAT(o.created_date, '%Y-%m-01') AS date, -- Group by month
COUNT(DISTINCT o.order_id) AS totalOrders, -- Count the number of unique orders
SUM(op.quantity * op.unit_price) AS totalRevenue -- Total revenue calculation
FROM
`order` o
JOIN
order_product op ON o.order_id = op.order_id
GROUP BY
DATE_FORMAT(o.created_date, '%Y-%m-01') -- Group by the first day of each month
ORDER BY
date; -- Order by date
EOL;

$con = self::connect();
$stm = $con->prepare($query);
$stm->execute();

echo json_encode($stm->fetchAll(PDO::FETCH_ASSOC));
}
}

0 comments on commit 67ad1b1

Please sign in to comment.