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
+31
View File
@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20231207143022 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->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');
}
}
+17 -2
View File
@@ -11,6 +11,7 @@ use Symfony\Component\Uid\Uuid;
use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Entity(repositoryClass: FeedbackRepository::class)] #[ORM\Entity(repositoryClass: FeedbackRepository::class)]
#[ORM\HasLifecycleCallbacks]
class Feedback implements BlameableEntityInterface, TimestampableEntityInterface class Feedback implements BlameableEntityInterface, TimestampableEntityInterface
{ {
use BlameableEntity; use BlameableEntity;
@@ -46,6 +47,9 @@ class Feedback implements BlameableEntityInterface, TimestampableEntityInterface
#[Assert\Count(min: 1, minMessage: 'Bitte gib eine Bewertung ab')] #[Assert\Count(min: 1, minMessage: 'Bitte gib eine Bewertung ab')]
private array $ratings = []; private array $ratings = [];
#[ORM\Column(nullable: true)]
private ?int $averageRating = null;
#[ORM\Column(type: Types::TEXT, nullable: true)] #[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $comment = null; private ?string $comment = null;
@@ -155,9 +159,18 @@ class Feedback implements BlameableEntityInterface, TimestampableEntityInterface
} }
public function getAverageRating(): ?int public function getAverageRating(): ?int
{
return $this->averageRating;
}
#[ORM\PrePersist]
#[ORM\PreUpdate]
public function setAverageRating(): static
{ {
if (0 === count($this->ratings)) { if (0 === count($this->ratings)) {
return null; $this->averageRating = null;
return $this;
} }
$sum = 0; $sum = 0;
@@ -166,7 +179,9 @@ class Feedback implements BlameableEntityInterface, TimestampableEntityInterface
$sum += $rating['mark']; $sum += $rating['mark'];
} }
return round($sum/count($this->ratings)); $this->averageRating = $sum/count($this->ratings) * 100;
return $this;
} }
public function getComment(): ?string public function getComment(): ?string
+1 -1
View File
@@ -14,7 +14,7 @@
{% block content %} {% block content %}
<div class="grid grid-cols-1 lg:grid-cols-2 gap-8"> <div class="grid grid-cols-1 lg:grid-cols-2 gap-8">
<div class="bg-gray-100 border border-gray-200 rounded-md px-4 py-2"> <div class="bg-gray-100 border border-gray-200 rounded-md px-4 py-2">
<h2 class="font-bold text-lg"> <h2 class="font-bold text-lg pb-2">
Neue Einteilungen Neue Einteilungen
</h2> </h2>
<ul class="divide-y divide-gray-200"> <ul class="divide-y divide-gray-200">