<?php
namespace App\Controller;
use App\Entity\BeatAuthor;
use App\Entity\ClientNotification;
use App\Entity\NotificationType;
use App\Repository\BeatAuthorRepository;
use App\Repository\BeatRepository;
use App\Repository\ClientNotificationRepository;
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 AuthorController extends AbstractController
{
private BeatAuthorRepository $beatAuthorRepository;
private ClientRepository $clientRepository;
private BeatRepository $beatRepository;
private UserDataController $userDataController;
private ClientNotificationRepository $clientNotificationRepository;
public function __construct(
BeatAuthorRepository $beatAuthorRepository,
ClientRepository $clientRepository,
UserDataController $userDataController,
BeatRepository $beatRepository,
ClientNotificationRepository $clientNotificationRepository
)
{
$this->beatAuthorRepository = $beatAuthorRepository;
$this->clientRepository = $clientRepository;
$this->userDataController = $userDataController;
$this->beatRepository = $beatRepository;
$this->clientNotificationRepository = $clientNotificationRepository;
}
#[Route(path: '/author/create', name: 'authorCreate', methods: ['POST'])]
public function frontendAuthorCreateAction(Request $request): JsonResponse
{
$data = $request->request->all();
$user = $this->getUser();
$client = $this->clientRepository->findOneByEmail($user->getUserIdentifier());
$newAuthor = new BeatAuthor();
$newAuthor
->setPseudoname($data['beat_author']['pseudoname'])
->setClient($client)
->setFirstname($client->getName())
->setLastname($client->getSurname())
->setCreatedAt(new \DateTime('now'));
$this->beatAuthorRepository->save($newAuthor,true);
return new JsonResponse('Created',201);
}
#[Route(path: '/author/edit', name: 'authorEdit', methods: ['POST'])]
public function frontendAuthorEditAction(Request $request): JsonResponse
{
$data = $request->request->all();
$user = $this->getUser();
$client = $this->clientRepository->findOneByEmail($user->getUserIdentifier());
$editAuthor = $client->getAuthor();
$editAuthor
->setPseudoname($data['beat_author']['pseudoname'])
->setClient($client)
->setFirstname($client->getName())
->setLastname($client->getSurname())
->setUpdatedAt(new \DateTime('now'));
$this->beatAuthorRepository->save($editAuthor,true);
return new JsonResponse('Done',200);
}
#[Route(path: [
'cs' => '/autor/{id}',
'en' => '/author/{id}'
], name: 'authorDetail')]
public function frontendRapperDetailAction($id): Response
{
$user = $this->getUser();
$author = $this->beatAuthorRepository->findOneBy(['id'=>$id]);
$mostFavoriteBeats = $this->beatRepository->findAllFavoritedBeatsByAuthor($author,6);
if ($user==null){
return $this->render('pages/author-detail.html.twig',[
"author"=>$author,
"mostFavoriteBeats"=>$mostFavoriteBeats
]);
}else{
$client = $this->clientRepository->findOneByEmail($user->getUserIdentifier());
if($client!=null && $client->isIsActive()){
return $this->render('pages/author-detail.html.twig',[
'userData'=>$this->userDataController->getUserData(),
"author"=>$author,
"mostFavoriteBeats"=>$mostFavoriteBeats
]);
}else{
return $this->render('pages/non-active-content.html.twig',[
'userData'=>$this->userDataController->getUserData(),
"author"=>$author,
"mostFavoriteBeats"=>$mostFavoriteBeats
]);
}
}
}
#[Route(path: '/author-follow', name: 'authorFollow', methods: ['POST'])]
public function frontendAuthorFollowAction(Request $request): JsonResponse
{
$data = $request->request->all();
$user = $this->getUser();
$client = $this->clientRepository->findOneByEmail($user->getUserIdentifier());
$editAuthor = $this->beatAuthorRepository->findOneBy(['id'=>$data['author_id']]);
$editAuthor->addFollower($client);
$notification = new ClientNotification();
$notification
->setCreatedAt(new \DateTime('now'))
->setTitle('New beat maker follower '.$client->getName())
->setNotificationText('You now have ('.count($editAuthor->getFollowers()).') followers on your beat maker profile.')
->setType(NotificationType::INTERACTION_TYPE_FOLLOWED)
->setClient($editAuthor->getClient())
->setNotificationRead(false);
$this->clientNotificationRepository->save($notification,true);
if($this->beatAuthorRepository->save($editAuthor,true)){
return new JsonResponse($editAuthor->getPseudoname(), Response::HTTP_CREATED);
}else{
return new JsonResponse("Error", Response::HTTP_BAD_REQUEST);
}
}
#[Route(path: '/author-unfollow', name: 'authorUnfollow', methods: ['POST'])]
public function frontendAuthorUnfollowAction(Request $request): JsonResponse
{
$data = $request->request->all();
$user = $this->getUser();
$client = $this->clientRepository->findOneByEmail($user->getUserIdentifier());
$editAuthor = $this->beatAuthorRepository->findOneBy(['id'=>$data['author_id']]);
$editAuthor->removeFollower($client);
if($this->beatAuthorRepository->save($editAuthor,true)){
return new JsonResponse($editAuthor->getPseudoname(), Response::HTTP_CREATED);
}else{
return new JsonResponse("Error", Response::HTTP_BAD_REQUEST);
}
}
}