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,13 @@ 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
155+ {
159156 $ admin = $ this ->requireAuthentication ($ request );
160157 if (!$ admin ->getPrivileges ()->has (PrivilegeFlag::Subscribers)) {
161158 throw $ this ->createAccessDeniedException ('You are not allowed to view subscribe pages. ' );
162159 }
160+ $ page = $ this ->subscribePageManager ->findPage (id: (int ) $ request ->get ('id ' ));
163161
164162 if (!$ page ) {
165163 throw $ this ->createNotFoundException ('Subscribe page not found ' );
@@ -179,6 +177,18 @@ public function getPage(
179177 properties: [
180178 new OA \Property (property: 'title ' , type: 'string ' ),
181179 new OA \Property (property: 'active ' , type: 'boolean ' , nullable: true ),
180+ new OA \Property (
181+ property: 'data ' ,
182+ type: 'array ' ,
183+ items: new OA \Items (
184+ properties: [
185+ new OA \Property (property: 'key ' , type: 'string ' ),
186+ new OA \Property (property: 'value ' , type: 'string ' ),
187+ ],
188+ type: 'object '
189+ ),
190+ nullable: true
191+ ),
182192 ]
183193 )
184194 ),
@@ -221,6 +231,10 @@ public function createPage(Request $request): JsonResponse
221231 $ createRequest = $ this ->validator ->validate ($ request , SubscribePageRequest::class);
222232
223233 $ page = $ this ->subscribePageManager ->createPage ($ createRequest ->title , $ createRequest ->active , $ admin );
234+ if ($ createRequest ->hasData ()) {
235+ $ this ->entityManager ->flush ();
236+ $ this ->subscribePageManager ->syncPageData ($ createRequest ->getDataMap (), $ page );
237+ }
224238 $ this ->entityManager ->flush ();
225239
226240 return $ this ->json ($ this ->normalizer ->normalize ($ page ), Response::HTTP_CREATED );
@@ -237,6 +251,18 @@ public function createPage(Request $request): JsonResponse
237251 properties: [
238252 new OA \Property (property: 'title ' , type: 'string ' , nullable: true ),
239253 new OA \Property (property: 'active ' , type: 'boolean ' , nullable: true ),
254+ new OA \Property (
255+ property: 'data ' ,
256+ type: 'array ' ,
257+ items: new OA \Items (
258+ properties: [
259+ new OA \Property (property: 'key ' , type: 'string ' ),
260+ new OA \Property (property: 'value ' , type: 'string ' ),
261+ ],
262+ type: 'object '
263+ ),
264+ nullable: true
265+ ),
240266 ]
241267 )
242268 ),
@@ -297,6 +323,9 @@ public function updatePage(
297323 active: $ updateRequest ->active ,
298324 owner: $ admin ,
299325 );
326+ if ($ updateRequest ->hasData ()) {
327+ $ this ->subscribePageManager ->syncPageData (data: $ updateRequest ->getDataMap (), page: $ page );
328+ }
300329 $ this ->entityManager ->flush ();
301330
302331 return $ this ->json ($ this ->normalizer ->normalize ($ updated ), Response::HTTP_OK );
@@ -356,162 +385,4 @@ public function deletePage(
356385
357386 return $ this ->json (null , Response::HTTP_NO_CONTENT );
358387 }
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- }
517388}
0 commit comments