6
6
7
7
use OpenApi \Attributes as OA ;
8
8
use PhpList \Core \Domain \Identity \Model \PrivilegeFlag ;
9
+ use PhpList \Core \Domain \Subscription \Model \SubscribePage ;
9
10
use PhpList \Core \Domain \Subscription \Service \Manager \SubscribePageManager ;
10
11
use PhpList \Core \Security \Authentication ;
11
12
use PhpList \RestBundle \Common \Controller \BaseController ;
12
13
use PhpList \RestBundle \Common \Validator \RequestValidator ;
14
+ use PhpList \RestBundle \Subscription \Request \SubscribePageRequest ;
13
15
use PhpList \RestBundle \Subscription \Serializer \SubscribePageNormalizer ;
16
+ use Symfony \Bridge \Doctrine \Attribute \MapEntity ;
14
17
use Symfony \Component \HttpFoundation \JsonResponse ;
15
18
use Symfony \Component \HttpFoundation \Request ;
16
19
use Symfony \Component \HttpFoundation \Response ;
@@ -54,14 +57,7 @@ public function __construct(
54
57
new OA \Response (
55
58
response: 200 ,
56
59
description: 'Success ' ,
57
- content: new OA \JsonContent (
58
- properties: [
59
- new OA \Property (property: 'id ' , type: 'integer ' ),
60
- new OA \Property (property: 'title ' , type: 'string ' ),
61
- new OA \Property (property: 'active ' , type: 'boolean ' ),
62
- new OA \Property (property: 'owner_id ' , type: 'integer ' , nullable: true ),
63
- ]
64
- ),
60
+ content: new OA \JsonContent (ref: '#/components/schemas/SubscribePage ' ),
65
61
),
66
62
new OA \Response (
67
63
response: 403 ,
@@ -75,14 +71,18 @@ public function __construct(
75
71
),
76
72
]
77
73
)]
78
- public function getPage (Request $ request , int $ id ): JsonResponse
79
- {
74
+ public function getPage (
75
+ Request $ request ,
76
+ #[MapEntity(mapping: ['id ' => 'id ' ])] ?SubscribePage $ page = null
77
+ ): JsonResponse {
80
78
$ admin = $ this ->requireAuthentication ($ request );
81
79
if (!$ admin ->getPrivileges ()->has (PrivilegeFlag::Subscribers)) {
82
80
throw $ this ->createAccessDeniedException ('You are not allowed to view subscribe pages. ' );
83
81
}
84
82
85
- $ page = $ this ->subscribePageManager ->getPage ($ id );
83
+ if (!$ page ) {
84
+ throw $ this ->createNotFoundException ('Subscribe page not found ' );
85
+ }
86
86
87
87
return $ this ->json ($ this ->normalizer ->normalize ($ page ), Response::HTTP_OK );
88
88
}
@@ -115,14 +115,7 @@ public function getPage(Request $request, int $id): JsonResponse
115
115
new OA \Response (
116
116
response: 201 ,
117
117
description: 'Created ' ,
118
- content: new OA \JsonContent (
119
- properties: [
120
- new OA \Property (property: 'id ' , type: 'integer ' ),
121
- new OA \Property (property: 'title ' , type: 'string ' ),
122
- new OA \Property (property: 'active ' , type: 'boolean ' ),
123
- new OA \Property (property: 'owner_id ' , type: 'integer ' , nullable: true ),
124
- ]
125
- )
118
+ content: new OA \JsonContent (ref: '#/components/schemas/SubscribePage ' )
126
119
),
127
120
new OA \Response (
128
121
response: 403 ,
@@ -143,19 +136,10 @@ public function createPage(Request $request): JsonResponse
143
136
throw $ this ->createAccessDeniedException ('You are not allowed to create subscribe pages. ' );
144
137
}
145
138
146
- $ data = json_decode ($ request ->getContent (), true ) ?: [];
147
- $ title = isset ($ data ['title ' ]) ? trim ((string )$ data ['title ' ]) : '' ;
148
- $ active = isset ($ data ['active ' ]) ? (bool )$ data ['active ' ] : false ;
149
-
150
- if ($ title === '' ) {
151
- return $ this ->json ([
152
- 'errors ' => [
153
- ['field ' => 'title ' , 'message ' => 'This field is required. ' ]
154
- ]
155
- ], Response::HTTP_UNPROCESSABLE_ENTITY );
156
- }
139
+ /** @var SubscribePageRequest $createRequest */
140
+ $ createRequest = $ this ->validator ->validate ($ request , SubscribePageRequest::class);
157
141
158
- $ page = $ this ->subscribePageManager ->createPage ($ title , $ active , $ admin );
142
+ $ page = $ this ->subscribePageManager ->createPage ($ createRequest -> title , $ createRequest -> active , $ admin );
159
143
160
144
return $ this ->json ($ this ->normalizer ->normalize ($ page ), Response::HTTP_CREATED );
161
145
}
0 commit comments