From eb5af013f00ed643d142f7422a75e19b93575adc Mon Sep 17 00:00:00 2001 From: Daniel Guzman Date: Sun, 11 Aug 2024 03:01:22 +0200 Subject: [PATCH] Get user relationships --- src/Controller/UserController.php | 8 ++- .../HandleGetRelationships.php | 51 +++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/Service/User/Handlers/getRelationships/HandleGetRelationships.php diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index 89284061..36ef8882 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -4,7 +4,7 @@ namespace DMD\LaLigaApi\Controller; use DMD\LaLigaApi\Repository\UserRepository; use DMD\LaLigaApi\Service\User\Handlers\delete\HandleDeleteUser; -use DMD\LaLigaApi\Service\User\Handlers\getRelationships\HandleGetUserRelationships; +use DMD\LaLigaApi\Service\User\Handlers\getRelationships\HandleGetRelationships; use DMD\LaLigaApi\Service\User\Handlers\update\HandleUpdateUser; use DMD\LaLigaApi\Service\User\Handlers\register\HandleRegistration; use Doctrine\ORM\EntityManagerInterface; @@ -35,6 +35,12 @@ class UserController extends AbstractController return $handleUpdateUser($request); } + #[Route('/api/user/relationships', name: 'app_get_user_relationships', methods: ['GET'])] + public function getUserRelationships(Request $request, HandleGetRelationships $handleGetRelationships): JsonResponse + { + return $handleGetRelationships($request); + } + #[Route('/api/user/password', name: 'app_user_change_password', methods: ['PUT'])] public function changePassword( Request $request, diff --git a/src/Service/User/Handlers/getRelationships/HandleGetRelationships.php b/src/Service/User/Handlers/getRelationships/HandleGetRelationships.php new file mode 100644 index 00000000..f8686c3d --- /dev/null +++ b/src/Service/User/Handlers/getRelationships/HandleGetRelationships.php @@ -0,0 +1,51 @@ +security->getUser(); + if (!$user instanceof User) + { + throw new HttpException(Response::HTTP_NOT_FOUND, 'User not found'); + } + $customRoles = $user->getRoles(); + if (empty($customRoles)) + { + return new JsonResponse([ + 'success' => true, + 'relationships' => [] + ], Response::HTTP_OK); + } + $relationshipArray = []; + foreach ($customRoles as $customRoleEntity) + { + $relationshipArray[] = [ + 'role' => $customRoleEntity->getName(), + 'entityId' => $customRoleEntity->getEntityId() + ]; + } + return new JsonResponse( + data: [ + 'success' => true, + 'relationships' => $relationshipArray + ], + status: Response::HTTP_OK + ); + } +} \ No newline at end of file