From fa24f75feabb58aa932d4ba7de9753ae59406ba5 Mon Sep 17 00:00:00 2001 From: Daniel Guzman Date: Sun, 2 Jun 2024 23:52:57 +0200 Subject: [PATCH] Update create season response --- src/Controller/LeagueController.php | 2 +- src/Dto/GameDto.php | 2 +- src/Dto/SeasonDto.php | 29 ++++--------- src/Repository/LeagueRepository.php | 2 +- src/Service/Season/SeasonFactory.php | 1 + .../createSeason/HandleCreateSeason.php | 42 +++++++++++++++---- .../getAllSeasons/HandleGetAllSeason.php | 2 +- .../getSeasonById/HandleGetSeasonById.php | 2 +- .../updateSeason/HandleUpdateSeason.php | 2 +- 9 files changed, 51 insertions(+), 33 deletions(-) diff --git a/src/Controller/LeagueController.php b/src/Controller/LeagueController.php index 2fc9e3d3..345e246f 100644 --- a/src/Controller/LeagueController.php +++ b/src/Controller/LeagueController.php @@ -18,7 +18,7 @@ use Symfony\Component\Routing\Annotation\Route; class LeagueController extends AbstractController { - #[Route('/api/league/new', name: 'app_league', methods: ['POST'])] + #[Route('/api/league/create', name: 'app_league', methods: ['POST'])] public function createLeague(Request $request, HandleCreateLeague $handleCreateLeague): JsonResponse { return $handleCreateLeague($request); diff --git a/src/Dto/GameDto.php b/src/Dto/GameDto.php index 47dda089..104e10b4 100644 --- a/src/Dto/GameDto.php +++ b/src/Dto/GameDto.php @@ -30,7 +30,7 @@ class GameDto 'awayTeam' => $this->awayTeamDto->toArray() ?? [], 'plannedDate' => !empty($this->plannedDate) ? $this->plannedDate->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->toArray() ?? [], + 'season' => $this->seasonDto->createSeasonArray() ?? [], 'notes' => $this->notes ?? null, 'facility' => $this->facilityDto->toArray() ?? [], 'pointsHome' => $this->pointsHome ?? 0, diff --git a/src/Dto/SeasonDto.php b/src/Dto/SeasonDto.php index 78263ca0..816f817d 100644 --- a/src/Dto/SeasonDto.php +++ b/src/Dto/SeasonDto.php @@ -12,6 +12,8 @@ class SeasonDto public array $teamDtoList; public array $facilityDtoList; public int $leagueId; + public int $seasonNumber; + public string $leagueName; public int $pointsPerWin; public int $pointsPerLoss; public int $pointsPerDraw; @@ -20,30 +22,17 @@ class SeasonDto public bool $active; public array $validationErrors; - public function toArray(): array + public function createSeasonArray(): array { - $teamList = []; - $facilityList = []; - if (!empty($this->teamDtoList)) - { - foreach ($this->teamDtoList as $teamDto) - { - $teamList[] = $teamDto->toArray(); - } - } - if (!empty($this->facilityDtoList)) - { - foreach ($this->facilityDtoList as $facilityDto) - { - $facilityList[] = $facilityDto->toArray(); - } - } return [ 'id' => $this->id ?? null, + 'seasonNumber' => $this->seasonNumber ?? null, 'dateStart' => !empty($this->dateStart) ? $this->dateStart->format('Y-m'): null, 'leagueId' => $this->leagueId ?? null, - 'teamList' => $teamList, - 'facilityList' => $facilityList, + 'leagueName' => $this->leagueName ?? null, + 'pointsPerWin' => $this->pointsPerWin ?? null, + 'pointsPerDraw' => $this->pointsPerDraw ?? null, + 'pointsPerLoss' => $this->pointsPerLoss ?? null, 'active' => $this->active ?? null ]; } @@ -56,7 +45,7 @@ class SeasonDto } if (!empty($dataList['dateStart'])) { - $dateStart = \DateTime::createFromFormat('Y-m' ,$dataList['dateStart']); + $dateStart = \DateTime::createFromFormat('Y-m', $dataList['dateStart']); if ($dateStart) { $this->dateStart = $dateStart; diff --git a/src/Repository/LeagueRepository.php b/src/Repository/LeagueRepository.php index 7b08a34d..b9002b71 100644 --- a/src/Repository/LeagueRepository.php +++ b/src/Repository/LeagueRepository.php @@ -36,7 +36,7 @@ class LeagueRepository extends ServiceEntityRepository // ; // } -// public function findOneBySomeField($value): ?League +// public function getSeasonCount($value): ?League // { // return $this->createQueryBuilder('l') // ->andWhere('l.exampleField = :val') diff --git a/src/Service/Season/SeasonFactory.php b/src/Service/Season/SeasonFactory.php index 65ca29ff..16739a07 100644 --- a/src/Service/Season/SeasonFactory.php +++ b/src/Service/Season/SeasonFactory.php @@ -4,6 +4,7 @@ namespace DMD\LaLigaApi\Service\Season; use DMD\LaLigaApi\Dto\SeasonDto; use DMD\LaLigaApi\Entity\Season; +use DMD\LaLigaApi\Entity\Team; class SeasonFactory { diff --git a/src/Service/Season/createSeason/HandleCreateSeason.php b/src/Service/Season/createSeason/HandleCreateSeason.php index 9e07a96e..4d7fc1c7 100644 --- a/src/Service/Season/createSeason/HandleCreateSeason.php +++ b/src/Service/Season/createSeason/HandleCreateSeason.php @@ -3,8 +3,12 @@ namespace DMD\LaLigaApi\Service\Season\createSeason; use DMD\LaLigaApi\Dto\SeasonDto; +use DMD\LaLigaApi\Dto\TeamDto; +use DMD\LaLigaApi\Entity\Season; +use DMD\LaLigaApi\Entity\Team; use DMD\LaLigaApi\Exception\ValidationException; use DMD\LaLigaApi\Repository\LeagueRepository; +use DMD\LaLigaApi\Repository\SeasonRepository; use DMD\LaLigaApi\Service\Common\AuthorizeRequest; use DMD\LaLigaApi\Service\Common\TeamFactory; use DMD\LaLigaApi\Service\Season\SeasonFactory; @@ -12,6 +16,7 @@ use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\Exception\HttpException; class HandleCreateSeason { @@ -20,6 +25,7 @@ class HandleCreateSeason public TeamFactory $teamFactory, public AuthorizeRequest $authorizeRequest, public LeagueRepository $leagueRepository, + public SeasonRepository $seasonRepository, public EntityManagerInterface $entityManager, ){} @@ -30,21 +36,29 @@ class HandleCreateSeason { $this->authorizeRequest->authorizeLeaguePresident($leagueId); $leagueEntity = $this->leagueRepository->find($leagueId); + if (is_null($leagueEntity)) + { + throw new HttpException(Response::HTTP_NOT_FOUND, 'League not found'); + } + $seasonCount = $this->seasonRepository->count([ + 'league' => $leagueId + ]); $seasonDto = new SeasonDto(); + $seasonDto->seasonNumber = $seasonCount + 1; + $seasonDto->leagueId = $leagueEntity->getId(); + $seasonDto->leagueName = $leagueEntity->getName(); $seasonDto->fillFromArray($request->toArray()); $seasonDto->validate(); + $numberOfTeams = $request->toArray()['numberOfTeams']; $seasonEntity = $this->seasonFactory::create($seasonDto); $seasonEntity->setLeague($leagueEntity); - if (!empty($seasonDto->teamDtoList)) + if (!empty($numberOfTeams)) { - foreach ($seasonDto->teamDtoList as $teamDto) - { - $teamEntity = $this->teamFactory::create($teamDto); - $this->entityManager->persist($teamEntity); - } + $this->createTeams($numberOfTeams, $seasonEntity); } + $this->entityManager->persist($seasonEntity); $this->entityManager->flush(); @@ -52,9 +66,23 @@ class HandleCreateSeason return new JsonResponse( data: [ 'success' => true, - 'season' => $seasonDto->toArray() + 'season' => $seasonDto->createSeasonArray() ], status: Response::HTTP_OK ); } + + private function createTeams(int $numberOfTeams, Season $seasonEntity): void + { + if (!empty($numberOfTeams)) + { + for ($i = 0; $i < $numberOfTeams; $i++) + { + $teamEntity = new Team(); + $teamEntity->addSeason($seasonEntity); + $teamEntity->setName('Equipo '. $i+1); + $this->entityManager->persist($teamEntity); + } + } + } } \ No newline at end of file diff --git a/src/Service/Season/getAllSeasons/HandleGetAllSeason.php b/src/Service/Season/getAllSeasons/HandleGetAllSeason.php index e002cd69..8e491b2f 100644 --- a/src/Service/Season/getAllSeasons/HandleGetAllSeason.php +++ b/src/Service/Season/getAllSeasons/HandleGetAllSeason.php @@ -33,7 +33,7 @@ class HandleGetAllSeason { $seasonDto = new SeasonDto(); $seasonDto->fillFromObject($seasonObj); - $seasonArray[] = $seasonDto->toArray(); + $seasonArray[] = $seasonDto->createSeasonArray(); } } return new JsonResponse( diff --git a/src/Service/Season/getSeasonById/HandleGetSeasonById.php b/src/Service/Season/getSeasonById/HandleGetSeasonById.php index 81590694..bdabf04a 100644 --- a/src/Service/Season/getSeasonById/HandleGetSeasonById.php +++ b/src/Service/Season/getSeasonById/HandleGetSeasonById.php @@ -36,7 +36,7 @@ class HandleGetSeasonById return new JsonResponse( data: [ 'success' => true, - 'season' => $seasonDto->toArray() + 'season' => $seasonDto->createSeasonArray() ], status: Response::HTTP_OK ); diff --git a/src/Service/Season/updateSeason/HandleUpdateSeason.php b/src/Service/Season/updateSeason/HandleUpdateSeason.php index f8090f9b..8d1213cb 100644 --- a/src/Service/Season/updateSeason/HandleUpdateSeason.php +++ b/src/Service/Season/updateSeason/HandleUpdateSeason.php @@ -43,7 +43,7 @@ class HandleUpdateSeason return new JsonResponse( data: [ 'success' => true, - 'season' => $seasonDto->toArray() + 'season' => $seasonDto->createSeasonArray() ], status: Response::HTTP_OK );