<?php
namespace App\Controller;
use App\Entity\ClientNotification;
use App\Entity\NotificationType;
use App\Entity\Rapper;
use App\Form\Type\RapperType;
use App\Repository\BattleRepository;
use App\Repository\ClientNotificationRepository;
use App\Repository\ClientRepository;
use App\Repository\CrewRepository;
use App\Repository\RapperRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Security;
use Vich\UploaderBundle\Form\Type\VichImageType;
class RapperController extends AbstractController
{
private RapperRepository $rapperRepository;
private BattleRepository $battleRepository;
private ClientRepository $clientRepository;
private CrewRepository $crewRepository;
private UserDataController $userDataController;
private FrontendController $frontendController;
private ClientNotificationRepository $clientNotificationRepository;
public function __construct(
RapperRepository $rapperRepository,
BattleRepository $battleRepository,
ClientRepository $clientRepository,
CrewRepository $crewRepository,
FrontendController $frontendController,
UserDataController $userDataController,
ClientNotificationRepository $clientNotificationRepository
)
{
$this->rapperRepository=$rapperRepository;
$this->battleRepository=$battleRepository;
$this->clientRepository=$clientRepository;
$this->userDataController = $userDataController;
$this->frontendController = $frontendController;
$this->crewRepository = $crewRepository;
$this->clientNotificationRepository = $clientNotificationRepository;
}
#[Route(path: [
'cs' => '/raperi',
'en' => '/rappers'
], name: 'rappers')]
public function frontendRappersAction(): Response
{
$user = $this->getUser();
$allRappers = $this->rapperRepository->findAll();
if ($user==null){
return $this->render('pages/rappers.html.twig',[
"rappers"=>$allRappers
]);
}else{
$client = $this->clientRepository->findOneByEmail($user->getUserIdentifier());
if($client!=null && $client->isIsActive()){
return $this->render('pages/rappers.html.twig',[
'userData'=>$this->userDataController->getUserData(),
'rappers'=>$allRappers
]);
}else{
return $this->render('pages/non-active-content.html.twig',[
'userData'=>$this->userDataController->getUserData(),
'rappers'=>$allRappers
]);
}
}
}
#[Route(path: [
'cs' => '/raper/{id}',
'en' => '/rapper/{id}'
], name: 'rapperDetail')]
public function frontendRapperDetailAction($id): Response
{
$user = $this->getUser();
$rapper = $this->rapperRepository->findOneBy(['id'=>$id]);
$latestBattles = [];
$openedBattles = [];
if($rapper!=null){
if($this->battleRepository->findLatestForRapper($rapper,6)!=null){
$latestBattles=$this->battleRepository->findLatestForRapper($rapper,6);
}
if($this->battleRepository->findOpenedForRapper($rapper,6)!=null){
$openedBattles = $this->battleRepository->findOpenedForRapper($rapper,6);
}
}
if ($user==null){
return $this->render('pages/rapper-detail.html.twig',[
"rapper"=>$rapper,
"latestBattles"=>$latestBattles,
"openedBattles"=>$openedBattles
]);
}else{
$client = $this->clientRepository->findOneByEmail($user->getUserIdentifier());
if($client!=null && $client->isIsActive()){
return $this->render('pages/rapper-detail.html.twig',[
'userData'=>$this->userDataController->getUserData(),
"rapper"=>$rapper,
"latestBattles"=>$latestBattles,
"openedBattles"=>$openedBattles
]);
}else{
return $this->render('pages/non-active-content.html.twig',[
'userData'=>$this->userDataController->getUserData(),
"rapper"=>$rapper,
"latestBattles"=>$latestBattles,
"openedBattles"=>$openedBattles
]);
}
}
}
#[Route(path: '/rapper-create', name: 'rapperCreate', methods: ['POST'])]
public function frontendRapperCreateAction(Request $request): Response
{
$data = $request->request->all();
$user = $this->getUser();
$client = $this->clientRepository->findOneByEmail($user->getUserIdentifier());
$avatar = $request->files->get('avatarFile');
$crew = null;
if(intval($data['crew'])!==0){
$crew = $this->crewRepository->findOneBy(['id'=>intval($data['crew'])]);
}
$newRapper = new Rapper();
$form=$this->createForm(RapperType::class,$newRapper);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()) {
$newRapper
->setClient($client)
->setRank(500)
->setPseudoname($data['rapper']['pseudoname'])
->setRapperStyle($data['rapper']['rapperStyle'])
->setBio($data['rapper']['bio'])
->setCreatedAt(new \DateTime('now'))
->setUpdatedAt(new \DateTime('now'));
if($crew!=null){
$newRapper->setCrew($crew);
}
if($this->rapperRepository->save($newRapper,true)) {
return $this->redirectToRoute("profile");
}
}
return $this->redirectToRoute("errorPage");
}
#[Route(path: '/rapper-edit', name: 'rapperEdit', methods: ['POST'])]
public function frontendRapperEditAction(Request $request): Response
{
$data = $request->request->all();
$user = $this->getUser();
$client = $this->clientRepository->findOneByEmail($user->getUserIdentifier());
$avatarNew = $request->files->get('avatarFile');
$editRapper = $client->getRapper();
$form = $this->createForm(RapperType::class, $editRapper);
$form->handleRequest($request);
$crewId=$data['form']['crew'];
$crew = null;
$foundCrew=$this->crewRepository->findOneBy(["id"=>$crewId]);
if($foundCrew!=null){
$crew = $foundCrew;
}
if ($form->isSubmitted() && $form->isValid()) {
$editRapper
->setClient($client)
->setRank($editRapper->getRank())
->setPseudoname($data['form']['pseudoname'])
->setRapperStyle($data['form']['rapperStyle'])
->setBio($data['form']['bio'])
->setCrew($crew)
->setUpdatedAt(new \DateTime('now'));
}
if($this->rapperRepository->save($editRapper,true)){
return $this->redirectToRoute('profile');
}else{
return new Response("Error", Response::HTTP_BAD_REQUEST);
}
}
#[Route(path: '/rapper-follow', name: 'rapperFollow', methods: ['POST'])]
public function frontendRapperFollowAction(Request $request): JsonResponse
{
$data = $request->request->all();
$user = $this->getUser();
$client = $this->clientRepository->findOneByEmail($user->getUserIdentifier());
$editRapper = $this->rapperRepository->findOneBy(['id'=>$data['rapper_id']]);
$editRapper->addFollower($client);
$notification = new ClientNotification();
$notification
->setCreatedAt(new \DateTime('now'))
->setTitle('New rapper follower '.$client->getName())
->setNotificationText('You now have ('.count($editRapper->getFollowers()).') followers on your rapper profile.')
->setType(NotificationType::INTERACTION_TYPE_FOLLOWED)
->setClient($editRapper->getClient())
->setNotificationRead(false);
$this->clientNotificationRepository->save($notification,true);
if($this->rapperRepository->save($editRapper,true)){
return new JsonResponse($editRapper->getPseudoname(), Response::HTTP_CREATED);
}else{
return new JsonResponse("Error", Response::HTTP_BAD_REQUEST);
}
}
#[Route(path: '/rapper-unfollow', name: 'rapperUnfollow', methods: ['POST'])]
public function frontendRapperUnfollowAction(Request $request): JsonResponse
{
$data = $request->request->all();
$user = $this->getUser();
$client = $this->clientRepository->findOneByEmail($user->getUserIdentifier());
$editRapper = $this->rapperRepository->findOneBy(['id'=>$data['rapper_id']]);
$editRapper->removeFollower($client);
if($this->rapperRepository->save($editRapper,true)){
return new JsonResponse($editRapper->getPseudoname(), Response::HTTP_CREATED);
}else{
return new JsonResponse("Error", Response::HTTP_BAD_REQUEST);
}
}
#[Route("/get-rappers/", name: "getRappersApi")]
public function getRappersUrlAction(): JsonResponse
{
$rappers=$this->rapperRepository->findAll();
$data = [];
foreach ($rappers as $rapper){
$latestBattles = [];
$openedBattles = [];
if($rapper!=null){
if($this->battleRepository->findLatestForRapper($rapper,6)!=null){
$latestBattles=$this->battleRepository->findLatestForRapper($rapper,0);
}
if($this->battleRepository->findOpenedForRapper($rapper,6)!=null){
$openedBattles = $this->battleRepository->findOpenedForRapper($rapper,0);
}
}
$data[] = [
'id' => $rapper->getId(),
'pseudoname' => $rapper->getPseudoname(),
'fullName' => $rapper->getClient()->getName()." ".$rapper->getClient()->getSurname(),
'avatarName' => $rapper->getAvatarName(),
'bio' => $rapper->getBio(),
'rapperStyle' => $rapper->getRapperStyle(),
'avatarSize' => $rapper->getAvatarSize(),
'openedBattles'=>$openedBattles,
'latestBattles'=>$latestBattles
];
}
$jsonResponse = [
"rappers" => $data
];
return new JsonResponse($jsonResponse,200);
}
}