diff --git a/src/Controller/Api/RatingController.php b/src/Controller/Api/RatingController.php index f68f8ca..a52e6e8 100644 --- a/src/Controller/Api/RatingController.php +++ b/src/Controller/Api/RatingController.php @@ -32,7 +32,7 @@ class RatingController extends AbstractController ], $repo->findAll() ), - ])); + ]), 200); } #[Route('/api/ratings', methods: ['POST'])] @@ -88,6 +88,27 @@ class RatingController extends AbstractController $em->flush(); - return $this->json(null, 201); + return $this->json(null, 202); + } + + #[Route('/api/ratings/{id}', methods: ['DELETE'])] + public function delete( + int $id, + EntityManagerInterface $em, + OutputService $output, + RatingRepository $ratingRepository, + ): JsonResponse { + $rating = $ratingRepository->findById($id); + + if ($rating === null) { + return $this->json( + $output->error(["UNKNOWN_RATING"]) + ); + } + + $em->remove($rating); + $em->flush(); + + return $this->json(null, 204); } } diff --git a/tests/Application/Controller/Api/RatingControllerTest.php b/tests/Application/Controller/Api/RatingControllerTest.php index b4499d1..0ff7aeb 100644 --- a/tests/Application/Controller/Api/RatingControllerTest.php +++ b/tests/Application/Controller/Api/RatingControllerTest.php @@ -106,7 +106,7 @@ class RatingControllerTest extends WebTestCase ); $this->assertResponseIsSuccessful(); - $this->assertResponseStatusCodeSame(201); + $this->assertResponseStatusCodeSame(202); $em = static::getContainer()->get(EntityManagerInterface::class); $em->clear(); @@ -115,4 +115,39 @@ class RatingControllerTest extends WebTestCase $this->assertSame(8, $updated->getScore()); $this->assertSame('Other note', $updated->getNote()); } + + public function testDelete(): 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', + ]); + $id = $rating->getId(); + + $client->request( + 'DELETE', + '/api/ratings/' . $id, + server: ['CONTENT_TYPE' => 'application/json'], + ); + + $this->assertResponseIsSuccessful(); + $this->assertResponseStatusCodeSame(204); + + $em = static::getContainer()->get(EntityManagerInterface::class); + $em->clear(); + + $this->assertNull( + $em->getRepository(Rating::class)->find($id) + ); + } }