-
Notifications
You must be signed in to change notification settings - Fork 265
Expand file tree
/
Copy pathRestController.php
More file actions
107 lines (86 loc) · 3.34 KB
/
RestController.php
File metadata and controls
107 lines (86 loc) · 3.34 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
namespace Lexik\Bundle\TranslationBundle\Controller;
use Lexik\Bundle\TranslationBundle\Util\Csrf\CsrfCheckerTrait;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
/**
* @author Cédric Girard <c.girard@lexik.fr>
*/
class RestController extends Controller
{
use CsrfCheckerTrait;
/**
* @param Request $request
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function listAction(Request $request)
{
list($transUnits, $count) = $this->get('lexik_translation.data_grid.request_handler')->getPage($request);
return $this->get('lexik_translation.data_grid.formatter')->createListResponse($transUnits, $count);
}
/**
* @param Request $request
* @param $token
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function listByProfileAction(Request $request, $token)
{
list($transUnits, $count) = $this->get('lexik_translation.data_grid.request_handler')->getPageByToken($request, $token);
return $this->get('lexik_translation.data_grid.formatter')->createListResponse($transUnits, $count);
}
/**
* @param Request $request
* @param integer $id
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
*
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
public function updateAction(Request $request, $id)
{
if (!$request->isMethod('PUT')) {
throw $this->createNotFoundException(sprintf('Invalid request method %s, PUT only.', $request->getMethod()));
}
$this->checkCsrf();
$transUnit = $this->get('lexik_translation.data_grid.request_handler')->updateFromRequest($id, $request);
return $this->get('lexik_translation.data_grid.formatter')->createSingleResponse($transUnit);
}
/**
* @param integer $id
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
*
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
public function deleteAction($id)
{
$this->checkCsrf();
$transUnit = $this->get('lexik_translation.translation_storage')->getTransUnitById($id);
if (!$transUnit) {
throw $this->createNotFoundException(sprintf('No TransUnit found for id "%s".', $id));
}
$deleted = $this->get('lexik_translation.trans_unit.manager')->delete($transUnit);
return new JsonResponse(array('deleted' => $deleted), $deleted ? 200 : 400);
}
/**
* @param integer $id
* @param string $locale
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
*
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
public function deleteTranslationAction($id, $locale)
{
$this->checkCsrf();
$transUnit = $this->get('lexik_translation.translation_storage')->getTransUnitById($id);
if (!$transUnit) {
throw $this->createNotFoundException(sprintf('No TransUnit found for id "%s".', $id));
}
$deleted = $this->get('lexik_translation.trans_unit.manager')->deleteTranslation($transUnit, $locale);
return new JsonResponse(array('deleted' => $deleted), $deleted ? 200 : 400);
}
}