Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 48 additions & 20 deletions config/routes/routes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -149,35 +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}/addresses
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
Expand All @@ -188,35 +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\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
Expand All @@ -227,35 +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\Api\PetController::showOwner
defaults:
_format: json
methods: GET

api_pets_show_owner_address:
path: api/v1/pets/{id}/owners/addresses
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
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Copy link
Collaborator

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

Suggested change
public function list()
public function index()

{
$clinics = $this->getDoctrine()
->getRepository('App\Entity\Clinic')
$clinics = $this->getDoctrine()->getRepository(Clinic::class)
->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);
Expand All @@ -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);
}

Expand All @@ -65,20 +56,21 @@ public function new(Request $request)
$clinic = new Clinic();
Copy link
Collaborator

Choose a reason for hiding this comment

The 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();
Copy link
Collaborator

Choose a reason for hiding this comment

The 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'));
Expand All @@ -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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This return $response is unnecessary. You can just return the new JsonResponse object, like this:

Suggested change
return $response;
return new JsonResponse(['msg' => 'We could not find'], Response::HTTP_OK);

}
}
Loading