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
{
#[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);

View File

@ -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,

View File

@ -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;

View File

@ -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')

View File

@ -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
{

View File

@ -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);
}
}
}
}

View File

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

View File

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

View File

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