src/Controller/API/DataVersionController.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Controller\API;
  3. use Symfony\Component\HttpFoundation\JsonResponse;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use App\Repository\DataVersionRepository;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. class DataVersionController extends AbstractAPIController
  11. {
  12.     #[Route('/api/data-version'name'app_data_version'methods: ['GET'])]
  13.     public function index(Request $requestDataVersionRepository $dataVersionRepo): JsonResponse
  14.     {
  15.         // check jwt-authorization
  16.         $token $this->authenticationService->verifyToken($request);
  17.         if (!$token) {
  18.             return new JsonResponse([
  19.                 'error' => 'Unauthorized access.',
  20.             ], 401);
  21.         }
  22.         return $this->json([
  23.             'data_version' => $dataVersionRepo->findOneBy([], ['id' => 'ASC'])->getVersionCounter()
  24.         ]);
  25.     }
  26.     #[Route('/api/data-version'name'post_data_version'methods: ['POST'])]
  27.     public function handleFormSubmission(Request $requestDataVersionRepository $dataVersionRepoEntityManagerInterface $em): JsonResponse
  28.     {
  29.         $user $this->getUser();
  30.     if (!$user) {
  31.         return $this->json([
  32.             'error' => 'Unauthorized access.'
  33.         ], 401);
  34.     }
  35.     $newVersionCounter $request->request->get('versionCounter');
  36.     if (empty($newVersionCounter)) {
  37.         return $this->json([
  38.             'error' => 'Invalid format.'
  39.         ], 422);
  40.     }
  41.     $versionEntry $dataVersionRepo->findOneBy([], ['id' => 'ASC']);
  42.     if (!$versionEntry) {
  43.         return $this->json([
  44.             'error' => 'No data version entry found.'
  45.         ], 404);
  46.     }
  47.     $versionEntry->setVersionCounter($newVersionCounter);
  48.     $em->persist($versionEntry);
  49.     $em->flush();
  50.     return $this->json($versionEntry);
  51.     }
  52. }