"oser_backend/settings/production.py" did not exist on "d04fe080e7ad898b88e9aaea25c973d9a6a1ab60"
Select Git revision
DefaultController.php 6.20 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 string $sport
* @return Response
*
* GET Route annotation.
* @Get("/packs/{sport}")
*/
public function getPacksBySportAction(string $sport)
{
try {
$packs = $this->getPackRepository()->findPacksBySport($sport);
$view = View::create()
->setStatusCode(200)
->setData($packs);
return $this->handleView($view);
} catch (\Exception $e) {
$this->getLogger()->err($e->getMessage());
$view = View::create()
->setStatusCode(400)
->setData($e->getMessage());
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 string $sport
* @return Response
*
* GET Route annotation.
* @Get("/predictions/{sport}/free")
*/
public function getFreePredictionsAction(string $sport)
{
$view = View::create();
$view->setStatusCode(200);
$view->setData($this->getPredictionRepository()->getFreePredictions($sport));
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);
}
/**
* @param string $uuid
*
*
* GET Route annotation.
* @Get("/notifications/{uuid}/read")
*/
public function readAllNotificationsAction(string $uuid)
{
$device = $this->getDeviceRepository()->findOneBy(['uuid' => $uuid]);
if ($device) {
try {
$this->getNotificationOnDeviceRepository()->readAllNotifications($device);
} catch (\Exception $e) {
}
} else {
}
}
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');
}
private function getLogger()
{
return $this->get('logger');
}
}