Skip to content

Commit

Permalink
implement /reviews/stats/count-over-time endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
creme332 committed Oct 8, 2024
1 parent 023f6d5 commit 82c0cda
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
15 changes: 8 additions & 7 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

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

0 comments on commit 82c0cda

Please sign in to comment.