From 442e3e324898633df90b8ac21f2fa63a99308bd8 Mon Sep 17 00:00:00 2001 From: Daniel Guzman Date: Thu, 25 Jul 2024 02:04:46 +0200 Subject: [PATCH] Add teams to response --- src/Dto/SeasonDto.php | 6 +++++ .../createSeason/HandleCreateSeason.php | 22 +++++++++++-------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/Dto/SeasonDto.php b/src/Dto/SeasonDto.php index d57b3c56..15de4996 100644 --- a/src/Dto/SeasonDto.php +++ b/src/Dto/SeasonDto.php @@ -30,9 +30,14 @@ class SeasonDto public function createSeasonArray(): array { $numberOfTeams = $this->numberOfTeams ?? 0; + $teamList = []; if (!empty($this->teamDtoList)) { $numberOfTeams = count($this->teamDtoList); + foreach ($this->teamDtoList as $teamDto) + { + $teamList[] = $teamDto->toArray(); + } } return [ 'id' => $this->id ?? null, @@ -42,6 +47,7 @@ class SeasonDto 'dateStart' => !empty($this->dateStart) ? $this->dateStart->format('Y-m-d'): null, 'leagueId' => $this->leagueId ?? null, 'leagueName' => $this->leagueName ?? null, + 'teams' => $teamList, 'pointsPerWin' => $this->pointsPerWin ?? null, 'pointsPerDraw' => $this->pointsPerDraw ?? null, 'pointsPerLoss' => $this->pointsPerLoss ?? null, diff --git a/src/Service/Season/createSeason/HandleCreateSeason.php b/src/Service/Season/createSeason/HandleCreateSeason.php index 9750182d..be7af2c3 100644 --- a/src/Service/Season/createSeason/HandleCreateSeason.php +++ b/src/Service/Season/createSeason/HandleCreateSeason.php @@ -58,11 +58,22 @@ class HandleCreateSeason { $this->createTeams($numberOfTeams, $seasonEntity, $seasonDto); } - $this->entityManager->persist($seasonEntity); $this->entityManager->flush(); - + $teamEntityList = $seasonEntity->getTeams(); + $teamDtoList = []; + if (!empty($teamEntityList)) + { + foreach ($teamEntityList as $teamEntity) + { + $teamDto = new TeamDto(); + $teamDto->name = $teamEntity->getName(); + $teamDto->id = $teamEntity->getId(); + $teamDtoList[] = $teamDto; + } + } $seasonDto->id = $seasonEntity->getId(); + $seasonDto->teamDtoList = $teamDtoList; return new JsonResponse( data: [ 'success' => true, @@ -78,18 +89,11 @@ class HandleCreateSeason { for ($i = 0; $i < $numberOfTeams; $i++) { - $teamDto = new TeamDto(); $teamEntity = new Team(); $teamEntity->addSeason($seasonEntity); $teamEntity->setName('Equipo '. $i+1); - $teamDto->name = $teamEntity->getName(); - $teamDto->active = true; $teamEntity->setActive(true); - $this->entityManager->persist($seasonEntity); $this->entityManager->persist($teamEntity); - $this->entityManager->flush(); - $teamDto->id = $teamEntity->getId(); - $seasonDto->teamDtoList[] = $teamDto; } } }