Get user relationships
dyb-tech.com/LaLiga-BackEnd/pipeline/head This commit looks good Details

This commit is contained in:
Daniel Guzman 2024-08-11 03:01:22 +02:00
parent 44a0214187
commit eb5af013f0
2 changed files with 58 additions and 1 deletions

View File

@ -4,7 +4,7 @@ namespace DMD\LaLigaApi\Controller;
use DMD\LaLigaApi\Repository\UserRepository; use DMD\LaLigaApi\Repository\UserRepository;
use DMD\LaLigaApi\Service\User\Handlers\delete\HandleDeleteUser; 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\update\HandleUpdateUser;
use DMD\LaLigaApi\Service\User\Handlers\register\HandleRegistration; use DMD\LaLigaApi\Service\User\Handlers\register\HandleRegistration;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
@ -35,6 +35,12 @@ class UserController extends AbstractController
return $handleUpdateUser($request); 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'])] #[Route('/api/user/password', name: 'app_user_change_password', methods: ['PUT'])]
public function changePassword( public function changePassword(
Request $request, Request $request,

View File

@ -0,0 +1,51 @@
<?php
namespace DMD\LaLigaApi\Service\User\Handlers\getRelationships;
use DMD\LaLigaApi\Dto\UserDto;
use DMD\LaLigaApi\Entity\User;
use DMD\LaLigaApi\Repository\UserRepository;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpException;
class HandleGetRelationships
{
public function __construct(
public Security $security
){}
public function __invoke(): JsonResponse
{
$user = $this->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
);
}
}