welcome back to dyb-tech
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace DMD\LaLigaApi\Service\Season\createSeason;
|
||||
|
||||
use DMD\LaLigaApi\Dto\SeasonDto;
|
||||
use DMD\LaLigaApi\Exception\ValidationException;
|
||||
use DMD\LaLigaApi\Repository\LeagueRepository;
|
||||
use DMD\LaLigaApi\Service\Common\AuthorizeRequest;
|
||||
use DMD\LaLigaApi\Service\Common\TeamFactory;
|
||||
use DMD\LaLigaApi\Service\Season\SeasonFactory;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class HandleCreateSeason
|
||||
{
|
||||
public function __construct(
|
||||
public SeasonFactory $seasonFactory,
|
||||
public TeamFactory $teamFactory,
|
||||
public AuthorizeRequest $authorizeRequest,
|
||||
public LeagueRepository $leagueRepository,
|
||||
public EntityManagerInterface $entityManager,
|
||||
){}
|
||||
|
||||
/**
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function __invoke(Request $request, int $leagueId): JsonResponse
|
||||
{
|
||||
$this->authorizeRequest->authorizeLeaguePresident($leagueId);
|
||||
$leagueEntity = $this->leagueRepository->find($leagueId);
|
||||
$seasonDto = new SeasonDto();
|
||||
$seasonDto->fillFromArray($request->toArray());
|
||||
$seasonDto->validate();
|
||||
|
||||
$seasonEntity = $this->seasonFactory::create($seasonDto);
|
||||
$seasonEntity->setLeague($leagueEntity);
|
||||
|
||||
if (!empty($seasonDto->teamDtoList))
|
||||
{
|
||||
foreach ($seasonDto->teamDtoList as $teamDto)
|
||||
{
|
||||
$teamEntity = $this->teamFactory::create($teamDto);
|
||||
$this->entityManager->persist($teamEntity);
|
||||
}
|
||||
}
|
||||
$this->entityManager->persist($seasonEntity);
|
||||
$this->entityManager->flush();
|
||||
|
||||
$seasonDto->id = $seasonEntity->getId();
|
||||
return new JsonResponse(
|
||||
data: [
|
||||
'success' => true,
|
||||
'season' => $seasonDto->toArray()
|
||||
],
|
||||
status: Response::HTTP_OK
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user