<?php
namespace App\Controller;
use App\Entity\Crew;
use App\Form\Type\CrewType;
use App\Repository\ClientRepository;
use App\Repository\CountryRepository;
use App\Repository\CrewRepository;
use App\Repository\RapperRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class CrewController extends AbstractController
{
private CrewRepository $crewRepository;
private ClientRepository $clientRepository;
private RapperRepository $rapperRepository;
private UserDataController $userDataController;
private CountryRepository $countryRepository;
public function __construct(
CrewRepository $crewRepository,
ClientRepository $clientRepository,
RapperRepository $rapperRepository,
CountryRepository $countryRepository,
UserDataController $userDataController
)
{
$this->crewRepository = $crewRepository;
$this->clientRepository = $clientRepository;
$this->rapperRepository = $rapperRepository;
$this->userDataController = $userDataController;
$this->countryRepository = $countryRepository;
}
#[Route(path: '/create-crew', name: 'createCrew', methods: ["POST"])]
public function createCrewAction(Request $request): JsonResponse
{
$data = $request->request->all();
$user = $this->getUser();
$client = $this->clientRepository->findOneBy(['email'=>$user->getUserIdentifier()]);
$crewName = $data["crewName"];
$existCrewCheck = $this->crewRepository->findOneBy(["name"=>$crewName]);
if($existCrewCheck!=null){
return new JsonResponse("Crew exists",400);
}
$newCrew = new Crew();
$newCrew
->setName($crewName)
->setCreatedBy($client)
->setCity($client->getCity())
->setCreatedAt(new \DateTime("now"));
$createdCrew = $this->crewRepository->save($newCrew,true);
if($createdCrew->getId()!=null){
$client->setCrew($createdCrew);
$this->clientRepository->save($client,true);
$rapper = $client->getRapper();
$rapper->setCrew($createdCrew);
$this->rapperRepository->save($rapper,true);
$data = [
'id'=>$createdCrew->getId(),
'name'=>$createdCrew->getName()
];
return new JsonResponse($data,201);
}else{
return new JsonResponse("Error",400);
}
}
#[Route(path: '/get-all-crews', name: 'getAllCrew')]
public function getAllCrewsRequest(): JsonResponse
{
$allCrews = $this->crewRepository->findAll();
$data = [];
foreach ($allCrews as $crew){
$data = [
'id'=>$crew->getId(),
'name'=>$crew->getName()
];
}
return new JsonResponse($data,200);
}
#[Route(path: '/crews', name: 'crews')]
public function frontendCrewListAction(): Response
{
$user = $this->getUser();
$crews = $this->crewRepository->findAll();
if ($user==null){
return $this->render('pages/crews.html.twig',[
"crews"=>$crews
]);
}else{
$client = $this->clientRepository->findOneByEmail($user->getUserIdentifier());
if($client!=null && $client->isIsActive()){
return $this->render('pages/crews.html.twig',[
'userData'=>$this->userDataController->getUserData(),
"crews"=>$crews,
"client"=>$client
]);
}else{
return $this->render('pages/non-active-content.html.twig',[
'userData'=>$this->userDataController->getUserData(),
"crews"=>$crews,
"client"=>$client
]);
}
}
}
#[Route(path: '/crew/{id}', name: 'crewDetail')]
public function frontendCrewDetailAction($id): Response
{
$user = $this->getUser();
$crew = $this->crewRepository->findOneBy(['id'=>$id]);
if ($user==null){
return $this->render('pages/crew-detail.html.twig',[
"crew"=>$crew,
]);
}else{
$client = $this->clientRepository->findOneByEmail($user->getUserIdentifier());
if($client!=null && $client->isIsActive()){
return $this->render('pages/crew-detail.html.twig',[
'userData'=>$this->userDataController->getUserData(),
"crew"=>$crew
]);
}else{
return $this->render('pages/non-active-content.html.twig',[
'userData'=>$this->userDataController->getUserData(),
"crew"=>$crew
]);
}
}
}
#[Route(path: '/crew-edit', name: 'crewEdit',methods: ["POST"])]
public function frontendCrewEditAction(Request $request): JsonResponse
{
$data = $request->request->all();
$id = $data["id"];
$crew = $this->crewRepository->findOneBy(["id"=>$id]);
$crewName=$data["crew"]["name"];
$crewDescription=$data["crew"]["description"];
$crewCity=$data["crew"]["city"];
$crewCountryID=$data["crew"]["country"];
$user = $this->getUser();
$country=$this->countryRepository->findOneBy(["id"=>$crewCountryID]);
$crew
->setName($crewName)
->setDescription($crewDescription)
->setCity($crewCity)
->setCountry($country);
$form=$this->createForm(CrewType::class,$crew);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()){
}
if($this->crewRepository->save($crew,true)){
return new JsonResponse($crew,201);
}else{
return new JsonResponse("Error",400);
}
}
#[Route("/get-crews/", name: "getCrewsApi")]
public function getCrewsUrlAction(): JsonResponse
{
$crews=$this->crewRepository->findAll();
$data = [];
foreach ($crews as $crew){
$rappers=[];
if(count($crew->getRappers())>0){
foreach ($crew->getRappers() as $rapper){
$rappers[]=[
'id' => $rapper->getId(),
'pseudoname' => $rapper->getPseudoname()
];
}
}
$data[] = [
'id' => $crew->getId(),
'name' => $crew->getName(),
'rank' => $crew->getRank(),
'imageName' => $crew->getImageName(),
'backgroundImageName' => $crew->getBackgroundFileName(),
'rappers'=>$rappers
];
}
$jsonResponse = [
"crews" => $data
];
return new JsonResponse($jsonResponse,200);
}
}