From 5821209e4d72b048b8bc3e73dbd700d750e16da6 Mon Sep 17 00:00:00 2001 From: myrmidex Date: Fri, 5 Jun 2026 22:32:53 +0000 Subject: [PATCH] 4 - Add createdAt column --- migrations/Version20260605221350.php | 35 +++++++++++++++++++ migrations/Version20260605222943.php | 31 ++++++++++++++++ src/Controller/Api/RatingController.php | 5 +++ src/Entity/Rating.php | 32 +++++++++++++++++ src/Factory/RatingFactory.php | 1 + .../Controller/Api/RatingControllerTest.php | 7 +++- 6 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 migrations/Version20260605221350.php create mode 100644 migrations/Version20260605222943.php diff --git a/migrations/Version20260605221350.php b/migrations/Version20260605221350.php new file mode 100644 index 0000000..927223f --- /dev/null +++ b/migrations/Version20260605221350.php @@ -0,0 +1,35 @@ +addSql('ALTER TABLE rating ADD user_id INT NOT NULL'); + $this->addSql('ALTER TABLE rating ADD CONSTRAINT FK_D8892622A76ED395 FOREIGN KEY (user_id) REFERENCES "user" (id) NOT DEFERRABLE'); + $this->addSql('CREATE INDEX IDX_D8892622A76ED395 ON rating (user_id)'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE rating DROP CONSTRAINT FK_D8892622A76ED395'); + $this->addSql('DROP INDEX IDX_D8892622A76ED395'); + $this->addSql('ALTER TABLE rating DROP user_id'); + } +} diff --git a/migrations/Version20260605222943.php b/migrations/Version20260605222943.php new file mode 100644 index 0000000..4ef90e2 --- /dev/null +++ b/migrations/Version20260605222943.php @@ -0,0 +1,31 @@ +addSql('ALTER TABLE rating ADD created_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE rating DROP created_at'); + } +} diff --git a/src/Controller/Api/RatingController.php b/src/Controller/Api/RatingController.php index 69a85b2..3623f43 100644 --- a/src/Controller/Api/RatingController.php +++ b/src/Controller/Api/RatingController.php @@ -3,6 +3,7 @@ namespace App\Controller\Api; use App\Entity\Rating; +use App\Entity\User; use App\Repository\RatingRepository; use App\Repository\SubjectRepository; use App\Services\OutputService; @@ -11,6 +12,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Attribute\Route; +use Symfony\Component\Security\Http\Attribute\CurrentUser; class RatingController extends AbstractController { @@ -40,6 +42,8 @@ class RatingController extends AbstractController EntityManagerInterface $em, OutputService $output, SubjectRepository $subjectRepository, + #[CurrentUser] + User $user ): JsonResponse { $subject = $subjectRepository->find($request->getPayload()->get('subject_id')); @@ -51,6 +55,7 @@ class RatingController extends AbstractController $rating = new Rating(); $rating->setSubject($subject); + $rating->setUser($user); $rating->setScore($request->getPayload()->get('score')); $em->persist($rating); diff --git a/src/Entity/Rating.php b/src/Entity/Rating.php index 3cc72eb..ffd51d5 100644 --- a/src/Entity/Rating.php +++ b/src/Entity/Rating.php @@ -3,10 +3,12 @@ namespace App\Entity; use App\Repository\RatingRepository; +use DateTimeImmutable; use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity(repositoryClass: RatingRepository::class)] +#[ORM\HasLifecycleCallbacks] class Rating { #[ORM\Id] @@ -14,6 +16,10 @@ class Rating #[ORM\Column] private ?int $id = null; + #[ORM\ManyToOne] + #[ORM\JoinColumn(nullable: false)] + private User $user; + #[ORM\ManyToOne(inversedBy: 'ratings')] #[ORM\JoinColumn(nullable: false)] private Subject $subject; @@ -24,6 +30,9 @@ class Rating #[ORM\Column(type: Types::TEXT, options: ['default' => ''])] private string $note = ''; + #[ORM\Column(type: Types::DATETIME_IMMUTABLE)] + private DateTimeImmutable $createdAt; + public function getId(): ?int { return $this->id; @@ -41,6 +50,18 @@ class Rating return $this; } + public function getUser(): User + { + return $this->user; + } + + public function setUser(User $user): static + { + $this->user = $user; + + return $this; + } + public function getScore(): ?int { return $this->score; @@ -64,4 +85,15 @@ class Rating return $this; } + + #[ORM\PrePersist] + public function setCreatedAtValue(): void + { + $this->createdAt = new \DateTimeImmutable(); + } + + public function getCreatedAt(): ?\DateTimeImmutable + { + return $this->createdAt; + } } diff --git a/src/Factory/RatingFactory.php b/src/Factory/RatingFactory.php index 6d06da0..1b92b0f 100644 --- a/src/Factory/RatingFactory.php +++ b/src/Factory/RatingFactory.php @@ -33,6 +33,7 @@ final class RatingFactory extends PersistentObjectFactory { return [ 'subject' => RestaurantFactory::new(), + 'user' => UserFactory::new(), ]; } diff --git a/tests/Application/Controller/Api/RatingControllerTest.php b/tests/Application/Controller/Api/RatingControllerTest.php index 7c4c4e6..2098197 100644 --- a/tests/Application/Controller/Api/RatingControllerTest.php +++ b/tests/Application/Controller/Api/RatingControllerTest.php @@ -73,7 +73,10 @@ class RatingControllerTest extends WebTestCase $this->assertNotNull($ratings); $this->assertCount(1, $ratings); - $this->assertInstanceOf(Rating::class, $ratings[0]); + + $rating = $ratings[0]; + $this->assertInstanceOf(Rating::class, $rating); + $this->assertSame($user->getId(), $rating->getUser()->getId()); } public function testUpdate(): void @@ -81,6 +84,7 @@ class RatingControllerTest extends WebTestCase $client = static::createClient(); $user = UserFactory::createOne(); + $client->loginUser($user); $subject = RestaurantFactory::createOne([ @@ -89,6 +93,7 @@ class RatingControllerTest extends WebTestCase $rating = RatingFactory::createOne([ 'subject' => $subject, + 'user' => $user, 'score' => 0, 'note' => 'first note', ]);