Skip to content

Commit

Permalink
Merge pull request #232 from Divyesh000/excludeunverified
Browse files Browse the repository at this point in the history
update getRatingDistribution() : modify SQL query in rating percentag…
  • Loading branch information
creme332 authored Jun 11, 2024
2 parents 2e3c5f6 + 018f243 commit a0486f1
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public static function getCategories(): array
return [];
}

$callback = fn($obj): string => $obj->category;
$callback = fn ($obj): string => $obj->category;

return array_map($callback, $result);
}
Expand Down Expand Up @@ -393,11 +393,18 @@ public function getRatingDistribution(): array
{
// Query the database to get the percentage distribution of ratings
$query = <<< EOL
SELECT rating,
COUNT(*) * 100.0 / (SELECT COUNT(*) FROM review WHERE product_id = :product_id) AS percentage
FROM review
WHERE product_id = :product_id
GROUP BY rating
SELECT rating,
COUNT(*) * 10.0 / (
SELECT COUNT(*)
FROM order_product op
JOIN `order` o ON op.order_id = o.order_id
WHERE op.product_id = :product_id
) AS percentage
FROM review r
JOIN `order` o ON r.client_id = o.client_id
JOIN order_product op ON op.order_id = o.order_id
WHERE op.product_id = :product_id
GROUP BY rating;
EOL;

$params = ['product_id' => $this->product_id];
Expand Down Expand Up @@ -433,4 +440,4 @@ public function updateProduct(array $newProductData): bool

return $this->update($newProductData, ['product_id' => $this->product_id], $this->table);
}
}
}

0 comments on commit a0486f1

Please sign in to comment.