src/Controller/SolicitacaoController.php line 38

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use FOS\RestBundle\Controller\Annotations as Rest;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  8. use App\Entity\Interfaces\EntityInterface;
  9. use App\Entity\Solicitacao;
  10. use App\Form\SolicitacaoType;
  11. use App\Repository\SolicitacaoRepository;
  12. use App\Service\SolicitacaoService;
  13. use App\Service\NotificationService;
  14. use Psr\Log\LoggerInterface;
  15. /**
  16.  * @Route("/api", name="api_")
  17.  */
  18. class SolicitacaoController extends AbstractAppController
  19. {
  20.     private $notificationService;
  21.     public function __construct(LoggerInterface $loggerSolicitacaoRepository $repositorySolicitacaoService $serviceNotificationService $notificationService)
  22.     {
  23.         $this->entity = new Solicitacao();
  24.         $this->repository $repository;
  25.         $this->formType SolicitacaoType::class;
  26.         $this->service $service;
  27.         $this->logger $logger;
  28.         $this->notificationService $notificationService;
  29.     }
  30.     /**
  31.      * @Rest\Get("/solicitacao")
  32.      */
  33.     public function list(): Response
  34.     {
  35.         return parent::list();
  36.     }
  37.     /**
  38.      * @Rest\Get("/solicitacao/{selector}")
  39.      * @ParamConverter("entity", class="App\Entity\Solicitacao")
  40.      */
  41.     public function show(EntityInterface $entity): Response
  42.     {
  43.         return parent::show($entity);
  44.     }
  45.     /**
  46.      * @Rest\Post("/solicitacao")
  47.      */
  48.     public function new(Request $request): Response
  49.     {
  50.         $entity $this->entity;
  51.         $entity $this->preHydrateEntity($entity);
  52.         $form $this->createForm($this->formType$entity);
  53.         $data json_decode($request->getContent(), true);
  54.         $form->submit($data);
  55.         if ($form->isSubmitted() && $form->isValid()) {
  56.             try {
  57.                 $this->service->criar($entity);
  58.             } catch (\Exception $e) {
  59.                 $this->logger->error($e->getMessage());
  60.                 return $this->handleView($this->view($e->getMessage(), Response::HTTP_BAD_REQUEST));
  61.             }
  62.             return $this->handleView($this->view([], Response::HTTP_CREATED));
  63.         }
  64.         return $this->handleView($this->view($form->getErrors(), Response::HTTP_BAD_REQUEST));
  65.     }
  66.     /**
  67.      * @Rest\Put("/solicitacao/{selector}/aprovar")
  68.      * @ParamConverter("entity", class="App\Entity\Solicitacao")
  69.      */
  70.     public function aprovar(Solicitacao $entity): Response
  71.     {
  72.         try {
  73.             $this->service->aprovar($entity$this->getUser());
  74.         } catch (\Exception $e) {
  75.             $this->logger->error($e->getMessage());
  76.             return $this->handleView($this->view($e->getMessage(), Response::HTTP_BAD_REQUEST));
  77.         }
  78.         $this->notificationService->sendDownloadStatus($entity->getDispositivo());
  79.         return $this->handleView($this->view($entityResponse::HTTP_OK));
  80.     }
  81.     /**
  82.      * @Rest\Put("/solicitacao/{selector}/recusar")
  83.      * @ParamConverter("entity", class="App\Entity\Solicitacao")
  84.      */
  85.     public function recusar(Solicitacao $entity): Response
  86.     {
  87.         try {
  88.             $this->service->recusar($entity$this->getUser());
  89.         } catch (\Exception $e) {
  90.             $this->logger->error($e->getMessage());
  91.             return $this->handleView($this->view($e->getMessage(), Response::HTTP_BAD_REQUEST));
  92.         }
  93.         $this->notificationService->sendDownloadStatus($entity->getDispositivo());
  94.         return $this->handleView($this->view($entityResponse::HTTP_OK));
  95.     }
  96.     /**
  97.      * @Rest\Get("/solicitacao_pendente")
  98.      */
  99.     public function listPendentes(): Response
  100.     {
  101.         $criteria = ['status' => Solicitacao::PENDENTE];
  102.         $orderBy = ['dataHoraSolicitacao' => 'DESC'];
  103.         return $this->handleView($this->view($this->repository->findByUser($this->getUser(), $criteria$orderBy)));
  104.     }
  105.     /**
  106.      * @Rest\Get("/solicitacao_aprovada_ultima")
  107.      */
  108.     public function listAprovadasUltimas(): Response
  109.     {
  110.         $criteria = ['status' => [Solicitacao::APROVADASolicitacao::PRE_APROVADA]];
  111.         $orderBy = ['dataHoraStatus' => 'DESC'];
  112.         $limit 10;
  113.         return $this->handleView($this->view($this->repository->findByUser($this->getUser(), $criteria$orderBy$limit)));
  114.     }
  115. }