1414use PhpList \RestBundle \Common \Controller \BaseController ;
1515use PhpList \RestBundle \Common \Service \Provider \PaginatedDataProvider ;
1616use PhpList \RestBundle \Common \Validator \RequestValidator ;
17- use PhpList \RestBundle \Subscription \Request \SubscribePageDataRequest ;
1817use PhpList \RestBundle \Subscription \Request \SubscribePageRequest ;
1918use PhpList \RestBundle \Subscription \Serializer \SubscribePageNormalizer ;
2019use Symfony \Bridge \Doctrine \Attribute \MapEntity ;
@@ -152,14 +151,12 @@ className: SubscribePage::class,
152151 ),
153152 ]
154153 )]
155- public function getPage (
156- Request $ request ,
157- #[MapEntity(mapping: ['id ' => 'id ' ])] ?SubscribePage $ page = null
158- ): JsonResponse {
154+ public function getPage (Request $ request ): JsonResponse {
159155 $ admin = $ this ->requireAuthentication ($ request );
160156 if (!$ admin ->getPrivileges ()->has (PrivilegeFlag::Subscribers)) {
161157 throw $ this ->createAccessDeniedException ('You are not allowed to view subscribe pages. ' );
162158 }
159+ $ page = $ this ->subscribePageManager ->findPage (id: (int ) $ request ->get ('id ' ));
163160
164161 if (!$ page ) {
165162 throw $ this ->createNotFoundException ('Subscribe page not found ' );
@@ -179,6 +176,18 @@ public function getPage(
179176 properties: [
180177 new OA \Property (property: 'title ' , type: 'string ' ),
181178 new OA \Property (property: 'active ' , type: 'boolean ' , nullable: true ),
179+ new OA \Property (
180+ property: 'data ' ,
181+ type: 'array ' ,
182+ items: new OA \Items (
183+ properties: [
184+ new OA \Property (property: 'key ' , type: 'string ' ),
185+ new OA \Property (property: 'value ' , type: 'string ' ),
186+ ],
187+ type: 'object '
188+ ),
189+ nullable: true
190+ ),
182191 ]
183192 )
184193 ),
@@ -221,6 +230,10 @@ public function createPage(Request $request): JsonResponse
221230 $ createRequest = $ this ->validator ->validate ($ request , SubscribePageRequest::class);
222231
223232 $ page = $ this ->subscribePageManager ->createPage ($ createRequest ->title , $ createRequest ->active , $ admin );
233+ if ($ createRequest ->hasData ()) {
234+ $ this ->entityManager ->flush ();
235+ $ this ->subscribePageManager ->syncPageData ($ createRequest ->getDataMap (), $ page );
236+ }
224237 $ this ->entityManager ->flush ();
225238
226239 return $ this ->json ($ this ->normalizer ->normalize ($ page ), Response::HTTP_CREATED );
@@ -237,6 +250,18 @@ public function createPage(Request $request): JsonResponse
237250 properties: [
238251 new OA \Property (property: 'title ' , type: 'string ' , nullable: true ),
239252 new OA \Property (property: 'active ' , type: 'boolean ' , nullable: true ),
253+ new OA \Property (
254+ property: 'data ' ,
255+ type: 'array ' ,
256+ items: new OA \Items (
257+ properties: [
258+ new OA \Property (property: 'key ' , type: 'string ' ),
259+ new OA \Property (property: 'value ' , type: 'string ' ),
260+ ],
261+ type: 'object '
262+ ),
263+ nullable: true
264+ ),
240265 ]
241266 )
242267 ),
@@ -297,6 +322,9 @@ public function updatePage(
297322 active: $ updateRequest ->active ,
298323 owner: $ admin ,
299324 );
325+ if ($ updateRequest ->hasData ()) {
326+ $ this ->subscribePageManager ->syncPageData (data: $ updateRequest ->getDataMap (), page: $ page );
327+ }
300328 $ this ->entityManager ->flush ();
301329
302330 return $ this ->json ($ this ->normalizer ->normalize ($ updated ), Response::HTTP_OK );
@@ -356,162 +384,4 @@ public function deletePage(
356384
357385 return $ this ->json (null , Response::HTTP_NO_CONTENT );
358386 }
359-
360- #[Route('/{id}/data ' , name: 'get_data ' , requirements: ['id ' => '\\d+ ' ], methods: ['GET ' ])]
361- #[OA \Get(
362- path: '/api/v2/subscribe-pages/{id}/data ' ,
363- description: '🚧 **Status: Beta** – This method is under development. Avoid using in production. ' ,
364- summary: 'Get subscribe page data ' ,
365- tags: ['subscriptions ' ],
366- parameters: [
367- new OA \Parameter (
368- name: 'php-auth-pw ' ,
369- description: 'Session key obtained from login ' ,
370- in: 'header ' ,
371- required: true ,
372- schema: new OA \Schema (type: 'string ' )
373- ),
374- new OA \Parameter (
375- name: 'id ' ,
376- description: 'Subscribe page ID ' ,
377- in: 'path ' ,
378- required: true ,
379- schema: new OA \Schema (type: 'integer ' )
380- )
381- ],
382- responses: [
383- new OA \Response (
384- response: 200 ,
385- description: 'Success ' ,
386- content: new OA \JsonContent (
387- type: 'array ' ,
388- items: new OA \Items (
389- properties: [
390- new OA \Property (property: 'id ' , type: 'integer ' ),
391- new OA \Property (property: 'name ' , type: 'string ' ),
392- new OA \Property (property: 'data ' , type: 'string ' , nullable: true ),
393- ],
394- type: 'object '
395- )
396- )
397- ),
398- new OA \Response (
399- response: 403 ,
400- description: 'Failure ' ,
401- content: new OA \JsonContent (ref: '#/components/schemas/UnauthorizedResponse ' )
402- ),
403- new OA \Response (
404- response: 404 ,
405- description: 'Not Found ' ,
406- content: new OA \JsonContent (ref: '#/components/schemas/NotFoundErrorResponse ' )
407- )
408- ]
409- )]
410- public function getPageData (
411- Request $ request ,
412- #[MapEntity(mapping: ['id ' => 'id ' ])] ?SubscribePage $ page = null
413- ): JsonResponse {
414- $ admin = $ this ->requireAuthentication ($ request );
415- if (!$ admin ->getPrivileges ()->has (PrivilegeFlag::Subscribers)) {
416- throw $ this ->createAccessDeniedException ('You are not allowed to view subscribe page data. ' );
417- }
418-
419- if (!$ page ) {
420- throw $ this ->createNotFoundException ('Subscribe page not found ' );
421- }
422-
423- $ data = $ this ->subscribePageManager ->getPageData ($ page );
424-
425- $ json = array_map (static function ($ item ) {
426- return [
427- 'id ' => $ item ->getId (),
428- 'name ' => $ item ->getName (),
429- 'data ' => $ item ->getData (),
430- ];
431- }, $ data );
432-
433- return $ this ->json ($ json , Response::HTTP_OK );
434- }
435-
436- #[Route('/{id}/data ' , name: 'set_data ' , requirements: ['id ' => '\\d+ ' ], methods: ['PUT ' ])]
437- #[OA \Put(
438- path: '/api/v2/subscribe-pages/{id}/data ' ,
439- description: '🚧 **Status: Beta** – This method is under development. Avoid using in production. ' ,
440- summary: 'Set subscribe page data item ' ,
441- requestBody: new OA \RequestBody (
442- required: true ,
443- content: new OA \JsonContent (
444- properties: [
445- new OA \Property (property: 'name ' , type: 'string ' ),
446- new OA \Property (property: 'value ' , type: 'string ' , nullable: true ),
447- ]
448- )
449- ),
450- tags: ['subscriptions ' ],
451- parameters: [
452- new OA \Parameter (
453- name: 'php-auth-pw ' ,
454- description: 'Session key obtained from login ' ,
455- in: 'header ' ,
456- required: true ,
457- schema: new OA \Schema (type: 'string ' )
458- ),
459- new OA \Parameter (
460- name: 'id ' ,
461- description: 'Subscribe page ID ' ,
462- in: 'path ' ,
463- required: true ,
464- schema: new OA \Schema (type: 'integer ' )
465- )
466- ],
467- responses: [
468- new OA \Response (
469- response: 200 ,
470- description: 'Success ' ,
471- content: new OA \JsonContent (
472- properties: [
473- new OA \Property (property: 'id ' , type: 'integer ' ),
474- new OA \Property (property: 'name ' , type: 'string ' ),
475- new OA \Property (property: 'data ' , type: 'string ' , nullable: true ),
476- ],
477- type: 'object '
478- )
479- ),
480- new OA \Response (
481- response: 403 ,
482- description: 'Failure ' ,
483- content: new OA \JsonContent (ref: '#/components/schemas/UnauthorizedResponse ' )
484- ),
485- new OA \Response (
486- response: 404 ,
487- description: 'Not Found ' ,
488- content: new OA \JsonContent (ref: '#/components/schemas/NotFoundErrorResponse ' )
489- )
490- ]
491- )]
492- public function setPageData (
493- Request $ request ,
494- #[MapEntity(mapping: ['id ' => 'id ' ])] ?SubscribePage $ page = null
495- ): JsonResponse {
496- $ admin = $ this ->requireAuthentication ($ request );
497- if (!$ admin ->getPrivileges ()->has (PrivilegeFlag::Subscribers)) {
498- throw $ this ->createAccessDeniedException ('You are not allowed to update subscribe page data. ' );
499- }
500-
501- if (!$ page ) {
502- throw $ this ->createNotFoundException ('Subscribe page not found ' );
503- }
504-
505- /** @var SubscribePageDataRequest $createRequest */
506- $ createRequest = $ this ->validator ->validate ($ request , SubscribePageDataRequest::class);
507-
508- $ item = $ this ->subscribePageManager ->setPageData ($ page , $ createRequest ->name , $ createRequest ->value );
509- $ this ->entityManager ->flush ();
510-
511- return $ this ->json ([
512- 'id ' => $ item ->getId (),
513- 'name ' => $ item ->getName (),
514- 'data ' => $ item ->getData (),
515- ], Response::HTTP_OK );
516- }
517387}
0 commit comments