diff --git a/docs/API.md b/docs/API.md index 37a70a4..64d9af2 100644 --- a/docs/API.md +++ b/docs/API.md @@ -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 diff --git a/src/controllers/api/Orders.php b/src/controllers/api/Orders.php new file mode 100644 index 0000000..186d5aa --- /dev/null +++ b/src/controllers/api/Orders.php @@ -0,0 +1,79 @@ + [ + '/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: + * + *
+     *     [
+     *         {
+     *             "date": "2024-06-01",
+     *             "totalOrders": 1,
+     *             "totalRevenue": "3.49"
+     *         },
+     *         {
+     *             "date": "2024-07-01",
+     *             "totalOrders": 9,
+     *             "totalRevenue": "40.91"
+     *         }
+     *     ]
+     * 
+ * + * @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)); + } +} \ No newline at end of file