feat: calculate and store average rating with feedback
This commit is contained in:
+17
-2
@@ -11,6 +11,7 @@ use Symfony\Component\Uid\Uuid;
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
|
||||
#[ORM\Entity(repositoryClass: FeedbackRepository::class)]
|
||||
#[ORM\HasLifecycleCallbacks]
|
||||
class Feedback implements BlameableEntityInterface, TimestampableEntityInterface
|
||||
{
|
||||
use BlameableEntity;
|
||||
@@ -46,6 +47,9 @@ class Feedback implements BlameableEntityInterface, TimestampableEntityInterface
|
||||
#[Assert\Count(min: 1, minMessage: 'Bitte gib eine Bewertung ab')]
|
||||
private array $ratings = [];
|
||||
|
||||
#[ORM\Column(nullable: true)]
|
||||
private ?int $averageRating = null;
|
||||
|
||||
#[ORM\Column(type: Types::TEXT, nullable: true)]
|
||||
private ?string $comment = null;
|
||||
|
||||
@@ -155,9 +159,18 @@ class Feedback implements BlameableEntityInterface, TimestampableEntityInterface
|
||||
}
|
||||
|
||||
public function getAverageRating(): ?int
|
||||
{
|
||||
return $this->averageRating;
|
||||
}
|
||||
|
||||
#[ORM\PrePersist]
|
||||
#[ORM\PreUpdate]
|
||||
public function setAverageRating(): static
|
||||
{
|
||||
if (0 === count($this->ratings)) {
|
||||
return null;
|
||||
$this->averageRating = null;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
$sum = 0;
|
||||
@@ -166,7 +179,9 @@ class Feedback implements BlameableEntityInterface, TimestampableEntityInterface
|
||||
$sum += $rating['mark'];
|
||||
}
|
||||
|
||||
return round($sum/count($this->ratings));
|
||||
$this->averageRating = $sum/count($this->ratings) * 100;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getComment(): ?string
|
||||
|
||||
Reference in New Issue
Block a user