memory

Play Memory
Log | Files | Refs

PlayerController.php (2848B)


      1 <?php
      2 
      3 namespace App\Controller;
      4 
      5 use App\Entity\Player;
      6 
      7 use Doctrine\ORM\EntityManager;
      8 use Doctrine\Persistence\ManagerRegistry;
      9 use Psr\Log\LoggerInterface;
     10 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
     11 use Symfony\Component\HttpFoundation\Request;
     12 use Symfony\Component\HttpFoundation\Response;
     13 use Symfony\Component\Routing\Annotation\Route;
     14 use Symfony\Component\HttpFoundation\JsonResponse;
     15 
     16 
     17 #[Route("/api/player/")]
     18 class PlayerController extends AbstractController {
     19 
     20     public function __construct(private LoggerInterface $logger) { }
     21 
     22     #[Route('{id}', requirements:['id'=>'\d+'])]
     23     public function getUserData($id, ManagerRegistry $doctrine):Response {
     24         $em = $doctrine->getManager();
     25         $user = $em->find(Player::class, $id);
     26         if ($user) return new JsonResponse($user);
     27         else return new Response('', 404);
     28     }
     29 
     30     #[Route('/')]
     31     public function index():Response {
     32         return new Response("PlayerController");
     33     }
     34 
     35     #[Route('{id}/games',  requirements:['id'=>'\d+'], methods:['GET'])]
     36     public function getPlayerGames($id, ManagerRegistry $doctrine):Response {
     37         $em = $doctrine->getManager();
     38         $user = $em->find(Player::class, $id);
     39         if ($user) return new JsonResponse($user->getGames()->toArray());
     40         else return new Response('', 404);
     41     }
     42 
     43     #[Route('{id}/preferences', requirements:['id'=>'\d+'], methods:['GET', 'POST'])]
     44     public function getPlayerPreferences($id, ManagerRegistry $doctrine):Response {
     45         $em = $doctrine->getManager();
     46         $user = $em->find(Player::class, $id);
     47         if ($user) {
     48             $request = Request::createFromGlobals();
     49             if ($request->getMethod() == 'POST') {
     50                 $params = json_decode(Request::createFromGlobals()->getContent(), true);
     51                 $user->setPreferences($params);
     52                 $em->persist($user);
     53                 $em->flush();
     54                 return new JsonResponse('',204);
     55             } else return new JsonResponse($user->getPreferences());
     56         }
     57 
     58         return new Response('', 404);
     59     }
     60 
     61     #[Route('{id}/email', requirements:['id'=>'\d+'],  methods:['GET', 'PUT'])]
     62     public function playerEmail($id, ManagerRegistry $doctrine):Response {
     63         $em = $doctrine->getManager();
     64         $user = $em->find(Player::class, $id);
     65         if ($user) {
     66             $request = Request::createFromGlobals();
     67             if ($request->getMethod() == 'PUT') {
     68                 $params = json_decode(Request::createFromGlobals()->getContent(), true);
     69                 $user->email = $params['email'];
     70                 $em->persist($user);
     71                 $em->flush();
     72                 return new JsonResponse('', 204);
     73             } else return new JsonResponse($user->email);
     74         }
     75 
     76         return new Response('', 404);    }
     77 
     78 
     79 }