From da7e5cf71170e57a095fc88f98b9952f3a753146 Mon Sep 17 00:00:00 2001 From: Micael Ferreira Date: Wed, 7 Aug 2019 19:58:48 -0300 Subject: [PATCH 1/4] Applying principles of REST and best pratices --- config/routes/routes.yaml | 35 +++++++++ src/Controller/ApiClinicController.php | 82 +++++++++---------- src/Controller/ApiPetController.php | 80 ++++++++----------- src/Controller/ApiUserController.php | 68 +++++++--------- src/Controller/ApiVeterinaryController.php | 91 +++++++++++----------- src/Entity/Pet.php | 2 +- src/Form/PetType.php | 36 +++++++++ 7 files changed, 215 insertions(+), 179 deletions(-) create mode 100644 src/Form/PetType.php diff --git a/config/routes/routes.yaml b/config/routes/routes.yaml index f8515ba..a765dda 100755 --- a/config/routes/routes.yaml +++ b/config/routes/routes.yaml @@ -161,6 +161,20 @@ api_clinics_show: _format: json methods: GET +# api_clinics_show_veterinaries: +# path: api/v1/clinics/{id}/veterinaries +# controller: App\Controller\ApiClinicController::showVeterinariesOfClinic +# defaults: +# _format: json +# methods: GET + +api_clinics_show_veterinaries: + path: api/v1/clinics/{id}/addresses + controller: App\Controller\ApiClinicController::showAddress + defaults: + _format: json + methods: GET + api_clinics_new: path: api/v1/clinics controller: App\Controller\ApiClinicController:new @@ -200,6 +214,13 @@ api_veterinaries_show: _format: json methods: GET +api_veterinaries_show_address: + path: api/v1/veterinaries/{id}/addresses + controller: App\Controller\ApiVeterinaryController::showAddress + defaults: + _format: json + methods: GET + api_veterinaries_new: path: api/v1/veterinaries controller: App\Controller\ApiVeterinaryController:new @@ -239,6 +260,20 @@ api_pets_show: _format: json methods: GET +api_pets_show_owner: + path: api/v1/pets/{id}/owners + controller: App\Controller\ApiPetController::showOwner + defaults: + _format: json + methods: GET + +api_pets_show_owner_address: + path: api/v1/pets/{id}/owners/addresses + controller: App\Controller\ApiPetController::showOwnerAddress + defaults: + _format: json + methods: GET + api_pets_new: path: api/v1/pets controller: App\Controller\ApiPetController:new diff --git a/src/Controller/ApiClinicController.php b/src/Controller/ApiClinicController.php index 8c7ffa6..c181bb9 100644 --- a/src/Controller/ApiClinicController.php +++ b/src/Controller/ApiClinicController.php @@ -17,15 +17,9 @@ class ApiClinicController extends AbstractController { - public function index() - { - return new JsonResponse(['status' => 'ok']); - } - public function list() { - $clinics = $this->getDoctrine() - ->getRepository('App\Entity\Clinic') + $clinics = $this->getDoctrine()->getRepository('App\Entity\Clinic') ->findAll(); $encoders = [new XmlEncoder(), new JsonEncoder()]; @@ -33,25 +27,25 @@ public function list() $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); } - public function show(Clinic $clinic) + public function show(Clinic $id) { + $clinic = $this->getDoctrine()->getRepository('App\Entity\Clinic') + ->find($id); + $encoders = [new XmlEncoder(), new JsonEncoder()]; $normalizers = [new ObjectNormalizer()]; $serializer = new Serializer($normalizers, $encoders); - $jsonContent = $serializer->serialize($clinic, 'json', [ - 'circular_reference_handler' => function ($object) { - return $object->getId(); - } + $jsonContent = $serializer->serialize($clinics, 'json', [ + 'ignored_attributes' => ['veterinaries'] ]); + return new Response($jsonContent); } @@ -65,20 +59,21 @@ public function new(Request $request) $clinic = new Clinic(); $clinic->setName($request->get('name')); $clinic->setAddress($address); - - $entityManager = $this->getDoctrine()->getManager(); - $entityManager->persist($clinic); - $entityManager->flush(); - return new JsonResponse(['msg' => 'Clinic created whit success!'], Response::HTTP_OK); + $form = $this->createForm(ClinicType::class, $clinic); + $form->submit($clinic); + + $em = $this->getDoctrine()->getManager(); + $em->persist($clinic); + $em->flush(); + + $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(); $address->setStreet($request->get('street')); $address->setNumber($request->get('number')); @@ -87,30 +82,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); + + $em = $this->getDoctrine()->getManager(); + $em->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; } } diff --git a/src/Controller/ApiPetController.php b/src/Controller/ApiPetController.php index 1368d5b..74ca9ee 100644 --- a/src/Controller/ApiPetController.php +++ b/src/Controller/ApiPetController.php @@ -5,11 +5,9 @@ use App\Entity\Pet; use App\Entity\User; use App\Form\PetType; - use Symfony\Component\HttpFoundation\Request; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Routing\Annotation\Route; - use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\Serializer\Encoder\JsonEncoder; use Symfony\Component\Serializer\Encoder\XmlEncoder; @@ -19,15 +17,9 @@ class ApiPetController extends AbstractController { - public function index() - { - return new JsonResponse(['status' => 'ok']); - } - public function list() { - $pets = $this->getDoctrine() - ->getRepository('App\Entity\Pet') + $pets = $this->getDoctrine()->getRepository('App\Entity\Pet') ->findAll(); $encoders = [new XmlEncoder(), new JsonEncoder()]; @@ -35,75 +27,69 @@ public function list() $serializer = new Serializer($normalizers, $encoders); $jsonContent = $serializer->serialize($pets, 'json', [ - 'circular_reference_handler' => function ($object) { - return $object->getId(); - } + 'ignored_attributes' => ['owner' => 'address'] ]); return new Response($jsonContent); } - public function show(Pet $pet) + public function show(Pet $id) { + $pet = $this->getDoctrine()->getRepository('App\Entity\Pet') + ->find($id); + $encoders = [new XmlEncoder(), new JsonEncoder()]; $normalizers = [new ObjectNormalizer()]; $serializer = new Serializer($normalizers, $encoders); $jsonContent = $serializer->serialize($pet, 'json', [ - 'circular_reference_handler' => function ($object) { - return $object->getId(); - } + 'ignored_attributes' => ['owner' => 'address'] ]); - + return new Response($jsonContent); } public function new(Request $request) { - $owner = $this->getDoctrine()->getRepository('App\Entity\User')->find($request->get('owner')); + // reference: https://symfonycasts.com/screencast/symfony-rest/form-post + $data = json_decode($request->getContent(), true); $pet = new Pet(); - $pet->setName($request->get('name')); - $pet->setDateBirth($request->get('dateBirth')); - $pet->setWeigth($request->get('weigth')); - $pet->setType($request->get('type')); - $pet->setBreed($request->get('breed')); - $pet->setOwner($owner); - + $form = $this->createForm(PetType::class, $pet); + $form->submit($data); + $entityManager = $this->getDoctrine()->getManager(); - $entityManager->persist($pet); $entityManager->flush(); + + $response = new Response('Pet created whit success!', Response::HTTP_CREATED); + $response->headers->set('Location', '/some/programmer/url'); - return new JsonResponse(['msg' => 'Pet created whit success!'], Response::HTTP_OK); + return $response; } - public function edit(Request $request, $pet) + public function edit(Request $request, Pet $id) { - $owner = $this->getDoctrine()->getRepository('App\Entity\User')->find($request->get('owner')); - if (empty($pet)) { - return new JsonResponse(['msg' => 'Pet not found!'], Response::HTTP_NOT_FOUND); - } + // reference: https://symfonycasts.com/screencast/symfony-rest/form-post + $data = json_decode($request->getContent(), true); - $pet->setName($request->get('name')); - $pet->setDateBirth($request->get('dateBirth')); - $pet->setWeigth($request->get('weigth')); - $pet->setType($request->get('type')); - $pet->setBreed($request->get('breed')); - $pet->setOwner($owner); - - if (!empty($pet->getPetName())) { - $entityManager = $this->getDoctrine()->getManager(); - $entityManager->flush(); + $pet = new Pet(); + $form = $this->createForm(PetType::class, $pet); + $form->submit($data); - return new JsonResponse(['msg' => 'Pet edited whit success!'], Response::HTTP_OK); - } + $entityManager = $this->getDoctrine()->getManager(); + $entityManager->flush(); - return new JsonResponse(['msg' => 'Check the empty fields'], Response::HTTP_NOT_ACCEPTABLE); + $response = new Response('Pet edited whit success!', Response::HTTP_OK); + $response->headers->set('Location', '/some/programmer/url'); + + return $response; } - public function delete($pet) + public function delete($id) { + $pet = $this->getDoctrine()->getRepository('App\Entity\Pet') + ->find($id); if (empty($pet)) { return new JsonResponse(['msg' => 'Pet not found!'], Response::HTTP_NOT_FOUND); @@ -117,6 +103,6 @@ public function delete($pet) return new JsonResponse(['msg' => 'Pet deleted whit success!'], Response::HTTP_OK); } - return new JsonResponse(['msg' => 'We could not find'], Response::HTTP_NOT_ACCEPTABLE); + return new JsonResponse(['msg' => 'We could not find'], Response::HTTP_BAD_REQUEST); } } diff --git a/src/Controller/ApiUserController.php b/src/Controller/ApiUserController.php index 58e0c76..cfa5fec 100644 --- a/src/Controller/ApiUserController.php +++ b/src/Controller/ApiUserController.php @@ -17,11 +17,6 @@ class ApiUserController extends AbstractController { - public function index() - { - return new JsonResponse(['status' => 'ok']); - } - public function list() { $users = $this->getDoctrine()->getRepository('App\Entity\User') @@ -36,8 +31,11 @@ public function list() return new Response($jsonContent); } - public function show(User $user) + public function show(User $id) { + $user = $this->getDoctrine()->getRepository('App\Entity\User') + ->find($id); + $encoders = [new XmlEncoder(), new JsonEncoder()]; $normalizers = [new ObjectNormalizer()]; @@ -55,58 +53,52 @@ public function new(Request $request) $address->setCity($request->get('city')); $user = new User(); - $user->setUserName($request->get('userName')); + $user->setUserName($request->get('name')); $user->setEmail($request->get('email')); $user->setAddress($address); $form = $this->createForm(UserType::class, $user); + $form->submit($user); - $entityManager = $this->getDoctrine()->getManager(); - $entityManager->persist($user); - $entityManager->flush(); + $em = $this->getDoctrine()->getManager(); + $em->persist($user); + $em->flush(); + + $response = new JsonResponse(['msg'=>'User created whit success!'], Response::HTTP_CREATED); - return new JsonResponse(['msg' => 'User created whit success!'], Response::HTTP_OK); + return $response; } - public function edit(Request $request, $user) + public function edit(Request $request, User $user) { - if (empty($user)) { - return new JsonResponse(['msg' => 'User not found!'], Response::HTTP_NOT_FOUND); - } - $address = $user->getAddress(); $address->setStreet($request->get('street')); $address->setNumber($request->get('number')); $address->setCity($request->get('city')); - $user->setUserName($request->get('userName')); + $user->setUserName($request->get('name')); $user->setEmail($request->get('email')); $user->setAddress($address); - if (!empty($user->getUserName())) { - $entityManager = $this->getDoctrine()->getManager(); - $entityManager->flush(); - - return new JsonResponse(['msg' => 'User edited whit success!'], Response::HTTP_OK); - } - - return new JsonResponse(['msg' => 'Check the empty fields'], Response::HTTP_NOT_ACCEPTABLE); + $form = $this->createForm(UserType::class, $user); + $form->submit($user); + + $em = $this->getDoctrine()->getManager(); + $em->flush(); + + $response = new JsonResponse(['msg'=>'User edited whit success!'], Response::HTTP_OK); + + return $response; } - public function delete($user) + public function delete(User $user) { - if (empty($user)) { - return new JsonResponse(['msg' => 'User not found!'], Response::HTTP_NOT_FOUND); - } - - if (!empty($user)) { - $entityManager = $this->getDoctrine()->getManager(); - $entityManager->remove($user); - $entityManager->flush(); - - return new JsonResponse(['msg' => 'User deleted whit success!'], Response::HTTP_OK); - } + $entityManager = $this->getDoctrine()->getManager(); + $entityManager->remove($user); + $entityManager->flush(); - return new JsonResponse(['msg' => 'We could not find'], Response::HTTP_NOT_ACCEPTABLE); + $response = new JsonResponse(['msg'=>'User deleted whit success!'], Response::HTTP_OK); + + return $response; } } diff --git a/src/Controller/ApiVeterinaryController.php b/src/Controller/ApiVeterinaryController.php index 4e7da5e..7ef3263 100644 --- a/src/Controller/ApiVeterinaryController.php +++ b/src/Controller/ApiVeterinaryController.php @@ -18,11 +18,6 @@ class ApiVeterinaryController extends AbstractController { - public function index() - { - return new JsonResponse(['status' => 'ok']); - } - public function list() { $veterinaries = $this->getDoctrine() @@ -34,25 +29,26 @@ public function list() $serializer = new Serializer($normalizers, $encoders); $jsonContent = $serializer->serialize($veterinaries, 'json', [ - 'circular_reference_handler' => function ($object) { - return $object->getId(); - } + 'ignored_attributes' => ['clinic'] ]); return new Response($jsonContent); } - public function show(Veterinary $veterinary) + public function show(Veterinary $id) { + $veterinary = $this->getDoctrine() + ->getRepository('App\Entity\Veterinary') + ->find($id); + $encoders = [new XmlEncoder(), new JsonEncoder()]; $normalizers = [new ObjectNormalizer()]; $serializer = new Serializer($normalizers, $encoders); $jsonContent = $serializer->serialize($veterinary, 'json', [ - 'circular_reference_handler' => function ($object) { - return $object->getId(); - } + 'ignored_attributes' => ['clinic'] ]); + return new Response($jsonContent); } @@ -63,60 +59,61 @@ public function new(Request $request) $address->setNumber($request->get('number')); $address->setCity($request->get('city')); + $clinic = $this->getDoctrine()->getRepository('App\Entity\Clinic') + ->find($request->get('clinic')); + $veterinary = new Veterinary(); $veterinary->setName($request->get('name')); $veterinary->setCrmv($request->get('crmv')); $veterinary->setAddress($address); - - $entityManager = $this->getDoctrine()->getManager(); - $entityManager->persist($veterinary); - $entityManager->flush(); + $veterinary->addClinic($clinic); + + $form = $this->createForm(VeterinaryType::class, $veterinary); + $form->submit($veterinary); - return new JsonResponse(['msg' => 'Veterinary created whit success!'], Response::HTTP_OK); + $em = $this->getDoctrine()->getManager(); + $em->persist($veterinary); + $em->flush(); + + $response = new JsonResponse(['msg'=>'Veterinary created whit success!'], Response::HTTP_CREATED); + + return $response; } - public function edit(Request $request, $veterinary) + public function edit(Request $request, Veterinary $veterinary) { - $clinic = $this->getDoctrine()->getRepository('App\Entity\Clinic')->find($request->get('clinic')); - - if (empty($veterinary)) { - return new JsonResponse(['msg' => 'Veterinary not found!'], Response::HTTP_NOT_FOUND); - } - - $address = $veterinary->getAddress(); + $address = new Address(); $address->setStreet($request->get('street')); $address->setNumber($request->get('number')); $address->setCity($request->get('city')); + $clinic = $this->getDoctrine()->getRepository('App\Entity\Clinic') + ->find($request->get('clinic')); + $veterinary->setName($request->get('name')); $veterinary->setCrmv($request->get('crmv')); - $veterinary->addClinic($clinic); $veterinary->setAddress($address); + $veterinary->addClinic($clinic); - if (!empty($veterinary->getName())) { - $entityManager = $this->getDoctrine()->getManager(); - $entityManager->flush(); - - return new JsonResponse(['msg' => 'Veterinary edited whit success!'], Response::HTTP_OK); - } - - return new JsonResponse(['msg' => 'Check the empty fields'], Response::HTTP_NOT_ACCEPTABLE); + $form = $this->createForm(VeterinaryType::class, $veterinary); + $form->submit($veterinary); + + $em = $this->getDoctrine()->getManager(); + $em->flush(); + + $response = new JsonResponse(['msg'=>'Veterinary edited whit success!'], Response::HTTP_OK); + + return $response; } - public function delete($veterinary) + public function delete(Veterinary $veterinary) { - if (empty($veterinary)) { - return new JsonResponse(['msg' => 'Veterinary not found!'], Response::HTTP_NOT_FOUND); - } - - if (!empty($veterinary)) { - $entityManager = $this->getDoctrine()->getManager(); - $entityManager->remove($veterinary); - $entityManager->flush(); - - return new JsonResponse(['msg' => 'Veterinary deleted whit success!'], Response::HTTP_OK); - } + $entityManager = $this->getDoctrine()->getManager(); + $entityManager->remove($veterinary); + $entityManager->flush(); - return new JsonResponse(['msg' => 'We could not find'], Response::HTTP_NOT_ACCEPTABLE); + $response = new JsonResponse(['msg'=>'Veterinary deleted whit success!'], Response::HTTP_OK); + + return $response; } } diff --git a/src/Entity/Pet.php b/src/Entity/Pet.php index b97623e..b283a01 100644 --- a/src/Entity/Pet.php +++ b/src/Entity/Pet.php @@ -68,7 +68,7 @@ public function getDateBirth(): ?\DateTimeInterface { return $this->dateBirth; } - + public function setDateBirth(\DateTimeInterface $dateBirth): self { $this->dateBirth = $dateBirth; diff --git a/src/Form/PetType.php b/src/Form/PetType.php new file mode 100644 index 0000000..d050d2f --- /dev/null +++ b/src/Form/PetType.php @@ -0,0 +1,36 @@ +add('name', TextType::class) + ->add('dateBirth', DateType::class, ['widget' => 'single_text']) + ->add('weight', TextType::class) + ->add('type', TextType::class) + ->add('breed', TextType::class) + ->add('owner', EntityType::class, [ + 'class' => User::class, + 'choice_label' => 'userName', + ]); + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults([ + 'data_class' => Pet::class, + ]); + } +} From 7ecfaad335ee9fca33200ca8c3233801eb555f48 Mon Sep 17 00:00:00 2001 From: Micael Ferreira Date: Wed, 21 Aug 2019 20:51:29 -0300 Subject: [PATCH 2/4] applying corrections suggestion from Codacy --- src/Controller/ApiClinicController.php | 19 ++++++++----------- src/Controller/ApiPetController.php | 16 ++++++---------- src/Controller/ApiUserController.php | 17 +++++++---------- src/Controller/ApiVeterinaryController.php | 18 +++++++----------- src/Form/PetType.php | 2 +- 5 files changed, 29 insertions(+), 43 deletions(-) diff --git a/src/Controller/ApiClinicController.php b/src/Controller/ApiClinicController.php index c181bb9..753f72c 100644 --- a/src/Controller/ApiClinicController.php +++ b/src/Controller/ApiClinicController.php @@ -19,7 +19,7 @@ class ApiClinicController extends AbstractController { public function list() { - $clinics = $this->getDoctrine()->getRepository('App\Entity\Clinic') + $clinics = $this->getDoctrine()->getRepository(Clinic::class) ->findAll(); $encoders = [new XmlEncoder(), new JsonEncoder()]; @@ -33,16 +33,13 @@ public function list() return new Response($jsonContent); } - public function show(Clinic $id) + public function show(Clinic $clinic) { - $clinic = $this->getDoctrine()->getRepository('App\Entity\Clinic') - ->find($id); - $encoders = [new XmlEncoder(), new JsonEncoder()]; $normalizers = [new ObjectNormalizer()]; $serializer = new Serializer($normalizers, $encoders); - $jsonContent = $serializer->serialize($clinics, 'json', [ + $jsonContent = $serializer->serialize($clinic, 'json', [ 'ignored_attributes' => ['veterinaries'] ]); @@ -63,9 +60,9 @@ public function new(Request $request) $form = $this->createForm(ClinicType::class, $clinic); $form->submit($clinic); - $em = $this->getDoctrine()->getManager(); - $em->persist($clinic); - $em->flush(); + $entityManager = $this->getDoctrine()->getManager(); + $entityManager->persist($clinic); + $entityManager->flush(); $response = new JsonResponse(['msg'=>'Clinic created whit success!'], Response::HTTP_CREATED); @@ -85,8 +82,8 @@ public function edit(Request $request, Clinic $clinic) $form = $this->createForm(ClinicType::class, $clinic); $form->submit($clinic); - $em = $this->getDoctrine()->getManager(); - $em->flush(); + $entityManager = $this->getDoctrine()->getManager(); + $entityManager->flush(); $response = new JsonResponse(['msg'=>'Clinic edited whit success!'], Response::HTTP_OK); diff --git a/src/Controller/ApiPetController.php b/src/Controller/ApiPetController.php index 74ca9ee..3bcc287 100644 --- a/src/Controller/ApiPetController.php +++ b/src/Controller/ApiPetController.php @@ -19,7 +19,7 @@ class ApiPetController extends AbstractController { public function list() { - $pets = $this->getDoctrine()->getRepository('App\Entity\Pet') + $pets = $this->getDoctrine()->getRepository(Pet::class) ->findAll(); $encoders = [new XmlEncoder(), new JsonEncoder()]; @@ -33,11 +33,8 @@ public function list() return new Response($jsonContent); } - public function show(Pet $id) + public function show(Pet $pet) { - $pet = $this->getDoctrine()->getRepository('App\Entity\Pet') - ->find($id); - $encoders = [new XmlEncoder(), new JsonEncoder()]; $normalizers = [new ObjectNormalizer()]; @@ -67,9 +64,8 @@ public function new(Request $request) return $response; } - public function edit(Request $request, Pet $id) + public function edit(Request $request, Pet $pet) { - // reference: https://symfonycasts.com/screencast/symfony-rest/form-post $data = json_decode($request->getContent(), true); @@ -86,10 +82,10 @@ public function edit(Request $request, Pet $id) return $response; } - public function delete($id) + public function delete(Pet $pet) { - $pet = $this->getDoctrine()->getRepository('App\Entity\Pet') - ->find($id); + $pet = $this->getDoctrine()->getRepository(Pet::class) + ->find($pet); if (empty($pet)) { return new JsonResponse(['msg' => 'Pet not found!'], Response::HTTP_NOT_FOUND); diff --git a/src/Controller/ApiUserController.php b/src/Controller/ApiUserController.php index cfa5fec..79744d1 100644 --- a/src/Controller/ApiUserController.php +++ b/src/Controller/ApiUserController.php @@ -19,7 +19,7 @@ class ApiUserController extends AbstractController { public function list() { - $users = $this->getDoctrine()->getRepository('App\Entity\User') + $users = $this->getDoctrine()->getRepository(User::class) ->findAll(); $encoders = [new XmlEncoder(), new JsonEncoder()]; @@ -31,11 +31,8 @@ public function list() return new Response($jsonContent); } - public function show(User $id) + public function show(User $user) { - $user = $this->getDoctrine()->getRepository('App\Entity\User') - ->find($id); - $encoders = [new XmlEncoder(), new JsonEncoder()]; $normalizers = [new ObjectNormalizer()]; @@ -60,9 +57,9 @@ public function new(Request $request) $form = $this->createForm(UserType::class, $user); $form->submit($user); - $em = $this->getDoctrine()->getManager(); - $em->persist($user); - $em->flush(); + $entityManager = $this->getDoctrine()->getManager(); + $entityManager->persist($user); + $entityManager->flush(); $response = new JsonResponse(['msg'=>'User created whit success!'], Response::HTTP_CREATED); @@ -83,8 +80,8 @@ public function edit(Request $request, User $user) $form = $this->createForm(UserType::class, $user); $form->submit($user); - $em = $this->getDoctrine()->getManager(); - $em->flush(); + $entityManager = $this->getDoctrine()->getManager(); + $entityManager->flush(); $response = new JsonResponse(['msg'=>'User edited whit success!'], Response::HTTP_OK); diff --git a/src/Controller/ApiVeterinaryController.php b/src/Controller/ApiVeterinaryController.php index 7ef3263..e02cb31 100644 --- a/src/Controller/ApiVeterinaryController.php +++ b/src/Controller/ApiVeterinaryController.php @@ -21,7 +21,7 @@ class ApiVeterinaryController extends AbstractController public function list() { $veterinaries = $this->getDoctrine() - ->getRepository('App\Entity\Veterinary') + ->getRepository(Veterinary::class) ->findAll(); $encoders = [new XmlEncoder(), new JsonEncoder()]; @@ -35,12 +35,8 @@ public function list() return new Response($jsonContent); } - public function show(Veterinary $id) + public function show(Veterinary $veterinary) { - $veterinary = $this->getDoctrine() - ->getRepository('App\Entity\Veterinary') - ->find($id); - $encoders = [new XmlEncoder(), new JsonEncoder()]; $normalizers = [new ObjectNormalizer()]; @@ -71,9 +67,9 @@ public function new(Request $request) $form = $this->createForm(VeterinaryType::class, $veterinary); $form->submit($veterinary); - $em = $this->getDoctrine()->getManager(); - $em->persist($veterinary); - $em->flush(); + $entityManager = $this->getDoctrine()->getManager(); + $entityManager->persist($veterinary); + $entityManager->flush(); $response = new JsonResponse(['msg'=>'Veterinary created whit success!'], Response::HTTP_CREATED); @@ -98,8 +94,8 @@ public function edit(Request $request, Veterinary $veterinary) $form = $this->createForm(VeterinaryType::class, $veterinary); $form->submit($veterinary); - $em = $this->getDoctrine()->getManager(); - $em->flush(); + $entityManager = $this->getDoctrine()->getManager(); + $entityManager->flush(); $response = new JsonResponse(['msg'=>'Veterinary edited whit success!'], Response::HTTP_OK); diff --git a/src/Form/PetType.php b/src/Form/PetType.php index d050d2f..0fb046c 100644 --- a/src/Form/PetType.php +++ b/src/Form/PetType.php @@ -14,7 +14,7 @@ class PetType extends AbstractType { - public function buildForm(FormBuilderInterface $builder, array $options) + public function buildForm(FormBuilderInterface $builder) { $builder->add('name', TextType::class) ->add('dateBirth', DateType::class, ['widget' => 'single_text']) From 7ad9eeae9e7a514f62a8fa74dd34bbd4f841ce73 Mon Sep 17 00:00:00 2001 From: Micael Ferreira Date: Thu, 22 Aug 2019 13:42:22 -0300 Subject: [PATCH 3/4] Applying use PHP namespaces properly for API --- .../{ApiClinicController.php => Api/ClinicController.php} | 2 +- src/Controller/{ApiPetController.php => Api/PetController.php} | 2 +- .../{ApiUserController.php => Api/UserController.php} | 2 +- .../VeterinaryController.php} | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) rename src/Controller/{ApiClinicController.php => Api/ClinicController.php} (98%) rename src/Controller/{ApiPetController.php => Api/PetController.php} (98%) rename src/Controller/{ApiUserController.php => Api/UserController.php} (98%) rename src/Controller/{ApiVeterinaryController.php => Api/VeterinaryController.php} (98%) diff --git a/src/Controller/ApiClinicController.php b/src/Controller/Api/ClinicController.php similarity index 98% rename from src/Controller/ApiClinicController.php rename to src/Controller/Api/ClinicController.php index 753f72c..d48ae5d 100644 --- a/src/Controller/ApiClinicController.php +++ b/src/Controller/Api/ClinicController.php @@ -15,7 +15,7 @@ use Symfony\Component\Serializer\Serializer; use Symfony\Component\HttpFoundation\Response; -class ApiClinicController extends AbstractController +class ClinicController extends AbstractController { public function list() { diff --git a/src/Controller/ApiPetController.php b/src/Controller/Api/PetController.php similarity index 98% rename from src/Controller/ApiPetController.php rename to src/Controller/Api/PetController.php index 3bcc287..1e3f8fd 100644 --- a/src/Controller/ApiPetController.php +++ b/src/Controller/Api/PetController.php @@ -15,7 +15,7 @@ use Symfony\Component\Serializer\Serializer; use Symfony\Component\HttpFoundation\Response; -class ApiPetController extends AbstractController +class PetController extends AbstractController { public function list() { diff --git a/src/Controller/ApiUserController.php b/src/Controller/Api/UserController.php similarity index 98% rename from src/Controller/ApiUserController.php rename to src/Controller/Api/UserController.php index 79744d1..832e709 100644 --- a/src/Controller/ApiUserController.php +++ b/src/Controller/Api/UserController.php @@ -15,7 +15,7 @@ use Symfony\Component\Serializer\Serializer; use Symfony\Component\HttpFoundation\Response; -class ApiUserController extends AbstractController +class UserController extends AbstractController { public function list() { diff --git a/src/Controller/ApiVeterinaryController.php b/src/Controller/Api/VeterinaryController.php similarity index 98% rename from src/Controller/ApiVeterinaryController.php rename to src/Controller/Api/VeterinaryController.php index e02cb31..18bb9f9 100644 --- a/src/Controller/ApiVeterinaryController.php +++ b/src/Controller/Api/VeterinaryController.php @@ -16,7 +16,7 @@ use Symfony\Component\Serializer\Serializer; use Symfony\Component\HttpFoundation\Response; -class ApiVeterinaryController extends AbstractController +class VeterinaryController extends AbstractController { public function list() { From 0901dde77a4b891f882951e5032e7de2c23b0674 Mon Sep 17 00:00:00 2001 From: Micael Ferreira Date: Thu, 22 Aug 2019 13:42:57 -0300 Subject: [PATCH 4/4] Applying use PHP namespaces properly for API --- config/routes/routes.yaml | 55 +++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 31 deletions(-) diff --git a/config/routes/routes.yaml b/config/routes/routes.yaml index a765dda..2750e40 100755 --- a/config/routes/routes.yaml +++ b/config/routes/routes.yaml @@ -110,35 +110,35 @@ pet_remove: api_users_list: path: api/v1/users - controller: App\Controller\ApiUserController::list + controller: App\Controller\Api\UserController::list defaults: _format: json methods: GET api_users_show: path: api/v1/users/{id} - controller: App\Controller\ApiUserController::show + controller: App\Controller\Api\UserController::show defaults: _format: json methods: GET api_users_new: path: api/v1/users - controller: App\Controller\ApiUserController:new + controller: App\Controller\Api\UserController:new defaults: _format: json methods: POST api_users_edit: path: api/v1/users/{id} - controller: App\Controller\ApiUserController::edit + controller: App\Controller\Api\UserController::edit defaults: _format: json methods: PUT api_users_delete: path: api/v1/users/{id} - controller: App\Controller\ApiUserController::delete + controller: App\Controller\Api\UserController::delete defaults: _format: json methods: DELETE @@ -149,49 +149,42 @@ api_users_delete: api_clinics_list: path: api/v1/clinics - controller: App\Controller\ApiClinicController::list + controller: App\Controller\Api\ClinicController::list defaults: _format: json methods: GET api_clinics_show: path: api/v1/clinics/{id} - controller: App\Controller\ApiClinicController::show + controller: App\Controller\Api\ClinicController::show defaults: _format: json methods: GET -# api_clinics_show_veterinaries: -# path: api/v1/clinics/{id}/veterinaries -# controller: App\Controller\ApiClinicController::showVeterinariesOfClinic -# defaults: -# _format: json -# methods: GET - api_clinics_show_veterinaries: path: api/v1/clinics/{id}/addresses - controller: App\Controller\ApiClinicController::showAddress + controller: App\Controller\Api\ClinicController::showAddress defaults: _format: json methods: GET api_clinics_new: path: api/v1/clinics - controller: App\Controller\ApiClinicController:new + controller: App\Controller\Api\ClinicController:new defaults: _format: json methods: POST api_clinics_edit: path: api/v1/clinics/{id} - controller: App\Controller\ApiClinicController::edit + controller: App\Controller\Api\ClinicController::edit defaults: _format: json methods: PUT api_clinics_delete: path: api/v1/clinics/{id} - controller: App\Controller\ApiClinicController::delete + controller: App\Controller\Api\ClinicController::delete defaults: _format: json methods: DELETE @@ -202,42 +195,42 @@ api_clinics_delete: api_veterinaries_list: path: api/v1/veterinaries - controller: App\Controller\ApiVeterinaryController::list + controller: App\Controller\Api\VeterinaryController::list defaults: _format: json methods: GET api_veterinaries_show: path: api/v1/veterinaries/{id} - controller: App\Controller\ApiVeterinaryController::show + controller: App\Controller\Api\VeterinaryController::show defaults: _format: json methods: GET api_veterinaries_show_address: path: api/v1/veterinaries/{id}/addresses - controller: App\Controller\ApiVeterinaryController::showAddress + controller: App\Controller\Api\VeterinaryController::showAddress defaults: _format: json methods: GET api_veterinaries_new: path: api/v1/veterinaries - controller: App\Controller\ApiVeterinaryController:new + controller: App\Controller\Api\VeterinaryController:new defaults: _format: json methods: POST api_veterinaries_edit: path: api/v1/veterinaries/{id} - controller: App\Controller\ApiVeterinaryController::edit + controller: App\Controller\Api\VeterinaryController::edit defaults: _format: json methods: PUT api_veterinaries_delete: path: api/v1/veterinaries/{id} - controller: App\Controller\ApiVeterinaryController::delete + controller: App\Controller\Api\VeterinaryController::delete defaults: _format: json methods: DELETE @@ -248,49 +241,49 @@ api_veterinaries_delete: api_pets_list: path: api/v1/pets - controller: App\Controller\ApiPetController::list + controller: App\Controller\Api\PetController::list defaults: _format: json methods: GET api_pets_show: path: api/v1/pets/{id} - controller: App\Controller\ApiPetController::show + controller: App\Controller\Api\PetController::show defaults: _format: json methods: GET api_pets_show_owner: path: api/v1/pets/{id}/owners - controller: App\Controller\ApiPetController::showOwner + controller: App\Controller\Api\PetController::showOwner defaults: _format: json methods: GET api_pets_show_owner_address: path: api/v1/pets/{id}/owners/addresses - controller: App\Controller\ApiPetController::showOwnerAddress + controller: App\Controller\Api\PetController::showOwnerAddress defaults: _format: json methods: GET api_pets_new: path: api/v1/pets - controller: App\Controller\ApiPetController:new + controller: App\Controller\Api\PetController:new defaults: _format: json methods: POST api_pets_edit: path: api/v1/pets/{id} - controller: App\Controller\ApiPetController::edit + controller: App\Controller\Api\PetController::edit defaults: _format: json methods: PUT api_pets_delete: path: api/v1/pets/{id} - controller: App\Controller\ApiPetController::delete + controller: App\Controller\Api\PetController::delete defaults: _format: json methods: DELETE \ No newline at end of file