Update create season response
dyb-tech.com/LaLiga-BackEnd/pipeline/head This commit looks good
Details
dyb-tech.com/LaLiga-BackEnd/pipeline/head This commit looks good
Details
This commit is contained in:
parent
fbb57e80de
commit
fa24f75fea
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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
|
||||
];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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')
|
||||
|
|
|
|||
|
|
@ -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
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -33,7 +33,7 @@ class HandleGetAllSeason
|
|||
{
|
||||
$seasonDto = new SeasonDto();
|
||||
$seasonDto->fillFromObject($seasonObj);
|
||||
$seasonArray[] = $seasonDto->toArray();
|
||||
$seasonArray[] = $seasonDto->createSeasonArray();
|
||||
}
|
||||
}
|
||||
return new JsonResponse(
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ class HandleGetSeasonById
|
|||
return new JsonResponse(
|
||||
data: [
|
||||
'success' => true,
|
||||
'season' => $seasonDto->toArray()
|
||||
'season' => $seasonDto->createSeasonArray()
|
||||
],
|
||||
status: Response::HTTP_OK
|
||||
);
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ class HandleUpdateSeason
|
|||
return new JsonResponse(
|
||||
data: [
|
||||
'success' => true,
|
||||
'season' => $seasonDto->toArray()
|
||||
'season' => $seasonDto->createSeasonArray()
|
||||
],
|
||||
status: Response::HTTP_OK
|
||||
);
|
||||
|
|
|
|||
Loading…
Reference in New Issue