src/Controller/BeatController.php line 152

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Beat;
  4. use App\Form\Type\BeatType;
  5. use App\Repository\BeatRepository;
  6. use App\Repository\ClientRepository;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. class BeatController extends AbstractController
  13. {
  14.     private BeatRepository $beatRepository;
  15.     private ClientRepository $clientRepository;
  16.     private UserDataController $userDataController;
  17.     public function __construct(
  18.         BeatRepository $beatRepository,
  19.         ClientRepository $clientRepository,
  20.         UserDataController $userDataController
  21.     )
  22.     {
  23.         $this->beatRepository $beatRepository;
  24.         $this->clientRepository $clientRepository;
  25.         $this->userDataController$userDataController;
  26.     }
  27.     #[Route("/get-beats/"name"getBeatsApi")]
  28.     public  function getBeatsUrlAction(): JsonResponse
  29.     {
  30.         $beats=$this->beatRepository->findAll();
  31.         $data = [];
  32.         foreach ($beats as $beat){
  33.             $categories=[];
  34.             if(count($beat->getCategory())>0){
  35.                 foreach ($beat->getCategory() as $category){
  36.                     $categories[]=[
  37.                         'id' => $category->getId(),
  38.                         'name' => $category->getName()
  39.                     ];
  40.                 }
  41.             }
  42.             $data[] = [
  43.                 'id' => $beat->getId(),
  44.                 'name' => $beat->getName(),
  45.                 'beatFileName' => $beat->getInstrumentalFileName(),
  46.                 'beatFileSize' => $beat->getInstrumentalSize(),
  47.                 'imageFileName' => $beat->getImageName(),
  48.                 'imageFileSize' => $beat->getImageSize(),
  49.                 'beatAuthor'=> $beat->getAuthor()->__toArray(),
  50.                 'beatLength'=> $beat->getBeatLength(),
  51.                 'categories'=>$categories,
  52.                 'used'=> count($beat->getBattles())." times"
  53.             ];
  54.         }
  55.         $jsonResponse = [
  56.             "beats" => $data
  57.         ];
  58.         return new JsonResponse($jsonResponse,200);
  59.     }
  60.     #[Route("/get-beat/{id}"name"getBeatLink")]
  61.     public  function getBeatUrlAction($id): Response
  62.     {
  63.         $beat=$this->beatRepository->findOneBy(['id'=>$id]);
  64.         if($beat){
  65.             return new Response("https://rapbattleonline.com/uploads/audio/beats/".$beat->getInstrumentalFileName(),200);
  66.         }else{
  67.             return new Response("NotFound",404);
  68.         }
  69.         
  70.     }
  71.     #[Route("/favorite-beat"name"favorite_beat")]
  72.     public function resetFavoriteBeatAction(Request $request): JsonResponse
  73.     {
  74.         $data $request->request->all();
  75.         $user $this->getUser();
  76.         $client $this->clientRepository->findOneBy(['email'=>$user->getUserIdentifier()]);
  77.         $beat $this->beatRepository->findOneBy(['id'=>$data['beat_id']]);
  78.         $client->addFavoriteBeat($beat);
  79.         $this->clientRepository->save($client,true);
  80.         return new JsonResponse("success",200);
  81.     }
  82.     #[Route("/unfavorite-beat"name"unfavorite_beat")]
  83.     public function resetUnfavoriteBeatAction(Request $request): JsonResponse
  84.     {
  85.         $data $request->request->all();
  86.         $user $this->getUser();
  87.         $client $this->clientRepository->findOneBy(['email'=>$user->getUserIdentifier()]);
  88.         $beat $this->beatRepository->findOneBy(['id'=>$data['beat_id']]);
  89.         $client->removeFavoriteBeat($beat);
  90.         $this->clientRepository->save($client,true);
  91.         return new JsonResponse("success",200);
  92.     }
  93.     #[Route("/beat-create"name"create_beat")]
  94.     public function createBeatAction(Request $request): Response
  95.     {
  96.         $data $request->request->all();
  97.         $user $this->getUser();
  98.         $author $this->clientRepository->findOneBy(['email'=>$user->getUserIdentifier()])->getAuthor();
  99.         $newBeat = new Beat();
  100.         $form $this->createForm(BeatType::class,$newBeat);
  101.         $form->handleRequest($request);
  102.         if($form->isSubmitted() && $form->isValid()) {
  103.             $newBeat
  104.                 ->setAuthor($author)
  105.                 ->setName($data['beat']['name'])
  106.                 ->setCreatedAt(new \DateTime('now'));
  107.             $this->beatRepository->save($newBeat,true);
  108.         }
  109.         return $this->redirectToRoute('profile');
  110.     }
  111.     #[Route("/beat-remove"name"remove_beat")]
  112.     public function removeBeatAction(Request $request): Response
  113.     {
  114.         $data $request->request->all();
  115.         $beatID=$data['id'];
  116.         $foundBeat=$this->beatRepository->findOneBy(['id'=>$beatID]);
  117.         $this->beatRepository->remove($foundBeat,true);
  118.         return $this->redirectToRoute('profile');
  119.     }
  120.     #[Route(path:  [
  121.         'en' => '/beat/{id}',
  122.         'cs' => '/detail-beatu/{id}'
  123.     ], name'beatDetail')]
  124.     public function frontendBeatDetailAction($id): Response
  125.     {
  126.         $user $this->getUser();
  127.         $beat $this->beatRepository->findOneBy(['id'=>$id]);
  128.         if($user==null){
  129.             return $this->render('pages/beat-detail.html.twig',[
  130.                 'beat'=>$beat
  131.             ]);
  132.         }else{
  133.             $client $this->clientRepository->findOneByEmail($user->getUserIdentifier());
  134.             if($client!=null && $client->isIsActive()){
  135.                 return $this->render('pages/beat-detail.html.twig',[
  136.                     'beat'=>$beat,
  137.                     'userData'=>$this->userDataController->getUserData(),
  138.                 ]);
  139.             }else{
  140.                 return $this->render('pages/non-active-content.html.twig',[
  141.                     'email'=>$client->getEmail(),
  142.                     'userData'=>$this->userDataController->getUserData(),
  143.                     'user'=>$client
  144.                 ]);
  145.             }
  146.         }
  147.     }
  148. }