src/Controller/CrewController.php line 98

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Crew;
  4. use App\Form\Type\CrewType;
  5. use App\Repository\ClientRepository;
  6. use App\Repository\CountryRepository;
  7. use App\Repository\CrewRepository;
  8. use App\Repository\RapperRepository;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. class CrewController extends AbstractController
  15. {
  16.     private CrewRepository $crewRepository;
  17.     private ClientRepository $clientRepository;
  18.     private RapperRepository $rapperRepository;
  19.     private UserDataController $userDataController;
  20.     private CountryRepository $countryRepository;
  21.     public function __construct(
  22.         CrewRepository $crewRepository,
  23.         ClientRepository $clientRepository,
  24.         RapperRepository $rapperRepository,
  25.         CountryRepository $countryRepository,
  26.         UserDataController $userDataController
  27.     )
  28.     {
  29.         $this->crewRepository $crewRepository;
  30.         $this->clientRepository $clientRepository;
  31.         $this->rapperRepository $rapperRepository;
  32.         $this->userDataController $userDataController;
  33.         $this->countryRepository $countryRepository;
  34.     }
  35.     #[Route(path'/create-crew'name'createCrew'methods: ["POST"])]
  36.     public function createCrewAction(Request $request): JsonResponse
  37.     {
  38.         $data $request->request->all();
  39.         $user $this->getUser();
  40.         $client $this->clientRepository->findOneBy(['email'=>$user->getUserIdentifier()]);
  41.         $crewName $data["crewName"];
  42.         $existCrewCheck $this->crewRepository->findOneBy(["name"=>$crewName]);
  43.         if($existCrewCheck!=null){
  44.             return new JsonResponse("Crew exists",400);
  45.         }
  46.         $newCrew = new Crew();
  47.         $newCrew
  48.             ->setName($crewName)
  49.             ->setCreatedBy($client)
  50.             ->setCity($client->getCity())
  51.             ->setCreatedAt(new \DateTime("now"));
  52.         $createdCrew $this->crewRepository->save($newCrew,true);
  53.         if($createdCrew->getId()!=null){
  54.             $client->setCrew($createdCrew);
  55.             $this->clientRepository->save($client,true);
  56.             $rapper $client->getRapper();
  57.             $rapper->setCrew($createdCrew);
  58.             $this->rapperRepository->save($rapper,true);
  59.             $data = [
  60.                 'id'=>$createdCrew->getId(),
  61.                 'name'=>$createdCrew->getName()
  62.             ];
  63.             return new JsonResponse($data,201);
  64.         }else{
  65.             return new JsonResponse("Error",400);
  66.         }
  67.     }
  68.     #[Route(path'/get-all-crews'name'getAllCrew')]
  69.     public function getAllCrewsRequest(): JsonResponse
  70.     {
  71.         $allCrews $this->crewRepository->findAll();
  72.         $data = [];
  73.         foreach ($allCrews as $crew){
  74.             $data = [
  75.                 'id'=>$crew->getId(),
  76.                 'name'=>$crew->getName()
  77.             ];
  78.         }
  79.         return new JsonResponse($data,200);
  80.     }
  81.     #[Route(path'/crews'name'crews')]
  82.     public function frontendCrewListAction(): Response
  83.     {
  84.         $user $this->getUser();
  85.         $crews $this->crewRepository->findAll();
  86.         if ($user==null){
  87.             return $this->render('pages/crews.html.twig',[
  88.                 "crews"=>$crews
  89.             ]);
  90.         }else{
  91.             $client $this->clientRepository->findOneByEmail($user->getUserIdentifier());
  92.             if($client!=null && $client->isIsActive()){
  93.                 return $this->render('pages/crews.html.twig',[
  94.                     'userData'=>$this->userDataController->getUserData(),
  95.                     "crews"=>$crews,
  96.                     "client"=>$client
  97.                 ]);
  98.             }else{
  99.                 return $this->render('pages/non-active-content.html.twig',[
  100.                     'userData'=>$this->userDataController->getUserData(),
  101.                     "crews"=>$crews,
  102.                     "client"=>$client
  103.                 ]);
  104.             }
  105.         }
  106.     }
  107.     #[Route(path'/crew/{id}'name'crewDetail')]
  108.     public function frontendCrewDetailAction($id): Response
  109.     {
  110.         $user $this->getUser();
  111.         $crew $this->crewRepository->findOneBy(['id'=>$id]);
  112.         if ($user==null){
  113.             return $this->render('pages/crew-detail.html.twig',[
  114.                 "crew"=>$crew,
  115.             ]);
  116.         }else{
  117.             $client $this->clientRepository->findOneByEmail($user->getUserIdentifier());
  118.             if($client!=null && $client->isIsActive()){
  119.                 return $this->render('pages/crew-detail.html.twig',[
  120.                     'userData'=>$this->userDataController->getUserData(),
  121.                     "crew"=>$crew
  122.                 ]);
  123.             }else{
  124.                 return $this->render('pages/non-active-content.html.twig',[
  125.                     'userData'=>$this->userDataController->getUserData(),
  126.                     "crew"=>$crew
  127.                 ]);
  128.             }
  129.         }
  130.     }
  131.     #[Route(path'/crew-edit'name'crewEdit',methods: ["POST"])]
  132.     public function frontendCrewEditAction(Request $request): JsonResponse
  133.     {
  134.         $data $request->request->all();
  135.         $id $data["id"];
  136.         $crew $this->crewRepository->findOneBy(["id"=>$id]);
  137.         $crewName=$data["crew"]["name"];
  138.         $crewDescription=$data["crew"]["description"];
  139.         $crewCity=$data["crew"]["city"];
  140.         $crewCountryID=$data["crew"]["country"];
  141.         $user $this->getUser();
  142.         $country=$this->countryRepository->findOneBy(["id"=>$crewCountryID]);
  143.         $crew
  144.             ->setName($crewName)
  145.             ->setDescription($crewDescription)
  146.             ->setCity($crewCity)
  147.             ->setCountry($country);
  148.         $form=$this->createForm(CrewType::class,$crew);
  149.         $form->handleRequest($request);
  150.         if($form->isSubmitted() && $form->isValid()){
  151.         }
  152.         if($this->crewRepository->save($crew,true)){
  153.             return new JsonResponse($crew,201);
  154.         }else{
  155.             return new JsonResponse("Error",400);
  156.         }
  157.     }
  158.     #[Route("/get-crews/"name"getCrewsApi")]
  159.     public  function getCrewsUrlAction(): JsonResponse
  160.     {
  161.         $crews=$this->crewRepository->findAll();
  162.         $data = [];
  163.         foreach ($crews as $crew){
  164.             $rappers=[];
  165.             if(count($crew->getRappers())>0){
  166.                 foreach ($crew->getRappers() as $rapper){
  167.                     $rappers[]=[
  168.                         'id' => $rapper->getId(),
  169.                         'pseudoname' => $rapper->getPseudoname()
  170.                     ];
  171.                 }
  172.             }
  173.             $data[] = [
  174.                 'id' => $crew->getId(),
  175.                 'name' => $crew->getName(),
  176.                 'rank' => $crew->getRank(),
  177.                 'imageName' => $crew->getImageName(),
  178.                 'backgroundImageName' => $crew->getBackgroundFileName(),
  179.                 'rappers'=>$rappers
  180.             ];
  181.         }
  182.         $jsonResponse = [
  183.             "crews" => $data
  184.         ];
  185.         return new JsonResponse($jsonResponse,200);
  186.     }
  187. }