Skip to content

Commit

Permalink
chore: updated logic on persist and update
Browse files Browse the repository at this point in the history
  • Loading branch information
benborla committed Nov 19, 2023
1 parent 8fd8147 commit c7be458
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
60 changes: 36 additions & 24 deletions api/src/Controller/FruitController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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);
Expand All @@ -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(
Expand All @@ -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());
}

/**
Expand Down Expand Up @@ -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;
}
}

0 comments on commit c7be458

Please sign in to comment.