diff --git a/src/Controller/SeasonController.php b/src/Controller/SeasonController.php index 976913e3..f2934ae1 100644 --- a/src/Controller/SeasonController.php +++ b/src/Controller/SeasonController.php @@ -3,6 +3,7 @@ namespace DMD\LaLigaApi\Controller; 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\createSeason\HandleCreateSeason; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; @@ -23,6 +24,11 @@ class SeasonController extends AbstractController { 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'])] public function createCalendar(Request $request, HandleCreateGameCalendarRequest $handleCreateGameCalendarRequest, int $leagueId, int $seasonId): JsonResponse diff --git a/src/Dto/FacilityDto.php b/src/Dto/FacilityDto.php index 0376c486..e266561a 100644 --- a/src/Dto/FacilityDto.php +++ b/src/Dto/FacilityDto.php @@ -18,16 +18,8 @@ class FacilityDto public \DateTimeImmutable $createdAt; 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 [ 'id' => $this->id ?? '', 'name' => $this->name ?? '', diff --git a/src/Dto/GameDto.php b/src/Dto/GameDto.php index 104e10b4..f9c84f12 100644 --- a/src/Dto/GameDto.php +++ b/src/Dto/GameDto.php @@ -32,7 +32,7 @@ class GameDto 'gameDateTime' => !empty($this->gameDateTime) ? $this->gameDateTime->format('Y-m-d H:i:s') : null, 'season' => $this->seasonDto->createSeasonArray() ?? [], 'notes' => $this->notes ?? null, - 'facility' => $this->facilityDto->toArray() ?? [], + 'facility' => $this->facilityDto->createFacilityArray() ?? [], 'pointsHome' => $this->pointsHome ?? 0, 'pointsAway' => $this->pointsAway ?? 0, 'createdAt' => !empty($this->createdAt) ? $this->createdAt->format('Y-m-d H:i:s') : null, diff --git a/src/Service/League/FacilityFactory.php b/src/Service/Common/FacilityFactory.php similarity index 72% rename from src/Service/League/FacilityFactory.php rename to src/Service/Common/FacilityFactory.php index d2c9c269..3a8467e7 100644 --- a/src/Service/League/FacilityFactory.php +++ b/src/Service/Common/FacilityFactory.php @@ -1,11 +1,9 @@ setName($facilityDto->address); } - if (!empty($facilityDto->address)) + if (!empty($facilityDto->availableHourList)) { - $facilityEntity->setName($facilityDto->address); + $facilityEntity->setAvailableHours($facilityDto->availableHourList); } $facilityEntity->setActive(true); return $facilityEntity; diff --git a/src/Service/League/createFacilities/HandleCreateFacilities.php b/src/Service/League/createFacilities/HandleCreateFacilities.php deleted file mode 100644 index 6c252ee3..00000000 --- a/src/Service/League/createFacilities/HandleCreateFacilities.php +++ /dev/null @@ -1,59 +0,0 @@ -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 - ); - } -} \ No newline at end of file diff --git a/src/Service/Season/createFacilities/HandleCreateFacilities.php b/src/Service/Season/createFacilities/HandleCreateFacilities.php new file mode 100644 index 00000000..075cef58 --- /dev/null +++ b/src/Service/Season/createFacilities/HandleCreateFacilities.php @@ -0,0 +1,68 @@ +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 + ); + } +} \ No newline at end of file diff --git a/var/cache/dev/ContainerEXZktqY/removed-ids.php b/var/cache/dev/ContainerEXZktqY/removed-ids.php index ec345153..d26ac250 100644 --- a/var/cache/dev/ContainerEXZktqY/removed-ids.php +++ b/var/cache/dev/ContainerEXZktqY/removed-ids.php @@ -260,10 +260,10 @@ return [ 'DMD\\LaLigaApi\\Service\\Common\\EmailSender' => true, 'DMD\\LaLigaApi\\Service\\Common\\NotificationFactory' => 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\\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\\declineJoinLeagueRequest\\HandleDeclineJoinLeagueRequest' => true, 'DMD\\LaLigaApi\\Service\\League\\getAllLeagues\\HandleGetAllLeagues' => true, diff --git a/var/cache/dev/ContainerXpGVJqc/removed-ids.php b/var/cache/dev/ContainerXpGVJqc/removed-ids.php index 57d587d3..e3f23987 100644 --- a/var/cache/dev/ContainerXpGVJqc/removed-ids.php +++ b/var/cache/dev/ContainerXpGVJqc/removed-ids.php @@ -260,10 +260,10 @@ return [ 'DMD\\LaLigaApi\\Service\\Common\\EmailSender' => true, 'DMD\\LaLigaApi\\Service\\Common\\NotificationFactory' => 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\\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\\declineJoinLeagueRequest\\HandleDeclineJoinLeagueRequest' => true, 'DMD\\LaLigaApi\\Service\\League\\getAllLeagues\\HandleGetAllLeagues' => true,