<?php
namespace App\Controller;
use App\Entity\Beat;
use App\Form\Type\BeatType;
use App\Repository\BeatRepository;
use App\Repository\ClientRepository;
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 BeatController extends AbstractController
{
private BeatRepository $beatRepository;
private ClientRepository $clientRepository;
private UserDataController $userDataController;
public function __construct(
BeatRepository $beatRepository,
ClientRepository $clientRepository,
UserDataController $userDataController
)
{
$this->beatRepository = $beatRepository;
$this->clientRepository = $clientRepository;
$this->userDataController= $userDataController;
}
#[Route("/get-beats/", name: "getBeatsApi")]
public function getBeatsUrlAction(): JsonResponse
{
$beats=$this->beatRepository->findAll();
$data = [];
foreach ($beats as $beat){
$categories=[];
if(count($beat->getCategory())>0){
foreach ($beat->getCategory() as $category){
$categories[]=[
'id' => $category->getId(),
'name' => $category->getName()
];
}
}
$data[] = [
'id' => $beat->getId(),
'name' => $beat->getName(),
'beatFileName' => $beat->getInstrumentalFileName(),
'beatFileSize' => $beat->getInstrumentalSize(),
'imageFileName' => $beat->getImageName(),
'imageFileSize' => $beat->getImageSize(),
'beatAuthor'=> $beat->getAuthor()->__toArray(),
'beatLength'=> $beat->getBeatLength(),
'categories'=>$categories,
'used'=> count($beat->getBattles())." times"
];
}
$jsonResponse = [
"beats" => $data
];
return new JsonResponse($jsonResponse,200);
}
#[Route("/get-beat/{id}", name: "getBeatLink")]
public function getBeatUrlAction($id): Response
{
$beat=$this->beatRepository->findOneBy(['id'=>$id]);
if($beat){
return new Response("https://rapbattleonline.com/uploads/audio/beats/".$beat->getInstrumentalFileName(),200);
}else{
return new Response("NotFound",404);
}
}
#[Route("/favorite-beat", name: "favorite_beat")]
public function resetFavoriteBeatAction(Request $request): JsonResponse
{
$data = $request->request->all();
$user = $this->getUser();
$client = $this->clientRepository->findOneBy(['email'=>$user->getUserIdentifier()]);
$beat = $this->beatRepository->findOneBy(['id'=>$data['beat_id']]);
$client->addFavoriteBeat($beat);
$this->clientRepository->save($client,true);
return new JsonResponse("success",200);
}
#[Route("/unfavorite-beat", name: "unfavorite_beat")]
public function resetUnfavoriteBeatAction(Request $request): JsonResponse
{
$data = $request->request->all();
$user = $this->getUser();
$client = $this->clientRepository->findOneBy(['email'=>$user->getUserIdentifier()]);
$beat = $this->beatRepository->findOneBy(['id'=>$data['beat_id']]);
$client->removeFavoriteBeat($beat);
$this->clientRepository->save($client,true);
return new JsonResponse("success",200);
}
#[Route("/beat-create", name: "create_beat")]
public function createBeatAction(Request $request): Response
{
$data = $request->request->all();
$user = $this->getUser();
$author = $this->clientRepository->findOneBy(['email'=>$user->getUserIdentifier()])->getAuthor();
$newBeat = new Beat();
$form = $this->createForm(BeatType::class,$newBeat);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()) {
$newBeat
->setAuthor($author)
->setName($data['beat']['name'])
->setCreatedAt(new \DateTime('now'));
$this->beatRepository->save($newBeat,true);
}
return $this->redirectToRoute('profile');
}
#[Route("/beat-remove", name: "remove_beat")]
public function removeBeatAction(Request $request): Response
{
$data = $request->request->all();
$beatID=$data['id'];
$foundBeat=$this->beatRepository->findOneBy(['id'=>$beatID]);
$this->beatRepository->remove($foundBeat,true);
return $this->redirectToRoute('profile');
}
#[Route(path: [
'en' => '/beat/{id}',
'cs' => '/detail-beatu/{id}'
], name: 'beatDetail')]
public function frontendBeatDetailAction($id): Response
{
$user = $this->getUser();
$beat = $this->beatRepository->findOneBy(['id'=>$id]);
if($user==null){
return $this->render('pages/beat-detail.html.twig',[
'beat'=>$beat
]);
}else{
$client = $this->clientRepository->findOneByEmail($user->getUserIdentifier());
if($client!=null && $client->isIsActive()){
return $this->render('pages/beat-detail.html.twig',[
'beat'=>$beat,
'userData'=>$this->userDataController->getUserData(),
]);
}else{
return $this->render('pages/non-active-content.html.twig',[
'email'=>$client->getEmail(),
'userData'=>$this->userDataController->getUserData(),
'user'=>$client
]);
}
}
}
}