Create Facility
dyb-tech.com/LaLiga-BackEnd/pipeline/head This commit looks good Details

This commit is contained in:
Daniel Guzman 2024-06-08 23:00:17 +02:00
parent 33980dbb46
commit 08a43cb760
8 changed files with 83 additions and 78 deletions

View File

@ -3,6 +3,7 @@
namespace DMD\LaLigaApi\Controller; namespace DMD\LaLigaApi\Controller;
use DMD\LaLigaApi\Service\Season\addTeam\HandleAddTeam; use DMD\LaLigaApi\Service\Season\addTeam\HandleAddTeam;
use DMD\LaLigaApi\Service\Season\createFacilities\HandleCreateFacilities;
use DMD\LaLigaApi\Service\Season\createGameCalendar\HandleCreateGameCalendarRequest; use DMD\LaLigaApi\Service\Season\createGameCalendar\HandleCreateGameCalendarRequest;
use DMD\LaLigaApi\Service\Season\createSeason\HandleCreateSeason; use DMD\LaLigaApi\Service\Season\createSeason\HandleCreateSeason;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
@ -23,6 +24,11 @@ class SeasonController extends AbstractController
{ {
return $handleAddTeam($request, $leagueId, $seasonId); return $handleAddTeam($request, $leagueId, $seasonId);
} }
#[Route('/api/league/{leagueId}/season/{seasonId}/facilities/create', name: 'app_create_facilities', methods: ['POST'])]
public function createFacilities(Request $request, HandleCreateFacilities $handleAddTeam, int $leagueId, int $seasonId): JsonResponse
{
return $handleAddTeam($request, $leagueId, $seasonId);
}
#[Route('/api/league/{leagueId}/season/{seasonId}/calendar', name: 'app_create_calendar', methods: ['POST'])] #[Route('/api/league/{leagueId}/season/{seasonId}/calendar', name: 'app_create_calendar', methods: ['POST'])]
public function createCalendar(Request $request, HandleCreateGameCalendarRequest $handleCreateGameCalendarRequest, int $leagueId, int $seasonId): JsonResponse public function createCalendar(Request $request, HandleCreateGameCalendarRequest $handleCreateGameCalendarRequest, int $leagueId, int $seasonId): JsonResponse

View File

@ -18,16 +18,8 @@ class FacilityDto
public \DateTimeImmutable $createdAt; public \DateTimeImmutable $createdAt;
public array $validationErrors; public array $validationErrors;
public function toArray(): array public function createFacilityArray(): array
{ {
$seasonList = [];
if (!empty($this->seasonDtoList))
{
foreach ($this->seasonDtoList as $seasonDto)
{
$seasonList = $seasonDto->toArray();
}
}
return [ return [
'id' => $this->id ?? '', 'id' => $this->id ?? '',
'name' => $this->name ?? '', 'name' => $this->name ?? '',

View File

@ -32,7 +32,7 @@ class GameDto
'gameDateTime' => !empty($this->gameDateTime) ? $this->gameDateTime->format('Y-m-d H:i:s') : null, 'gameDateTime' => !empty($this->gameDateTime) ? $this->gameDateTime->format('Y-m-d H:i:s') : null,
'season' => $this->seasonDto->createSeasonArray() ?? [], 'season' => $this->seasonDto->createSeasonArray() ?? [],
'notes' => $this->notes ?? null, 'notes' => $this->notes ?? null,
'facility' => $this->facilityDto->toArray() ?? [], 'facility' => $this->facilityDto->createFacilityArray() ?? [],
'pointsHome' => $this->pointsHome ?? 0, 'pointsHome' => $this->pointsHome ?? 0,
'pointsAway' => $this->pointsAway ?? 0, 'pointsAway' => $this->pointsAway ?? 0,
'createdAt' => !empty($this->createdAt) ? $this->createdAt->format('Y-m-d H:i:s') : null, 'createdAt' => !empty($this->createdAt) ? $this->createdAt->format('Y-m-d H:i:s') : null,

View File

@ -1,11 +1,9 @@
<?php <?php
namespace DMD\LaLigaApi\Service\League; namespace DMD\LaLigaApi\Service\Common;
use DMD\LaLigaApi\Dto\FacilityDto; use DMD\LaLigaApi\Dto\FacilityDto;
use DMD\LaLigaApi\Dto\LeagueDto;
use DMD\LaLigaApi\Entity\Facility; use DMD\LaLigaApi\Entity\Facility;
use DMD\LaLigaApi\Entity\League;
class FacilityFactory class FacilityFactory
{ {
@ -20,9 +18,9 @@ class FacilityFactory
{ {
$facilityEntity->setName($facilityDto->address); $facilityEntity->setName($facilityDto->address);
} }
if (!empty($facilityDto->address)) if (!empty($facilityDto->availableHourList))
{ {
$facilityEntity->setName($facilityDto->address); $facilityEntity->setAvailableHours($facilityDto->availableHourList);
} }
$facilityEntity->setActive(true); $facilityEntity->setActive(true);
return $facilityEntity; return $facilityEntity;

View File

@ -1,59 +0,0 @@
<?php
namespace DMD\LaLigaApi\Service\League\createFacilities;
use DMD\LaLigaApi\Dto\FacilityDto;
use DMD\LaLigaApi\Dto\LeagueDto;
use DMD\LaLigaApi\Entity\CustomRole;
use DMD\LaLigaApi\Entity\League;
use DMD\LaLigaApi\Entity\User;
use DMD\LaLigaApi\Enum\Role;
use DMD\LaLigaApi\Exception\ValidationException;
use DMD\LaLigaApi\Repository\UserRepository;
use DMD\LaLigaApi\Service\League\FacilityFactory;
use DMD\LaLigaApi\Service\League\LeagueFactory;
use Doctrine\ORM\EntityManagerInterface;
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 HandleCreateFacilities
{
public function __construct(
public FacilityFactory $facilityFactory,
public EntityManagerInterface $entityManager,
public Security $security,
public UserRepository $userRepository
){}
/**
* @throws ValidationException
*/
public function __invoke(Request $request): JsonResponse
{
$userEntity = $this->userRepository->findOneBy([
'email' => $this->security->getUser()?->getUserIdentifier()
]);
if (is_null($userEntity)) {
throw new HttpException(Response::HTTP_FORBIDDEN, "Unauthorized.");
}
$facilityDto = new FacilityDto();
$facilityDto->fillFromArray($request->toArray());
$facilityDto->validate();
$facilityEntity = $this->facilityFactory::create($facilityDto);
$this->entityManager->persist($facilityEntity);
$this->entityManager->flush();
$facilityDto->id = $facilityEntity->getId();
$facilityDto->createdAt = $facilityEntity->getCreatedAt();
return new JsonResponse(
data: [
'success' => true,
'league' => $facilityDto->toArray()
],
status: Response::HTTP_OK
);
}
}

View File

@ -0,0 +1,68 @@
<?php
namespace DMD\LaLigaApi\Service\Season\createFacilities;
use DMD\LaLigaApi\Controller\SeasonController;
use DMD\LaLigaApi\Dto\FacilityDto;
use DMD\LaLigaApi\Entity\Season;
use DMD\LaLigaApi\Entity\User;
use DMD\LaLigaApi\Exception\ValidationException;
use DMD\LaLigaApi\Repository\SeasonRepository;
use DMD\LaLigaApi\Repository\UserRepository;
use DMD\LaLigaApi\Service\Common\AuthorizeRequest;
use DMD\LaLigaApi\Service\Common\FacilityFactory;
use Doctrine\ORM\EntityManagerInterface;
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 HandleCreateFacilities
{
public function __construct(
public AuthorizeRequest $authorizeRequest,
public FacilityFactory $facilityFactory,
public EntityManagerInterface $entityManager,
public Security $security,
public UserRepository $userRepository,
public SeasonRepository $seasonRepository
){}
/**
* @throws ValidationException
*/
public function __invoke(Request $request, int $seasonId, int $leagueId): JsonResponse
{
$userEntity = $this->security->getUser();
$this->authorizeRequest->authorizeLeaguePresident($leagueId);
if ((!$userEntity instanceof User))
{
throw new HttpException(Response::HTTP_FORBIDDEN, "Unauthorized.");
}
$seasonEntity = $this->seasonRepository->find($seasonId);
if (!$seasonEntity instanceof Season)
{
throw new HttpException(Response::HTTP_NOT_FOUND, 'Season not found, check id.');
}
$facilityDtoList = [];
foreach ($request->toArray() as $facilityItem)
{
$facilityDto = new FacilityDto();
$facilityDto->fillFromArray($facilityItem);
$facilityDto->validate();
$facilityEntity = $this->facilityFactory::create($facilityDto);
$facilityEntity->setSeason($seasonEntity);
$this->entityManager->persist($facilityEntity);
$facilityDtoList[] = $facilityDto->createFacilityArray();
}
$this->entityManager->flush();
return new JsonResponse(
data: [
'success' => true,
'facilities' => $facilityDtoList
],
status: Response::HTTP_OK
);
}
}

View File

@ -260,10 +260,10 @@ return [
'DMD\\LaLigaApi\\Service\\Common\\EmailSender' => true, 'DMD\\LaLigaApi\\Service\\Common\\EmailSender' => true,
'DMD\\LaLigaApi\\Service\\Common\\NotificationFactory' => true, 'DMD\\LaLigaApi\\Service\\Common\\NotificationFactory' => true,
'DMD\\LaLigaApi\\Service\\Common\\TeamFactory' => true, 'DMD\\LaLigaApi\\Service\\Common\\TeamFactory' => true,
'DMD\\LaLigaApi\\Service\\League\\FacilityFactory' => true, 'DMD\\LaLigaApi\\Service\\Common\\FacilityFactory' => true,
'DMD\\LaLigaApi\\Service\\League\\LeagueFactory' => true, 'DMD\\LaLigaApi\\Service\\League\\LeagueFactory' => true,
'DMD\\LaLigaApi\\Service\\League\\acceptJoinLeagueRequest\\HandleAcceptJoinLeagueRequest' => true, 'DMD\\LaLigaApi\\Service\\League\\acceptJoinLeagueRequest\\HandleAcceptJoinLeagueRequest' => true,
'DMD\\LaLigaApi\\Service\\League\\createFacilities\\HandleCreateFacilities' => true, 'DMD\\LaLigaApi\\Service\\Season\\createFacilities\\HandleCreateFacilities' => true,
'DMD\\LaLigaApi\\Service\\League\\createLeague\\HandleCreateLeague' => true, 'DMD\\LaLigaApi\\Service\\League\\createLeague\\HandleCreateLeague' => true,
'DMD\\LaLigaApi\\Service\\League\\declineJoinLeagueRequest\\HandleDeclineJoinLeagueRequest' => true, 'DMD\\LaLigaApi\\Service\\League\\declineJoinLeagueRequest\\HandleDeclineJoinLeagueRequest' => true,
'DMD\\LaLigaApi\\Service\\League\\getAllLeagues\\HandleGetAllLeagues' => true, 'DMD\\LaLigaApi\\Service\\League\\getAllLeagues\\HandleGetAllLeagues' => true,

View File

@ -260,10 +260,10 @@ return [
'DMD\\LaLigaApi\\Service\\Common\\EmailSender' => true, 'DMD\\LaLigaApi\\Service\\Common\\EmailSender' => true,
'DMD\\LaLigaApi\\Service\\Common\\NotificationFactory' => true, 'DMD\\LaLigaApi\\Service\\Common\\NotificationFactory' => true,
'DMD\\LaLigaApi\\Service\\Common\\TeamFactory' => true, 'DMD\\LaLigaApi\\Service\\Common\\TeamFactory' => true,
'DMD\\LaLigaApi\\Service\\League\\FacilityFactory' => true, 'DMD\\LaLigaApi\\Service\\Common\\FacilityFactory' => true,
'DMD\\LaLigaApi\\Service\\League\\LeagueFactory' => true, 'DMD\\LaLigaApi\\Service\\League\\LeagueFactory' => true,
'DMD\\LaLigaApi\\Service\\League\\acceptJoinLeagueRequest\\HandleAcceptJoinLeagueRequest' => true, 'DMD\\LaLigaApi\\Service\\League\\acceptJoinLeagueRequest\\HandleAcceptJoinLeagueRequest' => true,
'DMD\\LaLigaApi\\Service\\League\\createFacilities\\HandleCreateFacilities' => true, 'DMD\\LaLigaApi\\Service\\Season\\createFacilities\\HandleCreateFacilities' => true,
'DMD\\LaLigaApi\\Service\\League\\createLeague\\HandleCreateLeague' => true, 'DMD\\LaLigaApi\\Service\\League\\createLeague\\HandleCreateLeague' => true,
'DMD\\LaLigaApi\\Service\\League\\declineJoinLeagueRequest\\HandleDeclineJoinLeagueRequest' => true, 'DMD\\LaLigaApi\\Service\\League\\declineJoinLeagueRequest\\HandleDeclineJoinLeagueRequest' => true,
'DMD\\LaLigaApi\\Service\\League\\getAllLeagues\\HandleGetAllLeagues' => true, 'DMD\\LaLigaApi\\Service\\League\\getAllLeagues\\HandleGetAllLeagues' => true,