src/Controller/AuthorController.php line 85

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\BeatAuthor;
  4. use App\Entity\ClientNotification;
  5. use App\Entity\NotificationType;
  6. use App\Repository\BeatAuthorRepository;
  7. use App\Repository\BeatRepository;
  8. use App\Repository\ClientNotificationRepository;
  9. use App\Repository\ClientRepository;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\JsonResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. class AuthorController extends AbstractController
  16. {
  17.     private BeatAuthorRepository $beatAuthorRepository;
  18.     private ClientRepository $clientRepository;
  19.     private BeatRepository $beatRepository;
  20.     private UserDataController $userDataController;
  21.     private ClientNotificationRepository $clientNotificationRepository;
  22.     public function __construct(
  23.         BeatAuthorRepository $beatAuthorRepository,
  24.         ClientRepository $clientRepository,
  25.         UserDataController $userDataController,
  26.         BeatRepository $beatRepository,
  27.         ClientNotificationRepository $clientNotificationRepository
  28.     )
  29.     {
  30.         $this->beatAuthorRepository $beatAuthorRepository;
  31.         $this->clientRepository $clientRepository;
  32.         $this->userDataController $userDataController;
  33.         $this->beatRepository $beatRepository;
  34.         $this->clientNotificationRepository $clientNotificationRepository;
  35.     }
  36.     #[Route(path'/author/create'name'authorCreate'methods: ['POST'])]
  37.     public function frontendAuthorCreateAction(Request $request): JsonResponse
  38.     {
  39.         $data $request->request->all();
  40.         $user $this->getUser();
  41.         $client $this->clientRepository->findOneByEmail($user->getUserIdentifier());
  42.         $newAuthor = new BeatAuthor();
  43.         $newAuthor
  44.             ->setPseudoname($data['beat_author']['pseudoname'])
  45.             ->setClient($client)
  46.             ->setFirstname($client->getName())
  47.             ->setLastname($client->getSurname())
  48.             ->setCreatedAt(new \DateTime('now'));
  49.         $this->beatAuthorRepository->save($newAuthor,true);
  50.         return new JsonResponse('Created',201);
  51.     }
  52.     #[Route(path'/author/edit'name'authorEdit'methods: ['POST'])]
  53.     public function frontendAuthorEditAction(Request $request): JsonResponse
  54.     {
  55.         $data $request->request->all();
  56.         $user $this->getUser();
  57.         $client $this->clientRepository->findOneByEmail($user->getUserIdentifier());
  58.         $editAuthor $client->getAuthor();
  59.         $editAuthor
  60.             ->setPseudoname($data['beat_author']['pseudoname'])
  61.             ->setClient($client)
  62.             ->setFirstname($client->getName())
  63.             ->setLastname($client->getSurname())
  64.             ->setUpdatedAt(new \DateTime('now'));
  65.         $this->beatAuthorRepository->save($editAuthor,true);
  66.         return new JsonResponse('Done',200);
  67.     }
  68.     #[Route(path:  [
  69.         'cs' => '/autor/{id}',
  70.         'en' => '/author/{id}'
  71.     ], name'authorDetail')]
  72.     public function frontendRapperDetailAction($id): Response
  73.     {
  74.         $user $this->getUser();
  75.         $author $this->beatAuthorRepository->findOneBy(['id'=>$id]);
  76.         $mostFavoriteBeats $this->beatRepository->findAllFavoritedBeatsByAuthor($author,6);
  77.         if ($user==null){
  78.             return $this->render('pages/author-detail.html.twig',[
  79.                 "author"=>$author,
  80.                 "mostFavoriteBeats"=>$mostFavoriteBeats
  81.             ]);
  82.         }else{
  83.             $client $this->clientRepository->findOneByEmail($user->getUserIdentifier());
  84.             if($client!=null && $client->isIsActive()){
  85.                 return $this->render('pages/author-detail.html.twig',[
  86.                     'userData'=>$this->userDataController->getUserData(),
  87.                     "author"=>$author,
  88.                     "mostFavoriteBeats"=>$mostFavoriteBeats
  89.                 ]);
  90.             }else{
  91.                 return $this->render('pages/non-active-content.html.twig',[
  92.                     'userData'=>$this->userDataController->getUserData(),
  93.                     "author"=>$author,
  94.                     "mostFavoriteBeats"=>$mostFavoriteBeats
  95.                 ]);
  96.             }
  97.         }
  98.     }
  99.     #[Route(path'/author-follow'name'authorFollow'methods: ['POST'])]
  100.     public function frontendAuthorFollowAction(Request $request): JsonResponse
  101.     {
  102.         $data $request->request->all();
  103.         $user $this->getUser();
  104.         $client $this->clientRepository->findOneByEmail($user->getUserIdentifier());
  105.         $editAuthor $this->beatAuthorRepository->findOneBy(['id'=>$data['author_id']]);
  106.         $editAuthor->addFollower($client);
  107.         $notification = new ClientNotification();
  108.         $notification
  109.             ->setCreatedAt(new \DateTime('now'))
  110.             ->setTitle('New beat maker follower '.$client->getName())
  111.             ->setNotificationText('You now have ('.count($editAuthor->getFollowers()).') followers on your beat maker profile.')
  112.             ->setType(NotificationType::INTERACTION_TYPE_FOLLOWED)
  113.             ->setClient($editAuthor->getClient())
  114.             ->setNotificationRead(false);
  115.         $this->clientNotificationRepository->save($notification,true);
  116.         if($this->beatAuthorRepository->save($editAuthor,true)){
  117.             return new JsonResponse($editAuthor->getPseudoname(), Response::HTTP_CREATED);
  118.         }else{
  119.             return new JsonResponse("Error"Response::HTTP_BAD_REQUEST);
  120.         }
  121.     }
  122.     #[Route(path'/author-unfollow'name'authorUnfollow'methods: ['POST'])]
  123.     public function frontendAuthorUnfollowAction(Request $request): JsonResponse
  124.     {
  125.         $data $request->request->all();
  126.         $user $this->getUser();
  127.         $client $this->clientRepository->findOneByEmail($user->getUserIdentifier());
  128.         $editAuthor $this->beatAuthorRepository->findOneBy(['id'=>$data['author_id']]);
  129.         $editAuthor->removeFollower($client);
  130.         if($this->beatAuthorRepository->save($editAuthor,true)){
  131.             return new JsonResponse($editAuthor->getPseudoname(), Response::HTTP_CREATED);
  132.         }else{
  133.             return new JsonResponse("Error"Response::HTTP_BAD_REQUEST);
  134.         }
  135.     }
  136. }