-
Notifications
You must be signed in to change notification settings - Fork 0
REST principles and best practices #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -15,27 +15,19 @@ | |||||
| use Symfony\Component\Serializer\Serializer; | ||||||
| use Symfony\Component\HttpFoundation\Response; | ||||||
|
|
||||||
| class ApiClinicController extends AbstractController | ||||||
| class ClinicController extends AbstractController | ||||||
| { | ||||||
| public function index() | ||||||
| { | ||||||
| return new JsonResponse(['status' => 'ok']); | ||||||
| } | ||||||
|
|
||||||
| public function list() | ||||||
| { | ||||||
| $clinics = $this->getDoctrine() | ||||||
| ->getRepository('App\Entity\Clinic') | ||||||
| $clinics = $this->getDoctrine()->getRepository(Clinic::class) | ||||||
Luitame marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| ->findAll(); | ||||||
|
|
||||||
| $encoders = [new XmlEncoder(), new JsonEncoder()]; | ||||||
| $normalizers = [new ObjectNormalizer()]; | ||||||
|
|
||||||
| $serializer = new Serializer($normalizers, $encoders); | ||||||
| $jsonContent = $serializer->serialize($clinics, 'json', [ | ||||||
| 'circular_reference_handler' => function ($object) { | ||||||
| return $object->getId(); | ||||||
| } | ||||||
| 'ignored_attributes' => ['veterinaries'] | ||||||
| ]); | ||||||
|
|
||||||
| return new Response($jsonContent); | ||||||
|
|
@@ -48,10 +40,9 @@ public function show(Clinic $clinic) | |||||
|
|
||||||
| $serializer = new Serializer($normalizers, $encoders); | ||||||
| $jsonContent = $serializer->serialize($clinic, 'json', [ | ||||||
| 'circular_reference_handler' => function ($object) { | ||||||
| return $object->getId(); | ||||||
| } | ||||||
| 'ignored_attributes' => ['veterinaries'] | ||||||
| ]); | ||||||
|
|
||||||
| return new Response($jsonContent); | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -65,20 +56,21 @@ public function new(Request $request) | |||||
| $clinic = new Clinic(); | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, continue using forms |
||||||
| $clinic->setName($request->get('name')); | ||||||
| $clinic->setAddress($address); | ||||||
|
|
||||||
|
|
||||||
| $form = $this->createForm(ClinicType::class, $clinic); | ||||||
| $form->submit($clinic); | ||||||
|
|
||||||
| $entityManager = $this->getDoctrine()->getManager(); | ||||||
| $entityManager->persist($clinic); | ||||||
| $entityManager->flush(); | ||||||
|
|
||||||
| return new JsonResponse(['msg' => 'Clinic created whit success!'], Response::HTTP_OK); | ||||||
| $response = new JsonResponse(['msg'=>'Clinic created whit success!'], Response::HTTP_CREATED); | ||||||
|
|
||||||
| return $response; | ||||||
| } | ||||||
|
|
||||||
| public function edit(Request $request, $clinic) | ||||||
| public function edit(Request $request, Clinic $clinic) | ||||||
| { | ||||||
| if (empty($clinic)) { | ||||||
| return new JsonResponse(['msg' => 'Clinic not found!'], Response::HTTP_NOT_FOUND); | ||||||
| } | ||||||
|
|
||||||
| $address = $clinic->getAddress(); | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid to do that, please continue using forms |
||||||
| $address->setStreet($request->get('street')); | ||||||
| $address->setNumber($request->get('number')); | ||||||
|
|
@@ -87,30 +79,25 @@ public function edit(Request $request, $clinic) | |||||
| $clinic->setName($request->get('name')); | ||||||
| $clinic->setAddress($address); | ||||||
|
|
||||||
| if (!empty($clinic->getName())) { | ||||||
| $entityManager = $this->getDoctrine()->getManager(); | ||||||
| $entityManager->flush(); | ||||||
|
|
||||||
| return new JsonResponse(['msg' => 'Clinic edited whit success!'], Response::HTTP_OK); | ||||||
| } | ||||||
|
|
||||||
| return new JsonResponse(['msg' => 'Check the empty fields'], Response::HTTP_NOT_ACCEPTABLE); | ||||||
| $form = $this->createForm(ClinicType::class, $clinic); | ||||||
| $form->submit($clinic); | ||||||
|
|
||||||
| $entityManager = $this->getDoctrine()->getManager(); | ||||||
| $entityManager->flush(); | ||||||
|
|
||||||
| $response = new JsonResponse(['msg'=>'Clinic edited whit success!'], Response::HTTP_OK); | ||||||
|
|
||||||
| return $response; | ||||||
| } | ||||||
|
|
||||||
| public function delete($clinic) | ||||||
| public function delete(Clinic $clinic) | ||||||
| { | ||||||
| if (empty($clinic)) { | ||||||
| return new JsonResponse(['msg' => 'Clinic not found!'], Response::HTTP_NOT_FOUND); | ||||||
| } | ||||||
|
|
||||||
| if (!empty($clinic)) { | ||||||
| $entityManager = $this->getDoctrine()->getManager(); | ||||||
| $entityManager->remove($clinic); | ||||||
| $entityManager->flush(); | ||||||
|
|
||||||
| return new JsonResponse(['msg' => 'Clinic deleted whit success!'], Response::HTTP_OK); | ||||||
| } | ||||||
| $entityManager = $this->getDoctrine()->getManager(); | ||||||
| $entityManager->remove($clinic); | ||||||
| $entityManager->flush(); | ||||||
|
|
||||||
| return new JsonResponse(['msg' => 'We could not find'], Response::HTTP_NOT_ACCEPTABLE); | ||||||
| $response = new JsonResponse(['msg'=>'Clinic deleted whit success!'], Response::HTTP_OK); | ||||||
|
|
||||||
| return $response; | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This
Suggested change
|
||||||
| } | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's keep
index()is more semantical in terms on REST APIs