<?php
namespace App\Controller\API;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use App\Repository\DataVersionRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class DataVersionController extends AbstractAPIController
{
#[Route('/api/data-version', name: 'app_data_version', methods: ['GET'])]
public function index(Request $request, DataVersionRepository $dataVersionRepo): JsonResponse
{
// check jwt-authorization
$token = $this->authenticationService->verifyToken($request);
if (!$token) {
return new JsonResponse([
'error' => 'Unauthorized access.',
], 401);
}
return $this->json([
'data_version' => $dataVersionRepo->findOneBy([], ['id' => 'ASC'])->getVersionCounter()
]);
}
#[Route('/api/data-version', name: 'post_data_version', methods: ['POST'])]
public function handleFormSubmission(Request $request, DataVersionRepository $dataVersionRepo, EntityManagerInterface $em): JsonResponse
{
$user = $this->getUser();
if (!$user) {
return $this->json([
'error' => 'Unauthorized access.'
], 401);
}
$newVersionCounter = $request->request->get('versionCounter');
if (empty($newVersionCounter)) {
return $this->json([
'error' => 'Invalid format.'
], 422);
}
$versionEntry = $dataVersionRepo->findOneBy([], ['id' => 'ASC']);
if (!$versionEntry) {
return $this->json([
'error' => 'No data version entry found.'
], 404);
}
$versionEntry->setVersionCounter($newVersionCounter);
$em->persist($versionEntry);
$em->flush();
return $this->json($versionEntry);
}
}