diff --git a/docs/API.md b/docs/API.md index 8153456..37a70a4 100644 --- a/docs/API.md +++ b/docs/API.md @@ -70,13 +70,14 @@ A user can be a client or an administrator. ### Review -| Endpoint | Description | Protected | Query string parameters | -|-------------------------------------|-------------------------------------------------------|-----------|-------------------------| -| `GET /api/v1/reviews` | Get all reviews for a particular product by its ID. | No | `limit`, `order_by` | -| `GET /api/v1/products/[id]/reviews` | Get all reviews for a particular product by its ID. | No | -| `POST /api/v1/reviews` | Create a new review for a product. | Yes | -| `PUT /api/v1/reviews/[id]` | Update the details of a review with the specified ID. | Yes | -| `DELETE /api/v1/reviews/[id]` | Delete a review with the specified ID. | Yes | +| Endpoint | Description | Protected | Query string parameters | +|---------------------------------------------|-------------------------------------------------------|-----------|-------------------------| +| `GET /api/v1/reviews` | Get all reviews for a particular product by its ID. | No | `limit`, `order_by` | +| `GET /api/v1/products/[id]/reviews` | Get all reviews for a particular product by its ID. | No | +| `POST /api/v1/reviews` | Create a new review for a product. | Yes | +| `PUT /api/v1/reviews/[id]` | Update the details of a review with the specified ID. | Yes | +| `DELETE /api/v1/reviews/[id]` | Delete a review with the specified ID. | Yes | +| `GET /api/v1/reviews/stats/count-over-time` | Get the count of reviews for each month. | No | | ### District diff --git a/src/controllers/api/Reviews.php b/src/controllers/api/Reviews.php index 5e44d76..aee5f82 100644 --- a/src/controllers/api/Reviews.php +++ b/src/controllers/api/Reviews.php @@ -7,18 +7,19 @@ use Opis\JsonSchema\{Errors\ErrorFormatter}; use Exception; use PDO; -use Steamy\Core\Database; +use Steamy\Core\Model; use Steamy\Core\Utility; use Steamy\Model\Review; class Reviews { - use Database; + use Model; public static array $routes = [ 'GET' => [ '/reviews' => 'getAllReviews', '/reviews/{id}' => 'getReviewByID', + '/reviews/stats/count-over-time' => 'getCountOverTime', ], 'POST' => [ '/reviews' => 'createReview', @@ -203,4 +204,31 @@ public function deleteReview(): void echo json_encode(['error' => 'Failed to delete review']); } } + + /** + * Gets the number of reviews for each month + * @return void + */ + public function getCountOverTime(): void + { + $query = <<< EOL + SELECT + DATE_FORMAT(created_date, '%Y-%m-01') AS date, -- Group by month + COUNT(*) AS totalReviews, -- Total number of reviews + SUM(IF(rating >= 3, 1, 0)) AS positiveReviews, -- Count of positive reviews (rating 3 and above) + SUM(IF(rating < 3, 1, 0)) AS negativeReviews -- Count of negative reviews (rating below 3) + FROM + review + GROUP BY + DATE_FORMAT(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)); + } }