Skip to content
Snippets Groups Projects
Select Git revision
  • a94fc349e950d81c43f2caf97e08d86213f8ddd1
  • master default
2 results

LeagueOnDeviceRepository.php

Blame
  • DefaultController.php 4.84 KiB
    <?php
    
    namespace ApiBundle\Controller;
    
    use BackendBundle\Entity\Device;
    use FOS\RestBundle\View\View;
    use FOS\RestBundle\Controller\Annotations\Get;
    use FOS\RestBundle\Controller\Annotations\Post;
    use FOS\RestBundle\Controller\FOSRestController;
    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\HttpFoundation\Response;
    
    class DefaultController extends FOSRestController
    {
        /**
         * @return Response
         */
        public function getPredictionsAction()
        {
            $view = View::create()
                ->setStatusCode(200)
                ->setData($this->getPredictionRepository()->findAll());
    
            return $this->handleView($view);
    
        }
    
        /**
         * @return Response
         */
        public function getLeaguesAction()
        {
            $view = View::create()
                ->setStatusCode(200)
                ->setData($this->getLeagueRepository()->findAll());
    
            return $this->handleView($view);
    
        }
    
        /**
         * @return Response
         */
        public function getPacksAction()
        {
            $view = View::create()
                ->setStatusCode(200)
                ->setData($this->getPackRepository()->findAll());
    
            return $this->handleView($view);
    
        }
    
        /**
         * @param int $id
         * @return Response
         *
         * GET Route annotation.
         * @Get("/predictions/league/{id}")
         */
        public function getPredictionsByLeagueAction(int $id)
        {
            $league = $this->getLeagueRepository()->find($id);
    
            $view = View::create();
    
            if ($league) {
                $view->setStatusCode(200);
                $view->setData($this->getPredictionRepository()->findBy([
                    'league' => $league
                ]));
                return $view;
            }
    
            $view->setStatusCode(400);
            return $view;
        }
    
        /**
         * @param $id
         * @return Response
         */
        public function getPredictionAction($id)
        {
            $prediction = $this->getPredictionRepository()->find($id);
    
            $view = View::create();
    
            if ($prediction) {
                $view->setStatusCode(200);
                $view->setData($prediction);
                return $view;
            }
    
            $view->setStatusCode(400);
            return $this->handleView($view);
        }
    
        /**
         * @param Request $request
         * @return Response
         *
         * @Post("/devices/register")
         */
        public function registerDeviceAction(Request $request)
        {
    
            $view = View::create();
    
            if ($request->getMethod() === 'POST') {
                $content = (array)\GuzzleHttp\json_decode($request->getContent());
                $deviceRepository = $this->getDeviceRepository();
                if ($device = $deviceRepository->findOneBy(['uuid' => $content['uuid']])) {
                    $deviceRepository->updateInfo($content,$device);
                } else {
                    $deviceRepository->hydrate($content);
                }
    
                return $this->handleView($view);
    
            }
    
            $view->setStatusCode(500);
            return $this->handleView($view);
        }
    
        /**
         * @param string $uuid
         * @return Response
         *
         * GET Route annotation.
         * @Get("/notifications/{uuid}")
         */
        public function getNotificationsAction(string $uuid)
        {
            $notifications = $this->getNotificationRepository()->get(10);
            $device = $this->getDeviceRepository()->findOneBy(['uuid' => $uuid]);
            if ($device) {
                $status = $this->getNotificationOnDeviceRepository()->findNotificationsByDevice($notifications, $device);
                $view = View::create()
                    ->setStatusCode(200)
                    ->setData([
                        'notifications' => $notifications,
                        'status' => $status
                    ]);
            } else {
                $view = View::create()
                    ->setStatusCode(400);
            }
    
            return $this->handleView($view);
        }
    
        public function readAllNotificationsAction(string $uuid)
        {
            $device = $this->getDeviceRepository()->findOneBy(['uuid' => $uuid]);
            if ($device) {
                $this->getNotificationOnDeviceRepository()->readAllNotifications($device);
            }
        }
    
        private function getNotificationOnDeviceRepository()
        {
            return $this->getDoctrine()->getRepository('BackendBundle:NotificationOnDevice');
        }
    
        private function getDeviceRepository()
        {
            return $this->getDoctrine()->getRepository('BackendBundle:Device');
        }
    
        private function getNotificationRepository()
        {
            return $this->getDoctrine()->getRepository('BackendBundle:Notification');
        }
    
        private function getPredictionRepository()
        {
            return $this->getDoctrine()->getRepository('BackendBundle:Prediction');
        }
    
        private function getLeagueRepository()
        {
            return $this->getDoctrine()->getRepository('BackendBundle:League');
        }
    
        private function getPackRepository()
        {
            return $this->getDoctrine()->getRepository('BackendBundle:Pack');
        }
    }