44
55namespace PhpList \RestBundle \Controller ;
66
7- use PhpList \Core \Domain \Model \Messaging \SubscriberList ;
7+ use PhpList \Core \Domain \Model \Subscription \SubscriberList ;
88use PhpList \Core \Domain \Repository \Subscription \SubscriberRepository ;
9+ use PhpList \RestBundle \Entity \CreateSubscriberListRequest ;
10+ use PhpList \RestBundle \Serializer \SubscriberListNormalizer ;
11+ use PhpList \RestBundle \Service \Manager \SubscriberListManager ;
12+ use PhpList \RestBundle \Validator \RequestValidator ;
913use Symfony \Bridge \Doctrine \Attribute \MapEntity ;
1014use Symfony \Bundle \FrameworkBundle \Controller \AbstractController ;
11- use PhpList \Core \Domain \Repository \Messaging \SubscriberListRepository ;
15+ use PhpList \Core \Domain \Repository \Subscription \SubscriberListRepository ;
1216use PhpList \Core \Security \Authentication ;
1317use PhpList \RestBundle \Controller \Traits \AuthenticationTrait ;
1418use Symfony \Component \HttpFoundation \JsonResponse ;
@@ -34,17 +38,21 @@ class ListController extends AbstractController
3438 private SubscriberListRepository $ subscriberListRepository ;
3539 private SubscriberRepository $ subscriberRepository ;
3640 private SerializerInterface $ serializer ;
41+ private SubscriberListManager $ subscriberListManager ;
42+ private RequestValidator $ validator ;
3743
3844 public function __construct (
3945 Authentication $ authentication ,
4046 SubscriberListRepository $ repository ,
4147 SubscriberRepository $ subscriberRepository ,
42- SerializerInterface $ serializer
48+ SerializerInterface $ serializer ,
49+ RequestValidator $ validator
4350 ) {
4451 $ this ->authentication = $ authentication ;
4552 $ this ->subscriberListRepository = $ repository ;
4653 $ this ->subscriberRepository = $ subscriberRepository ;
4754 $ this ->serializer = $ serializer ;
55+ $ this ->validator = $ validator ;
4856 }
4957
5058 #[Route('' , name: 'get_lists ' , methods: ['GET ' ])]
@@ -340,4 +348,71 @@ public function getSubscribersCount(
340348
341349 return new JsonResponse ($ json , Response::HTTP_OK , [], true );
342350 }
351+
352+ #[Route('' , name: 'create_list ' , methods: ['POST ' ])]
353+ #[OA \Post(
354+ path: '/lists ' ,
355+ description: 'Returns created list. ' ,
356+ summary: 'Create a subscriber list. ' ,
357+ requestBody: new OA \RequestBody (
358+ description: 'Pass parameters to create a new subscriber list. ' ,
359+ required: true ,
360+ content: new OA \JsonContent (
361+ required: ['name ' ],
362+ properties: [
363+ new OA \Property (property: 'name ' , type: 'string ' , format: 'string ' , example: 'News ' ),
364+ new OA \Property (property: 'description ' , type: 'string ' , example: 'News (and some fun stuff) ' ),
365+ new OA \Property (property: 'list_position ' , type: 'number ' , example: 12 ),
366+ new OA \Property (property: 'public ' , type: 'boolean ' , example: true ),
367+ new OA \Property (property: 'owner ' , type: 'number ' , example: 12 ),
368+ ]
369+ )
370+ ),
371+ tags: ['lists ' ],
372+ parameters: [
373+ new OA \Parameter (
374+ name: 'session ' ,
375+ description: 'Session ID obtained from authentication ' ,
376+ in: 'header ' ,
377+ required: true ,
378+ schema: new OA \Schema (
379+ type: 'string '
380+ )
381+ )
382+ ],
383+ responses: [
384+ new OA \Response (
385+ response: 201 ,
386+ description: 'Success ' ,
387+ content: new OA \JsonContent (
388+ type: 'object ' ,
389+ example: [
390+ 'name ' => 'News ' ,
391+ 'description ' => 'News (and some fun stuff) ' ,
392+ 'creation_date ' => '2016-06-22T15:01:17+00:00 ' ,
393+ 'list_position ' => 12 ,
394+ 'subject_prefix ' => 'phpList ' ,
395+ 'public ' => true ,
396+ 'category ' => 'news ' ,
397+ 'id ' => 1
398+ ]
399+ )
400+ ),
401+ new OA \Response (
402+ response: 403 ,
403+ description: 'Failure ' ,
404+ content: new OA \JsonContent (ref: '#/components/schemas/UnauthorizedResponse ' )
405+ )
406+ ]
407+ )]
408+ public function createList (Request $ request , SubscriberListNormalizer $ normalizer ): JsonResponse
409+ {
410+ $ this ->requireAuthentication ($ request );
411+
412+ /** @var CreateSubscriberListRequest $subscriberListRequest */
413+ $ subscriberListRequest = $ this ->validator ->validate ($ request , CreateSubscriberListRequest::class);
414+ $ data = $ this ->subscriberListManager ->createSubscriberList ($ subscriberListRequest );
415+
416+ return new JsonResponse ($ normalizer ->normalize ($ data ), Response::HTTP_CREATED , [], true );
417+ }
343418}
0 commit comments