From a582f149eb584d0e3df903844784aa55c07ba4f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Fromme?= Date: Thu, 7 Dec 2023 15:31:54 +0100 Subject: [PATCH] feat: calculate and store average rating with feedback --- migrations/Version20231207143022.php | 31 +++++++++++++++++++++++++ src/Entity/Feedback.php | 19 +++++++++++++-- templates/house_manager/index.html.twig | 2 +- 3 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 migrations/Version20231207143022.php diff --git a/migrations/Version20231207143022.php b/migrations/Version20231207143022.php new file mode 100644 index 0000000..cf723a2 --- /dev/null +++ b/migrations/Version20231207143022.php @@ -0,0 +1,31 @@ +addSql('ALTER TABLE feedback ADD average_rating INT DEFAULT NULL'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE feedback DROP average_rating'); + } +} diff --git a/src/Entity/Feedback.php b/src/Entity/Feedback.php index c6323e3..1af402c 100644 --- a/src/Entity/Feedback.php +++ b/src/Entity/Feedback.php @@ -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 diff --git a/templates/house_manager/index.html.twig b/templates/house_manager/index.html.twig index edc432c..2fe2336 100644 --- a/templates/house_manager/index.html.twig +++ b/templates/house_manager/index.html.twig @@ -14,7 +14,7 @@ {% block content %}
-

+

Neue Einteilungen