feat: calculate and store average rating with feedback

This commit is contained in:
Björn Fromme
2023-12-07 15:31:54 +01:00
parent f1bee04e60
commit a582f149eb
3 changed files with 49 additions and 3 deletions
+17 -2
View File
@@ -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