src/Controller/RapperController.php line 57

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\ClientNotification;
  4. use App\Entity\NotificationType;
  5. use App\Entity\Rapper;
  6. use App\Form\Type\RapperType;
  7. use App\Repository\BattleRepository;
  8. use App\Repository\ClientNotificationRepository;
  9. use App\Repository\ClientRepository;
  10. use App\Repository\CrewRepository;
  11. use App\Repository\RapperRepository;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\Form\Extension\Core\Type\FormType;
  14. use Symfony\Component\Form\Form;
  15. use Symfony\Component\HttpFoundation\JsonResponse;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. use Symfony\Component\Security\Core\Security;
  20. use Vich\UploaderBundle\Form\Type\VichImageType;
  21. class RapperController extends AbstractController
  22. {
  23.     private RapperRepository $rapperRepository;
  24.     private BattleRepository $battleRepository;
  25.     private ClientRepository $clientRepository;
  26.     private CrewRepository $crewRepository;
  27.     private UserDataController $userDataController;
  28.     private FrontendController $frontendController;
  29.     private ClientNotificationRepository $clientNotificationRepository;
  30.     public function __construct(
  31.         RapperRepository $rapperRepository,
  32.         BattleRepository $battleRepository,
  33.         ClientRepository $clientRepository,
  34.         CrewRepository $crewRepository,
  35.         FrontendController $frontendController,
  36.         UserDataController $userDataController,
  37.         ClientNotificationRepository $clientNotificationRepository
  38.     )
  39.     {
  40.         $this->rapperRepository=$rapperRepository;
  41.         $this->battleRepository=$battleRepository;
  42.         $this->clientRepository=$clientRepository;
  43.         $this->userDataController $userDataController;
  44.         $this->frontendController $frontendController;
  45.         $this->crewRepository $crewRepository;
  46.         $this->clientNotificationRepository $clientNotificationRepository;
  47.     }
  48.     #[Route(path:  [
  49.         'cs' => '/raperi',
  50.         'en' => '/rappers'
  51.     ], name'rappers')]
  52.     public function frontendRappersAction(): Response
  53.     {
  54.         $user $this->getUser();
  55.         $allRappers $this->rapperRepository->findAll();
  56.         if ($user==null){
  57.             return $this->render('pages/rappers.html.twig',[
  58.                 "rappers"=>$allRappers
  59.             ]);
  60.         }else{
  61.             $client $this->clientRepository->findOneByEmail($user->getUserIdentifier());
  62.             if($client!=null && $client->isIsActive()){
  63.                 return $this->render('pages/rappers.html.twig',[
  64.                     'userData'=>$this->userDataController->getUserData(),
  65.                     'rappers'=>$allRappers
  66.                 ]);
  67.             }else{
  68.                 return $this->render('pages/non-active-content.html.twig',[
  69.                     'userData'=>$this->userDataController->getUserData(),
  70.                     'rappers'=>$allRappers
  71.                 ]);
  72.             }
  73.         }
  74.     }
  75.     #[Route(path:  [
  76.         'cs' => '/raper/{id}',
  77.         'en' => '/rapper/{id}'
  78.     ], name'rapperDetail')]
  79.     public function frontendRapperDetailAction($id): Response
  80.     {
  81.         $user $this->getUser();
  82.         $rapper $this->rapperRepository->findOneBy(['id'=>$id]);
  83.         $latestBattles = [];
  84.         $openedBattles = [];
  85.         if($rapper!=null){
  86.             if($this->battleRepository->findLatestForRapper($rapper,6)!=null){
  87.                 $latestBattles=$this->battleRepository->findLatestForRapper($rapper,6);
  88.             }
  89.             if($this->battleRepository->findOpenedForRapper($rapper,6)!=null){
  90.                 $openedBattles $this->battleRepository->findOpenedForRapper($rapper,6);
  91.             }
  92.         }
  93.         if ($user==null){
  94.             return $this->render('pages/rapper-detail.html.twig',[
  95.                 "rapper"=>$rapper,
  96.                 "latestBattles"=>$latestBattles,
  97.                 "openedBattles"=>$openedBattles
  98.             ]);
  99.         }else{
  100.             $client $this->clientRepository->findOneByEmail($user->getUserIdentifier());
  101.             if($client!=null && $client->isIsActive()){
  102.                 return $this->render('pages/rapper-detail.html.twig',[
  103.                     'userData'=>$this->userDataController->getUserData(),
  104.                     "rapper"=>$rapper,
  105.                     "latestBattles"=>$latestBattles,
  106.                     "openedBattles"=>$openedBattles
  107.                 ]);
  108.             }else{
  109.                 return $this->render('pages/non-active-content.html.twig',[
  110.                     'userData'=>$this->userDataController->getUserData(),
  111.                     "rapper"=>$rapper,
  112.                     "latestBattles"=>$latestBattles,
  113.                     "openedBattles"=>$openedBattles
  114.                 ]);
  115.             }
  116.         }
  117.     }
  118.     #[Route(path'/rapper-create'name'rapperCreate'methods: ['POST'])]
  119.     public function frontendRapperCreateAction(Request $request): Response
  120.     {
  121.         $data $request->request->all();
  122.         $user $this->getUser();
  123.         $client $this->clientRepository->findOneByEmail($user->getUserIdentifier());
  124.         $avatar $request->files->get('avatarFile');
  125.         $crew null;
  126.         if(intval($data['crew'])!==0){
  127.             $crew $this->crewRepository->findOneBy(['id'=>intval($data['crew'])]);
  128.         }
  129.         $newRapper = new Rapper();
  130.         $form=$this->createForm(RapperType::class,$newRapper);
  131.         $form->handleRequest($request);
  132.         if($form->isSubmitted() && $form->isValid()) {
  133.             $newRapper
  134.                 ->setClient($client)
  135.                 ->setRank(500)
  136.                 ->setPseudoname($data['rapper']['pseudoname'])
  137.                 ->setRapperStyle($data['rapper']['rapperStyle'])
  138.                 ->setBio($data['rapper']['bio'])
  139.                 ->setCreatedAt(new \DateTime('now'))
  140.                 ->setUpdatedAt(new \DateTime('now'));
  141.             if($crew!=null){
  142.                 $newRapper->setCrew($crew);
  143.             }
  144.             if($this->rapperRepository->save($newRapper,true)) {
  145.                 return $this->redirectToRoute("profile");
  146.             }
  147.         }
  148.         return $this->redirectToRoute("errorPage");
  149.     }
  150.     #[Route(path'/rapper-edit'name'rapperEdit'methods: ['POST'])]
  151.     public function frontendRapperEditAction(Request $request): Response
  152.     {
  153.         $data $request->request->all();
  154.         $user $this->getUser();
  155.         $client $this->clientRepository->findOneByEmail($user->getUserIdentifier());
  156.         $avatarNew $request->files->get('avatarFile');
  157.         $editRapper $client->getRapper();
  158.         $form $this->createForm(RapperType::class, $editRapper);
  159.         $form->handleRequest($request);
  160.         $crewId=$data['form']['crew'];
  161.         $crew null;
  162.         $foundCrew=$this->crewRepository->findOneBy(["id"=>$crewId]);
  163.         if($foundCrew!=null){
  164.             $crew $foundCrew;
  165.         }
  166.         if ($form->isSubmitted() && $form->isValid()) {
  167.             $editRapper
  168.                 ->setClient($client)
  169.                 ->setRank($editRapper->getRank())
  170.                 ->setPseudoname($data['form']['pseudoname'])
  171.                 ->setRapperStyle($data['form']['rapperStyle'])
  172.                 ->setBio($data['form']['bio'])
  173.                 ->setCrew($crew)
  174.                 ->setUpdatedAt(new \DateTime('now'));
  175.         }
  176.         if($this->rapperRepository->save($editRapper,true)){
  177.             return $this->redirectToRoute('profile');
  178.         }else{
  179.             return new Response("Error"Response::HTTP_BAD_REQUEST);
  180.         }
  181.     }
  182.     #[Route(path'/rapper-follow'name'rapperFollow'methods: ['POST'])]
  183.     public function frontendRapperFollowAction(Request $request): JsonResponse
  184.     {
  185.         $data $request->request->all();
  186.         $user $this->getUser();
  187.         $client $this->clientRepository->findOneByEmail($user->getUserIdentifier());
  188.         $editRapper $this->rapperRepository->findOneBy(['id'=>$data['rapper_id']]);
  189.         $editRapper->addFollower($client);
  190.         $notification = new ClientNotification();
  191.         $notification
  192.             ->setCreatedAt(new \DateTime('now'))
  193.             ->setTitle('New rapper follower '.$client->getName())
  194.             ->setNotificationText('You now have ('.count($editRapper->getFollowers()).') followers on your rapper profile.')
  195.             ->setType(NotificationType::INTERACTION_TYPE_FOLLOWED)
  196.             ->setClient($editRapper->getClient())
  197.             ->setNotificationRead(false);
  198.         $this->clientNotificationRepository->save($notification,true);
  199.         if($this->rapperRepository->save($editRapper,true)){
  200.             return new JsonResponse($editRapper->getPseudoname(), Response::HTTP_CREATED);
  201.         }else{
  202.             return new JsonResponse("Error"Response::HTTP_BAD_REQUEST);
  203.         }
  204.     }
  205.     #[Route(path'/rapper-unfollow'name'rapperUnfollow'methods: ['POST'])]
  206.     public function frontendRapperUnfollowAction(Request $request): JsonResponse
  207.     {
  208.         $data $request->request->all();
  209.         $user $this->getUser();
  210.         $client $this->clientRepository->findOneByEmail($user->getUserIdentifier());
  211.         $editRapper $this->rapperRepository->findOneBy(['id'=>$data['rapper_id']]);
  212.         $editRapper->removeFollower($client);
  213.         if($this->rapperRepository->save($editRapper,true)){
  214.             return new JsonResponse($editRapper->getPseudoname(), Response::HTTP_CREATED);
  215.         }else{
  216.             return new JsonResponse("Error"Response::HTTP_BAD_REQUEST);
  217.         }
  218.     }
  219.     #[Route("/get-rappers/"name"getRappersApi")]
  220.     public  function getRappersUrlAction(): JsonResponse
  221.     {
  222.         $rappers=$this->rapperRepository->findAll();
  223.         $data = [];
  224.         foreach ($rappers as $rapper){
  225.             $latestBattles = [];
  226.             $openedBattles = [];
  227.             if($rapper!=null){
  228.                 if($this->battleRepository->findLatestForRapper($rapper,6)!=null){
  229.                     $latestBattles=$this->battleRepository->findLatestForRapper($rapper,0);
  230.                 }
  231.                 if($this->battleRepository->findOpenedForRapper($rapper,6)!=null){
  232.                     $openedBattles $this->battleRepository->findOpenedForRapper($rapper,0);
  233.                 }
  234.             }
  235.             $data[] = [
  236.                 'id' => $rapper->getId(),
  237.                 'pseudoname' => $rapper->getPseudoname(),
  238.                 'fullName' => $rapper->getClient()->getName()." ".$rapper->getClient()->getSurname(),
  239.                 'avatarName' => $rapper->getAvatarName(),
  240.                 'bio' => $rapper->getBio(),
  241.                 'rapperStyle' => $rapper->getRapperStyle(),
  242.                 'avatarSize' => $rapper->getAvatarSize(),
  243.                 'openedBattles'=>$openedBattles,
  244.                 'latestBattles'=>$latestBattles
  245.             ];
  246.         }
  247.         $jsonResponse = [
  248.             "rappers" => $data
  249.         ];
  250.         return new JsonResponse($jsonResponse,200);
  251.     }
  252. }