From eac85a904502e69110e90a9152dfa7b982d3611e Mon Sep 17 00:00:00 2001 From: myrmidex Date: Thu, 4 Jun 2026 22:20:33 +0000 Subject: [PATCH] Update rating + add note field --- migrations/Version20260604220935.php | 31 +++++++++++++++ src/Controller/Api/RatingController.php | 31 +++++++++++++++ src/Entity/Rating.php | 15 +++++++ .../Controller/Api/RatingControllerTest.php | 39 +++++++++++++++++++ 4 files changed, 116 insertions(+) create mode 100644 migrations/Version20260604220935.php diff --git a/migrations/Version20260604220935.php b/migrations/Version20260604220935.php new file mode 100644 index 0000000..203646d --- /dev/null +++ b/migrations/Version20260604220935.php @@ -0,0 +1,31 @@ +addSql('ALTER TABLE rating ADD note TEXT DEFAULT \'\' 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 note'); + } +} diff --git a/src/Controller/Api/RatingController.php b/src/Controller/Api/RatingController.php index cdd6b93..f68f8ca 100644 --- a/src/Controller/Api/RatingController.php +++ b/src/Controller/Api/RatingController.php @@ -59,4 +59,35 @@ class RatingController extends AbstractController return $this->json(null, 201); } + + #[Route('/api/ratings/{id}', methods: ['PATCH'])] + public function update( + int $id, + Request $request, + EntityManagerInterface $em, + OutputService $output, + RatingRepository $ratingRepository, + ): JsonResponse { + $rating = $ratingRepository->findById($id); + + if ($rating === null) { + return $this->json( + $output->error(["UNKNOWN_RATING"]) + ); + } + + $payload = $request->getPayload(); + + if ($payload->has('score')) { + $rating->setScore($payload->getInt('score')); + } + + if ($payload->has('note')) { + $rating->setNote($payload->getString('note')); + } + + $em->flush(); + + return $this->json(null, 201); + } } diff --git a/src/Entity/Rating.php b/src/Entity/Rating.php index be7e3c9..755b207 100644 --- a/src/Entity/Rating.php +++ b/src/Entity/Rating.php @@ -21,6 +21,9 @@ class Rating #[ORM\Column(type: Types::SMALLINT, nullable: true)] private ?int $score = null; + #[ORM\Column(type: Types::TEXT, options: ['default' => ''])] + private string $note = ''; + public function getId(): ?int { return $this->id; @@ -49,4 +52,16 @@ class Rating return $this; } + + public function getNote(): string + { + return $this->note; + } + + public function setNote(?string $note = ''): static + { + $this->note = $note; + + return $this; + } } diff --git a/tests/Application/Controller/Api/RatingControllerTest.php b/tests/Application/Controller/Api/RatingControllerTest.php index 01ed41b..b4499d1 100644 --- a/tests/Application/Controller/Api/RatingControllerTest.php +++ b/tests/Application/Controller/Api/RatingControllerTest.php @@ -9,6 +9,7 @@ use App\Factory\RestaurantFactory; use App\Factory\UserFactory; use App\Repository\RatingRepository; use App\Repository\SubjectRepository; +use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; class RatingControllerTest extends WebTestCase @@ -76,4 +77,42 @@ class RatingControllerTest extends WebTestCase $this->assertCount(1, $ratings); $this->assertInstanceOf(Rating::class, $ratings[0]); } + + public function testUpdate(): void + { + $client = static::createClient(); + + $user = UserFactory::createOne(); + $client->loginUser($user); + + $subject = RestaurantFactory::createOne([ + 'createdBy' => $user, + ]); + + $rating = RatingFactory::createOne([ + 'subject' => $subject, + 'score' => 0, + 'note' => 'first note', + ]); + + $client->request( + 'PATCH', + '/api/ratings/' . $rating->getId(), + server: ['CONTENT_TYPE' => 'application/json'], + content: json_encode([ + 'score' => 8, + 'note' => 'Other note', + ]), + ); + + $this->assertResponseIsSuccessful(); + $this->assertResponseStatusCodeSame(201); + + $em = static::getContainer()->get(EntityManagerInterface::class); + $em->clear(); + $updated = $em->getRepository(Rating::class)->find($rating->getId()); + + $this->assertSame(8, $updated->getScore()); + $this->assertSame('Other note', $updated->getNote()); + } }