Create Facility
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
33980dbb46
commit
08a43cb760
|
|
@ -3,6 +3,7 @@
|
|||
namespace DMD\LaLigaApi\Controller;
|
||||
|
||||
use DMD\LaLigaApi\Service\Season\addTeam\HandleAddTeam;
|
||||
use DMD\LaLigaApi\Service\Season\createFacilities\HandleCreateFacilities;
|
||||
use DMD\LaLigaApi\Service\Season\createGameCalendar\HandleCreateGameCalendarRequest;
|
||||
use DMD\LaLigaApi\Service\Season\createSeason\HandleCreateSeason;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
|
|
@ -23,6 +24,11 @@ class SeasonController extends AbstractController
|
|||
{
|
||||
return $handleAddTeam($request, $leagueId, $seasonId);
|
||||
}
|
||||
#[Route('/api/league/{leagueId}/season/{seasonId}/facilities/create', name: 'app_create_facilities', methods: ['POST'])]
|
||||
public function createFacilities(Request $request, HandleCreateFacilities $handleAddTeam, int $leagueId, int $seasonId): JsonResponse
|
||||
{
|
||||
return $handleAddTeam($request, $leagueId, $seasonId);
|
||||
}
|
||||
|
||||
#[Route('/api/league/{leagueId}/season/{seasonId}/calendar', name: 'app_create_calendar', methods: ['POST'])]
|
||||
public function createCalendar(Request $request, HandleCreateGameCalendarRequest $handleCreateGameCalendarRequest, int $leagueId, int $seasonId): JsonResponse
|
||||
|
|
|
|||
|
|
@ -18,16 +18,8 @@ class FacilityDto
|
|||
public \DateTimeImmutable $createdAt;
|
||||
public array $validationErrors;
|
||||
|
||||
public function toArray(): array
|
||||
public function createFacilityArray(): array
|
||||
{
|
||||
$seasonList = [];
|
||||
if (!empty($this->seasonDtoList))
|
||||
{
|
||||
foreach ($this->seasonDtoList as $seasonDto)
|
||||
{
|
||||
$seasonList = $seasonDto->toArray();
|
||||
}
|
||||
}
|
||||
return [
|
||||
'id' => $this->id ?? '',
|
||||
'name' => $this->name ?? '',
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ class GameDto
|
|||
'gameDateTime' => !empty($this->gameDateTime) ? $this->gameDateTime->format('Y-m-d H:i:s') : null,
|
||||
'season' => $this->seasonDto->createSeasonArray() ?? [],
|
||||
'notes' => $this->notes ?? null,
|
||||
'facility' => $this->facilityDto->toArray() ?? [],
|
||||
'facility' => $this->facilityDto->createFacilityArray() ?? [],
|
||||
'pointsHome' => $this->pointsHome ?? 0,
|
||||
'pointsAway' => $this->pointsAway ?? 0,
|
||||
'createdAt' => !empty($this->createdAt) ? $this->createdAt->format('Y-m-d H:i:s') : null,
|
||||
|
|
|
|||
|
|
@ -1,11 +1,9 @@
|
|||
<?php
|
||||
|
||||
namespace DMD\LaLigaApi\Service\League;
|
||||
namespace DMD\LaLigaApi\Service\Common;
|
||||
|
||||
use DMD\LaLigaApi\Dto\FacilityDto;
|
||||
use DMD\LaLigaApi\Dto\LeagueDto;
|
||||
use DMD\LaLigaApi\Entity\Facility;
|
||||
use DMD\LaLigaApi\Entity\League;
|
||||
|
||||
class FacilityFactory
|
||||
{
|
||||
|
|
@ -20,9 +18,9 @@ class FacilityFactory
|
|||
{
|
||||
$facilityEntity->setName($facilityDto->address);
|
||||
}
|
||||
if (!empty($facilityDto->address))
|
||||
if (!empty($facilityDto->availableHourList))
|
||||
{
|
||||
$facilityEntity->setName($facilityDto->address);
|
||||
$facilityEntity->setAvailableHours($facilityDto->availableHourList);
|
||||
}
|
||||
$facilityEntity->setActive(true);
|
||||
return $facilityEntity;
|
||||
|
|
@ -1,59 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace DMD\LaLigaApi\Service\League\createFacilities;
|
||||
|
||||
use DMD\LaLigaApi\Dto\FacilityDto;
|
||||
use DMD\LaLigaApi\Dto\LeagueDto;
|
||||
use DMD\LaLigaApi\Entity\CustomRole;
|
||||
use DMD\LaLigaApi\Entity\League;
|
||||
use DMD\LaLigaApi\Entity\User;
|
||||
use DMD\LaLigaApi\Enum\Role;
|
||||
use DMD\LaLigaApi\Exception\ValidationException;
|
||||
use DMD\LaLigaApi\Repository\UserRepository;
|
||||
use DMD\LaLigaApi\Service\League\FacilityFactory;
|
||||
use DMD\LaLigaApi\Service\League\LeagueFactory;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||
|
||||
class HandleCreateFacilities
|
||||
{
|
||||
public function __construct(
|
||||
public FacilityFactory $facilityFactory,
|
||||
public EntityManagerInterface $entityManager,
|
||||
public Security $security,
|
||||
public UserRepository $userRepository
|
||||
){}
|
||||
|
||||
/**
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function __invoke(Request $request): JsonResponse
|
||||
{
|
||||
$userEntity = $this->userRepository->findOneBy([
|
||||
'email' => $this->security->getUser()?->getUserIdentifier()
|
||||
]);
|
||||
if (is_null($userEntity)) {
|
||||
throw new HttpException(Response::HTTP_FORBIDDEN, "Unauthorized.");
|
||||
}
|
||||
$facilityDto = new FacilityDto();
|
||||
$facilityDto->fillFromArray($request->toArray());
|
||||
$facilityDto->validate();
|
||||
$facilityEntity = $this->facilityFactory::create($facilityDto);
|
||||
$this->entityManager->persist($facilityEntity);
|
||||
$this->entityManager->flush();
|
||||
|
||||
$facilityDto->id = $facilityEntity->getId();
|
||||
$facilityDto->createdAt = $facilityEntity->getCreatedAt();
|
||||
return new JsonResponse(
|
||||
data: [
|
||||
'success' => true,
|
||||
'league' => $facilityDto->toArray()
|
||||
],
|
||||
status: Response::HTTP_OK
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,68 @@
|
|||
<?php
|
||||
|
||||
namespace DMD\LaLigaApi\Service\Season\createFacilities;
|
||||
|
||||
use DMD\LaLigaApi\Controller\SeasonController;
|
||||
use DMD\LaLigaApi\Dto\FacilityDto;
|
||||
use DMD\LaLigaApi\Entity\Season;
|
||||
use DMD\LaLigaApi\Entity\User;
|
||||
use DMD\LaLigaApi\Exception\ValidationException;
|
||||
use DMD\LaLigaApi\Repository\SeasonRepository;
|
||||
use DMD\LaLigaApi\Repository\UserRepository;
|
||||
use DMD\LaLigaApi\Service\Common\AuthorizeRequest;
|
||||
use DMD\LaLigaApi\Service\Common\FacilityFactory;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||
|
||||
class HandleCreateFacilities
|
||||
{
|
||||
public function __construct(
|
||||
public AuthorizeRequest $authorizeRequest,
|
||||
public FacilityFactory $facilityFactory,
|
||||
public EntityManagerInterface $entityManager,
|
||||
public Security $security,
|
||||
public UserRepository $userRepository,
|
||||
public SeasonRepository $seasonRepository
|
||||
){}
|
||||
|
||||
/**
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function __invoke(Request $request, int $seasonId, int $leagueId): JsonResponse
|
||||
{
|
||||
$userEntity = $this->security->getUser();
|
||||
$this->authorizeRequest->authorizeLeaguePresident($leagueId);
|
||||
if ((!$userEntity instanceof User))
|
||||
{
|
||||
throw new HttpException(Response::HTTP_FORBIDDEN, "Unauthorized.");
|
||||
}
|
||||
$seasonEntity = $this->seasonRepository->find($seasonId);
|
||||
if (!$seasonEntity instanceof Season)
|
||||
{
|
||||
throw new HttpException(Response::HTTP_NOT_FOUND, 'Season not found, check id.');
|
||||
}
|
||||
$facilityDtoList = [];
|
||||
foreach ($request->toArray() as $facilityItem)
|
||||
{
|
||||
$facilityDto = new FacilityDto();
|
||||
$facilityDto->fillFromArray($facilityItem);
|
||||
$facilityDto->validate();
|
||||
$facilityEntity = $this->facilityFactory::create($facilityDto);
|
||||
$facilityEntity->setSeason($seasonEntity);
|
||||
$this->entityManager->persist($facilityEntity);
|
||||
$facilityDtoList[] = $facilityDto->createFacilityArray();
|
||||
}
|
||||
$this->entityManager->flush();
|
||||
return new JsonResponse(
|
||||
data: [
|
||||
'success' => true,
|
||||
'facilities' => $facilityDtoList
|
||||
],
|
||||
status: Response::HTTP_OK
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -260,10 +260,10 @@ return [
|
|||
'DMD\\LaLigaApi\\Service\\Common\\EmailSender' => true,
|
||||
'DMD\\LaLigaApi\\Service\\Common\\NotificationFactory' => true,
|
||||
'DMD\\LaLigaApi\\Service\\Common\\TeamFactory' => true,
|
||||
'DMD\\LaLigaApi\\Service\\League\\FacilityFactory' => true,
|
||||
'DMD\\LaLigaApi\\Service\\Common\\FacilityFactory' => true,
|
||||
'DMD\\LaLigaApi\\Service\\League\\LeagueFactory' => true,
|
||||
'DMD\\LaLigaApi\\Service\\League\\acceptJoinLeagueRequest\\HandleAcceptJoinLeagueRequest' => true,
|
||||
'DMD\\LaLigaApi\\Service\\League\\createFacilities\\HandleCreateFacilities' => true,
|
||||
'DMD\\LaLigaApi\\Service\\Season\\createFacilities\\HandleCreateFacilities' => true,
|
||||
'DMD\\LaLigaApi\\Service\\League\\createLeague\\HandleCreateLeague' => true,
|
||||
'DMD\\LaLigaApi\\Service\\League\\declineJoinLeagueRequest\\HandleDeclineJoinLeagueRequest' => true,
|
||||
'DMD\\LaLigaApi\\Service\\League\\getAllLeagues\\HandleGetAllLeagues' => true,
|
||||
|
|
|
|||
|
|
@ -260,10 +260,10 @@ return [
|
|||
'DMD\\LaLigaApi\\Service\\Common\\EmailSender' => true,
|
||||
'DMD\\LaLigaApi\\Service\\Common\\NotificationFactory' => true,
|
||||
'DMD\\LaLigaApi\\Service\\Common\\TeamFactory' => true,
|
||||
'DMD\\LaLigaApi\\Service\\League\\FacilityFactory' => true,
|
||||
'DMD\\LaLigaApi\\Service\\Common\\FacilityFactory' => true,
|
||||
'DMD\\LaLigaApi\\Service\\League\\LeagueFactory' => true,
|
||||
'DMD\\LaLigaApi\\Service\\League\\acceptJoinLeagueRequest\\HandleAcceptJoinLeagueRequest' => true,
|
||||
'DMD\\LaLigaApi\\Service\\League\\createFacilities\\HandleCreateFacilities' => true,
|
||||
'DMD\\LaLigaApi\\Service\\Season\\createFacilities\\HandleCreateFacilities' => true,
|
||||
'DMD\\LaLigaApi\\Service\\League\\createLeague\\HandleCreateLeague' => true,
|
||||
'DMD\\LaLigaApi\\Service\\League\\declineJoinLeagueRequest\\HandleDeclineJoinLeagueRequest' => true,
|
||||
'DMD\\LaLigaApi\\Service\\League\\getAllLeagues\\HandleGetAllLeagues' => true,
|
||||
|
|
|
|||
Loading…
Reference in New Issue