From 33e6994aaa8b2173c813dad92288b5465c42d0c1 Mon Sep 17 00:00:00 2001 From: Ramon Gutierrez Date: Mon, 22 May 2023 13:00:54 +0800 Subject: [PATCH] Add rider rating average computation #730 --- src/Controller/CustomerAppAPI/RiderController.php | 5 ++++- src/Entity/Rider.php | 13 ++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Controller/CustomerAppAPI/RiderController.php b/src/Controller/CustomerAppAPI/RiderController.php index 53032c27..96557a54 100644 --- a/src/Controller/CustomerAppAPI/RiderController.php +++ b/src/Controller/CustomerAppAPI/RiderController.php @@ -230,7 +230,10 @@ class RiderController extends ApiController $this->em->persist($rating); $this->em->flush(); - // TODO: set average rating in rider entity + // TODO: preliminary rating computation on the entity for now + $rider->updateRatingAverage(); + $this->em->persist($rider); + $this->em->flush(); // response return new ApiResponse(); diff --git a/src/Entity/Rider.php b/src/Entity/Rider.php index 8fcec623..fcdb3ed2 100644 --- a/src/Entity/Rider.php +++ b/src/Entity/Rider.php @@ -81,7 +81,7 @@ class Rider // current rating of rider /** - * @ORM\Column(type="integer") + * @ORM\Column(type="float") */ protected $curr_rating; @@ -241,6 +241,17 @@ class Rider return $this->image_file; } + public function updateRatingAverage() + { + $total = 0; + + foreach ($this->ratings as $rating) { + $total += $rating->getRating(); + } + + $this->setCurrentRating(round($total / $this->ratings->count(), 2)); + } + public function setCurrentRating($rating) { $this->curr_rating = $rating;