diff --git a/README.md b/README.md index efe87d6..216cb02 100644 --- a/README.md +++ b/README.md @@ -85,7 +85,7 @@ Sample Payload: "name": "test_name", "genus": "test_genus", "family": "test_family", - "fruit_order": "test_fruit_order", + "fruitOrder": "test_fruit_order", "carbohydrates": 0, "fat": 0, "protein": 0, diff --git a/api/src/Controller/FruitController.php b/api/src/Controller/FruitController.php index 5e3f113..14b88d2 100644 --- a/api/src/Controller/FruitController.php +++ b/api/src/Controller/FruitController.php @@ -64,17 +64,11 @@ public function fruitNew(Request $request, ValidatorInterface $validator): JsonR $fruit = $this->serializedForm($request, $fruit); - $violations = $validator->validate($fruit); - - // @INFO: Return an error upon validator errors - if (count($violations) > 0) { - $errors = []; - foreach ($violations as $violation) { - $errors[$violation->getPropertyPath()][] = $violation->getMessage(); - } - + //@INFO: Return validation errors + if ($errors = $this->validate($fruit, $validator)) { return $this->json(['errors' => $errors], Response::HTTP_UNPROCESSABLE_ENTITY); } + $this->em->persist($fruit); $this->em->flush(); @@ -90,7 +84,7 @@ public function fruitNew(Request $request, ValidatorInterface $validator): JsonR * @return \Symfony\Component\HttpFoundation\JsonResponse */ #[Route('/fruit/{id}', name: 'fruit', methods: ['GET', 'POST', 'PATCH', 'DELETE'])] - public function fruit(Request $request, FruitRepository $fruits) + public function fruit(Request $request, FruitRepository $fruits, ValidatorInterface $validator): JsonResponse { $id = (int) $request->attributes->get('id'); $fruit = $fruits->find($id); @@ -100,16 +94,6 @@ public function fruit(Request $request, FruitRepository $fruits) return $this->json([], Response::HTTP_NOT_FOUND); } - // @INFO: Return a resource - if ($request->getMethod() === Request::METHOD_GET) { - // @INFO: Throw a 404 if a resource is not found - if (!$fruit instanceof Fruit) { - return $this->json([], Response::HTTP_NOT_FOUND); - } - - return $this->json($fruit->toArray()); - } - // @INFO: Update and return a resource if ( in_array( @@ -118,19 +102,23 @@ public function fruit(Request $request, FruitRepository $fruits) ) ) { $fruit = $this->serializedForm($request, $fruit); + + //@INFO: Return validation errors + if ($errors = $this->validate($fruit, $validator)) { + return $this->json(['errors' => $errors], Response::HTTP_UNPROCESSABLE_ENTITY); + } + $fruit->setUpdatedAt(new \DateTime('now')); $this->em->flush(); - - return $this->json($fruit->toArray()); } // @INFO: Delete a resource if ($request->getMethod() === Request::METHOD_DELETE) { $this->em->remove($fruit); $this->em->flush(); - - return $this->json($fruit->toArray()); } + + return $this->json($fruit->toArray()); } /** @@ -255,4 +243,28 @@ private function serializedForm(Request $request, Fruit $fruit): Fruit ->setCalories($data['calories'] ?? $fruit->getCalories() ?: 0) ->setSource(Fruit::SOURCE_FROM_APP); } + + /** + * Ensure that the submitted data adheres the field requirement + * + * @param \App\Entity\Fruit $fruit + * @param Symfony\Component\Validator\Validator\ValidatorInterface $validator + * + * @return \Symfony\Component\HttpFoundation\JsonResponse + */ + private function validate(Fruit $fruit, ValidatorInterface $validator) + { + $violations = $validator->validate($fruit); + $errors = []; + + // @INFO: Return an error upon validator errors + if (count($violations) > 0) { + $errors = []; + foreach ($violations as $violation) { + $errors[$violation->getPropertyPath()][] = $violation->getMessage(); + } + } + + return $errors; + } }