-
Notifications
You must be signed in to change notification settings - Fork 263
Expand file tree
/
Copy pathRestController.php
More file actions
91 lines (70 loc) · 2.91 KB
/
RestController.php
File metadata and controls
91 lines (70 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
namespace Lexik\Bundle\TranslationBundle\Controller;
use Lexik\Bundle\TranslationBundle\Manager\TransUnitManagerInterface;
use Lexik\Bundle\TranslationBundle\Storage\StorageInterface;
use Lexik\Bundle\TranslationBundle\Util\Csrf\CsrfCheckerTrait;
use Lexik\Bundle\TranslationBundle\Util\DataGrid\DataGridFormatter;
use Lexik\Bundle\TranslationBundle\Util\DataGrid\DataGridRequestHandler;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* @author Cédric Girard <c.girard@lexik.fr>
*/
class RestController extends AbstractController
{
use CsrfCheckerTrait;
public function __construct(
private readonly DataGridRequestHandler $dataGridRequestHandler,
private readonly DataGridFormatter $dataGridFormatter,
private readonly StorageInterface $translationStorage,
private readonly TransUnitManagerInterface $transUnitManager,
) {
}
public function listAction(Request $request): JsonResponse
{
[$transUnits, $count] = $this->dataGridRequestHandler->getPage($request);
return $this->dataGridFormatter->createListResponse($transUnits, $count);
}
public function listByProfileAction(Request $request, string $token): JsonResponse
{
[$transUnits, $count] = $this->dataGridRequestHandler->getPageByToken($request, $token);
return $this->dataGridFormatter->createListResponse($transUnits, $count);
}
/**
* @throws NotFoundHttpException
*/
public function updateAction(Request $request, int $id): JsonResponse
{
$this->checkCsrf();
$transUnit = $this->dataGridRequestHandler->updateFromRequest($id, $request);
return $this->dataGridFormatter->createSingleResponse($transUnit);
}
/**
* @throws NotFoundHttpException
*/
public function deleteAction(int $id): JsonResponse
{
$this->checkCsrf();
$transUnit = $this->translationStorage->getTransUnitById($id);
if (!$transUnit) {
throw $this->createNotFoundException(sprintf('No TransUnit found for id "%s".', $id));
}
$deleted = $this->transUnitManager->delete($transUnit);
return new JsonResponse(['deleted' => $deleted], $deleted ? 200 : 400);
}
/**
* @throws NotFoundHttpException
*/
public function deleteTranslationAction(int $id, string $locale): JsonResponse
{
$this->checkCsrf();
$transUnit = $this->translationStorage->getTransUnitById($id);
if (!$transUnit) {
throw $this->createNotFoundException(sprintf('No TransUnit found for id "%s".', $id));
}
$deleted = $this->transUnitManager->deleteTranslation($transUnit, $locale);
return new JsonResponse(['deleted' => $deleted], $deleted ? 200 : 400);
}
}