7
7
use App \Common \DoctrineListRepresentationFactory ;
8
8
use App \Entity \Event ;
9
9
use App \Repository \EventRepository ;
10
- use FOS \RestBundle \Controller \Annotations as Rest ;
11
- use FOS \RestBundle \Controller \Annotations \RouteResource ;
12
- use FOS \RestBundle \Routing \ClassResourceInterface ;
13
- use FOS \RestBundle \View \ViewHandlerInterface ;
14
10
use Sulu \Bundle \MediaBundle \Entity \MediaRepositoryInterface ;
15
- use Sulu \ Component \ Rest \ AbstractRestController ;
11
+ use Symfony \ Bundle \ FrameworkBundle \ Controller \ AbstractController ;
16
12
use Symfony \Component \HttpFoundation \Request ;
17
13
use Symfony \Component \HttpFoundation \Response ;
18
14
use Symfony \Component \HttpKernel \Exception \NotFoundHttpException ;
19
- use Symfony \Component \Security \ Core \ Authentication \ Token \ Storage \ TokenStorageInterface ;
15
+ use Symfony \Component \Routing \ Annotation \ Route ;
20
16
21
- /**
22
- * @RouteResource("event")
23
- */
24
- class EventController extends AbstractRestController implements ClassResourceInterface
17
+ class EventController extends AbstractController
25
18
{
26
19
/**
27
20
* @var EventRepository
28
21
*/
29
- private $ repository ;
22
+ private $ eventRepository ;
30
23
31
24
/**
32
25
* @var MediaRepositoryInterface
@@ -42,55 +35,60 @@ public function __construct(
42
35
EventRepository $ repository ,
43
36
MediaRepositoryInterface $ mediaRepository ,
44
37
DoctrineListRepresentationFactory $ doctrineListRepresentationFactory ,
45
- ViewHandlerInterface $ viewHandler ,
46
- ?TokenStorageInterface $ tokenStorage = null
47
38
) {
48
- $ this ->repository = $ repository ;
39
+ $ this ->eventRepository = $ repository ;
49
40
$ this ->mediaRepository = $ mediaRepository ;
50
41
$ this ->doctrineListRepresentationFactory = $ doctrineListRepresentationFactory ;
51
-
52
- parent ::__construct ($ viewHandler , $ tokenStorage );
53
42
}
54
43
55
- public function cgetAction (Request $ request ): Response
44
+ /**
45
+ * @Route("/admin/api/events/{id}", methods={"GET"}, name="app.get_event")
46
+ */
47
+ public function getAction (int $ id , Request $ request ): Response
56
48
{
57
- $ locale = $ request ->query ->get ('locale ' );
58
- $ listRepresentation = $ this ->doctrineListRepresentationFactory ->createDoctrineListRepresentation (
59
- Event::RESOURCE_KEY ,
60
- [],
61
- ['locale ' => $ locale ]
62
- );
49
+ $ event = $ this ->load ($ id , $ request );
50
+ if (!$ event ) {
51
+ throw new NotFoundHttpException ();
52
+ }
63
53
64
- return $ this ->handleView ($ this ->view ( $ listRepresentation ));
54
+ return $ this ->json ($ this ->getDataForEntity ( $ event ));
65
55
}
66
56
67
- public function getAction (int $ id , Request $ request ): Response
57
+ /**
58
+ * @Route("/admin/api/events/{id}", methods={"PUT"}, name="app.put_event")
59
+ */
60
+ public function putAction (int $ id , Request $ request ): Response
68
61
{
69
- $ entity = $ this ->load ($ id , $ request );
70
- if (!$ entity ) {
62
+ $ event = $ this ->load ($ id , $ request );
63
+ if (!$ event ) {
71
64
throw new NotFoundHttpException ();
72
65
}
73
66
74
- return $ this ->handleView ($ this ->view ($ entity ));
67
+ $ this ->mapDataToEntity ($ request ->toArray (), $ event );
68
+ $ this ->save ($ event );
69
+
70
+ return $ this ->json ($ this ->getDataForEntity ($ event ));
75
71
}
76
72
73
+ /**
74
+ * @Route("/admin/api/events", methods={"POST"}, name="app.post_event")
75
+ */
77
76
public function postAction (Request $ request ): Response
78
77
{
79
- $ entity = $ this ->create ($ request );
80
-
81
- $ this ->mapDataToEntity ($ request ->request ->all (), $ entity );
78
+ $ event = $ this ->create ($ request );
82
79
83
- $ this ->save ($ entity );
80
+ $ this ->mapDataToEntity ($ request ->toArray (), $ event );
81
+ $ this ->save ($ event );
84
82
85
- return $ this ->handleView ($ this ->view ( $ entity ) );
83
+ return $ this ->json ($ this ->getDataForEntity ( $ event ), 201 );
86
84
}
87
85
88
86
/**
89
- * @Rest\Post ("/events/{id}")
87
+ * @Route ("/admin/api/ events/{id}", methods={"POST"}, name="app.post_event_trigger ")
90
88
*/
91
89
public function postTriggerAction (int $ id , Request $ request ): Response
92
90
{
93
- $ event = $ this ->repository ->findById ($ id , (string ) $ this ->getLocale ($ request ));
91
+ $ event = $ this ->eventRepository ->findById ($ id , (string ) $ this ->getLocale ($ request ));
94
92
if (!$ event ) {
95
93
throw new NotFoundHttpException ();
96
94
}
@@ -104,30 +102,57 @@ public function postTriggerAction(int $id, Request $request): Response
104
102
break ;
105
103
}
106
104
107
- $ this ->repository ->save ($ event );
105
+ $ this ->eventRepository ->save ($ event );
108
106
109
- return $ this ->handleView ($ this ->view ($ event ));
107
+ return $ this ->json ($ this ->getDataForEntity ($ event ));
110
108
}
111
109
112
- public function putAction (int $ id , Request $ request ): Response
110
+ /**
111
+ * @Route("/admin/api/events/{id}", methods={"DELETE"}, name="app.delete_event")
112
+ */
113
+ public function deleteAction (int $ id ): Response
113
114
{
114
- $ entity = $ this ->load ($ id , $ request );
115
- if (!$ entity ) {
116
- throw new NotFoundHttpException ();
117
- }
115
+ $ this ->remove ($ id );
118
116
119
- $ this ->mapDataToEntity ($ request ->request ->all (), $ entity );
117
+ return $ this ->json (null , 204 );
118
+ }
120
119
121
- $ this ->save ($ entity );
120
+ /**
121
+ * @Route("/admin/api/events", methods={"GET"}, name="app.get_event_list")
122
+ */
123
+ public function getListAction (Request $ request ): Response
124
+ {
125
+ $ listRepresentation = $ this ->doctrineListRepresentationFactory ->createDoctrineListRepresentation (
126
+ Event::RESOURCE_KEY ,
127
+ [],
128
+ ['locale ' => $ this ->getLocale ($ request )]
129
+ );
122
130
123
- return $ this ->handleView ( $ this -> view ( $ entity ));
131
+ return $ this ->json ( $ listRepresentation -> toArray ( ));
124
132
}
125
133
126
- public function deleteAction (int $ id ): Response
134
+ /**
135
+ * @return array<string, mixed>
136
+ */
137
+ protected function getDataForEntity (Event $ entity ): array
127
138
{
128
- $ this ->remove ($ id );
129
-
130
- return $ this ->handleView ($ this ->view ());
139
+ $ image = $ entity ->getImage ();
140
+ $ startDate = $ entity ->getStartDate ();
141
+ $ endDate = $ entity ->getEndDate ();
142
+
143
+ return [
144
+ 'id ' => $ entity ->getId (),
145
+ 'enabled ' => $ entity ->isEnabled (),
146
+ 'title ' => $ entity ->getTitle (),
147
+ 'image ' => $ image
148
+ ? ['id ' => $ image ->getId ()]
149
+ : null ,
150
+ 'teaser ' => $ entity ->getTeaser (),
151
+ 'description ' => $ entity ->getDescription (),
152
+ 'startDate ' => $ startDate ? $ startDate ->format ('c ' ) : null ,
153
+ 'endDate ' => $ endDate ? $ endDate ->format ('c ' ) : null ,
154
+ 'location ' => $ entity ->getLocation (),
155
+ ];
131
156
}
132
157
133
158
/**
@@ -166,21 +191,26 @@ protected function mapDataToEntity(array $data, Event $entity): void
166
191
167
192
protected function load (int $ id , Request $ request ): ?Event
168
193
{
169
- return $ this ->repository ->findById ($ id , (string ) $ this ->getLocale ($ request ));
194
+ return $ this ->eventRepository ->findById ($ id , (string ) $ this ->getLocale ($ request ));
170
195
}
171
196
172
197
protected function create (Request $ request ): Event
173
198
{
174
- return $ this ->repository ->create ((string ) $ this ->getLocale ($ request ));
199
+ return $ this ->eventRepository ->create ((string ) $ this ->getLocale ($ request ));
175
200
}
176
201
177
202
protected function save (Event $ entity ): void
178
203
{
179
- $ this ->repository ->save ($ entity );
204
+ $ this ->eventRepository ->save ($ entity );
180
205
}
181
206
182
207
protected function remove (int $ id ): void
183
208
{
184
- $ this ->repository ->remove ($ id );
209
+ $ this ->eventRepository ->remove ($ id );
210
+ }
211
+
212
+ public function getLocale (Request $ request ): ?string
213
+ {
214
+ return $ request ->query ->has ('locale ' ) ? (string ) $ request ->query ->get ('locale ' ) : null ;
185
215
}
186
216
}
0 commit comments