Skip to content

Commit

Permalink
update getRatingDistribution() : modify SQL query in rating percentag…
Browse files Browse the repository at this point in the history
…e calculation
  • Loading branch information
Divyeshhhh committed Jun 8, 2024
1 parent 30440a2 commit d0ae2e1
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 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 @@ -394,7 +394,14 @@ 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
COUNT(*) * 100.0 / (
SELECT COUNT(*)
FROM review r
JOIN order_product op ON r.client_id = op.client_id
JOIN `order` o ON op.order_id = o.order_id
WHERE r.product_id = :product_id
AND op.product_id = :product_id
) AS percentage
FROM review
WHERE product_id = :product_id
GROUP BY rating
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 d0ae2e1

Please sign in to comment.