Update create season response
dyb-tech.com/LaLiga-BackEnd/pipeline/head This commit looks good Details

This commit is contained in:
Daniel Guzman 2024-06-02 23:52:57 +02:00
parent fbb57e80de
commit fa24f75fea
9 changed files with 51 additions and 33 deletions

View File

@ -18,7 +18,7 @@ use Symfony\Component\Routing\Annotation\Route;
class LeagueController extends AbstractController 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 public function createLeague(Request $request, HandleCreateLeague $handleCreateLeague): JsonResponse
{ {
return $handleCreateLeague($request); return $handleCreateLeague($request);

View File

@ -30,7 +30,7 @@ class GameDto
'awayTeam' => $this->awayTeamDto->toArray() ?? [], 'awayTeam' => $this->awayTeamDto->toArray() ?? [],
'plannedDate' => !empty($this->plannedDate) ? $this->plannedDate->format('Y-m-d H:i:s') : null, '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, '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, 'notes' => $this->notes ?? null,
'facility' => $this->facilityDto->toArray() ?? [], 'facility' => $this->facilityDto->toArray() ?? [],
'pointsHome' => $this->pointsHome ?? 0, 'pointsHome' => $this->pointsHome ?? 0,

View File

@ -12,6 +12,8 @@ class SeasonDto
public array $teamDtoList; public array $teamDtoList;
public array $facilityDtoList; public array $facilityDtoList;
public int $leagueId; public int $leagueId;
public int $seasonNumber;
public string $leagueName;
public int $pointsPerWin; public int $pointsPerWin;
public int $pointsPerLoss; public int $pointsPerLoss;
public int $pointsPerDraw; public int $pointsPerDraw;
@ -20,30 +22,17 @@ class SeasonDto
public bool $active; public bool $active;
public array $validationErrors; 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 [ return [
'id' => $this->id ?? null, 'id' => $this->id ?? null,
'seasonNumber' => $this->seasonNumber ?? null,
'dateStart' => !empty($this->dateStart) ? $this->dateStart->format('Y-m'): null, 'dateStart' => !empty($this->dateStart) ? $this->dateStart->format('Y-m'): null,
'leagueId' => $this->leagueId ?? null, 'leagueId' => $this->leagueId ?? null,
'teamList' => $teamList, 'leagueName' => $this->leagueName ?? null,
'facilityList' => $facilityList, 'pointsPerWin' => $this->pointsPerWin ?? null,
'pointsPerDraw' => $this->pointsPerDraw ?? null,
'pointsPerLoss' => $this->pointsPerLoss ?? null,
'active' => $this->active ?? null 'active' => $this->active ?? null
]; ];
} }
@ -56,7 +45,7 @@ class SeasonDto
} }
if (!empty($dataList['dateStart'])) if (!empty($dataList['dateStart']))
{ {
$dateStart = \DateTime::createFromFormat('Y-m' ,$dataList['dateStart']); $dateStart = \DateTime::createFromFormat('Y-m', $dataList['dateStart']);
if ($dateStart) if ($dateStart)
{ {
$this->dateStart = $dateStart; $this->dateStart = $dateStart;

View File

@ -36,7 +36,7 @@ class LeagueRepository extends ServiceEntityRepository
// ; // ;
// } // }
// public function findOneBySomeField($value): ?League // public function getSeasonCount($value): ?League
// { // {
// return $this->createQueryBuilder('l') // return $this->createQueryBuilder('l')
// ->andWhere('l.exampleField = :val') // ->andWhere('l.exampleField = :val')

View File

@ -4,6 +4,7 @@ namespace DMD\LaLigaApi\Service\Season;
use DMD\LaLigaApi\Dto\SeasonDto; use DMD\LaLigaApi\Dto\SeasonDto;
use DMD\LaLigaApi\Entity\Season; use DMD\LaLigaApi\Entity\Season;
use DMD\LaLigaApi\Entity\Team;
class SeasonFactory class SeasonFactory
{ {

View File

@ -3,8 +3,12 @@
namespace DMD\LaLigaApi\Service\Season\createSeason; namespace DMD\LaLigaApi\Service\Season\createSeason;
use DMD\LaLigaApi\Dto\SeasonDto; 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\Exception\ValidationException;
use DMD\LaLigaApi\Repository\LeagueRepository; use DMD\LaLigaApi\Repository\LeagueRepository;
use DMD\LaLigaApi\Repository\SeasonRepository;
use DMD\LaLigaApi\Service\Common\AuthorizeRequest; use DMD\LaLigaApi\Service\Common\AuthorizeRequest;
use DMD\LaLigaApi\Service\Common\TeamFactory; use DMD\LaLigaApi\Service\Common\TeamFactory;
use DMD\LaLigaApi\Service\Season\SeasonFactory; use DMD\LaLigaApi\Service\Season\SeasonFactory;
@ -12,6 +16,7 @@ use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpException;
class HandleCreateSeason class HandleCreateSeason
{ {
@ -20,6 +25,7 @@ class HandleCreateSeason
public TeamFactory $teamFactory, public TeamFactory $teamFactory,
public AuthorizeRequest $authorizeRequest, public AuthorizeRequest $authorizeRequest,
public LeagueRepository $leagueRepository, public LeagueRepository $leagueRepository,
public SeasonRepository $seasonRepository,
public EntityManagerInterface $entityManager, public EntityManagerInterface $entityManager,
){} ){}
@ -30,21 +36,29 @@ class HandleCreateSeason
{ {
$this->authorizeRequest->authorizeLeaguePresident($leagueId); $this->authorizeRequest->authorizeLeaguePresident($leagueId);
$leagueEntity = $this->leagueRepository->find($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 = new SeasonDto();
$seasonDto->seasonNumber = $seasonCount + 1;
$seasonDto->leagueId = $leagueEntity->getId();
$seasonDto->leagueName = $leagueEntity->getName();
$seasonDto->fillFromArray($request->toArray()); $seasonDto->fillFromArray($request->toArray());
$seasonDto->validate(); $seasonDto->validate();
$numberOfTeams = $request->toArray()['numberOfTeams'];
$seasonEntity = $this->seasonFactory::create($seasonDto); $seasonEntity = $this->seasonFactory::create($seasonDto);
$seasonEntity->setLeague($leagueEntity); $seasonEntity->setLeague($leagueEntity);
if (!empty($seasonDto->teamDtoList)) if (!empty($numberOfTeams))
{ {
foreach ($seasonDto->teamDtoList as $teamDto) $this->createTeams($numberOfTeams, $seasonEntity);
{
$teamEntity = $this->teamFactory::create($teamDto);
$this->entityManager->persist($teamEntity);
}
} }
$this->entityManager->persist($seasonEntity); $this->entityManager->persist($seasonEntity);
$this->entityManager->flush(); $this->entityManager->flush();
@ -52,9 +66,23 @@ class HandleCreateSeason
return new JsonResponse( return new JsonResponse(
data: [ data: [
'success' => true, 'success' => true,
'season' => $seasonDto->toArray() 'season' => $seasonDto->createSeasonArray()
], ],
status: Response::HTTP_OK 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);
}
}
}
} }

View File

@ -33,7 +33,7 @@ class HandleGetAllSeason
{ {
$seasonDto = new SeasonDto(); $seasonDto = new SeasonDto();
$seasonDto->fillFromObject($seasonObj); $seasonDto->fillFromObject($seasonObj);
$seasonArray[] = $seasonDto->toArray(); $seasonArray[] = $seasonDto->createSeasonArray();
} }
} }
return new JsonResponse( return new JsonResponse(

View File

@ -36,7 +36,7 @@ class HandleGetSeasonById
return new JsonResponse( return new JsonResponse(
data: [ data: [
'success' => true, 'success' => true,
'season' => $seasonDto->toArray() 'season' => $seasonDto->createSeasonArray()
], ],
status: Response::HTTP_OK status: Response::HTTP_OK
); );

View File

@ -43,7 +43,7 @@ class HandleUpdateSeason
return new JsonResponse( return new JsonResponse(
data: [ data: [
'success' => true, 'success' => true,
'season' => $seasonDto->toArray() 'season' => $seasonDto->createSeasonArray()
], ],
status: Response::HTTP_OK status: Response::HTTP_OK
); );